gpt4 book ai didi

php - Laravel Sanctum token - 两个数据库

转载 作者:行者123 更新时间:2023-12-03 20:46:51 25 4
gpt4 key购买 nike

我在让 Laravel Sanctum 授权两个独立数据库中的两个表时遇到了一些问题。
我正在使用 Laravel Sanctum token 进行授权。我有两个表来授权用户(用户和联系人)我设置了两个单独的保护,并且可以使用一个 token 表使所有内容在单个数据库上工作。
但是我想将联系人表放在一个单独的数据库中。这样做会创建两个personal_access_tokens 表,一个在Users 数据库中,另一个在Contacts 数据库中,我不介意。我可以很好地创建 token ,但是当我尝试使用 token 授权联系人时,Sanctum 正在尝试查看 Users personal_access_tokens 表,而不是 Contacts personal_access_tokens 表。所以本质上它只是查看个人_access_tokens 表的错误数据库,我不知道如何更改它。
我的设置如下:
卫兵:

'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],

/*'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],*/

'users' => [
'driver' => 'sanctum',
'provider' => 'users',
'hash' => false,
],

'contacts' => [
'driver' => 'sanctum',
'provider' => 'contacts',
'hash' => false,
],
],
供应商
   'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],

'contacts' => [
'driver' => 'eloquent',
'model' => App\Models\Contact::class,
],
],
用户模型
namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'email',
'password',
];

/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
联系方式
namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class Contact extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;

/**
* The connection name for the model.
*
* @var string
*/
protected $connection = 'puranet_crm';

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'first_name',
'last_name',
'email',
'password',
];

/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
我的两个 api 路由是:
Route::group(['middleware' => 'auth:sanctum'], function() {
//All secure URL's
Route::get('test',[UserController::class, 'test']);
});

Route::group(['middleware' => 'auth:contacts'], function() {
Route::get('test-contacts',[ContactController::class, 'test']);
});
联系 Controller (这与 UserController 相同,但它引用的模型除外)
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Contact;
use Illuminate\Support\Facades\Hash;

class ContactController extends Controller
{
/**
* @param Request $request
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
*/
public function login(Request $request)
{

$user = Contact::where('email', $request->email)->first();

if (!$user || !Hash::check($request->password, $user->password)) {
return response([
'message' => ['These credentials do not match our records.']
], 404);
}

$token = $user->createToken('contacts-app-token')->plainTextToken;

$response = [
'user' => $user,
'token' => $token
];

return response($response, 201);
}

/**
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
*/
public function test()
{
return response(["response" => "Test Contacts"], 201);
}
}

最佳答案

您需要覆盖项目中的 sanctum 模型并覆盖其中的 $connection 变量,这样您就可以连接到您想要连接的数据库,就像使用普通模型一样。您可以找到如何覆盖版本 8 的 Laravel 文档中的 sanctum 模型。
在您的项目之一中创建此模型以覆盖 sanctum 将在何处查找 token 。

class PersonalAccessToken extends SanctumPersonalAccessToken{

use HasFactory;

protected $connection = 'name of your connection in database.php';
}
因此,两个 sanctum 都将使用相同的数据库来验证用户。
我希望我能帮助你:)

关于php - Laravel Sanctum token - 两个数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65141198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com