laravel passport的createToken方法未给出哈希令牌。它从数据库表中返回此对象。
{
"name": "Laravel Password Grant Client",
"abilities": [
"*"
],
"tokenable_id": 10,
"tokenable_type": "App\\Models\\User",
"updated_at": "2021-09-02T15:58:03.000000Z",
"created_at": "2021-09-02T15:58:03.000000Z",
"id": 7
}.
我需要用于验证的令牌。
我运行了composer require laravel/passport
php artisan migrate
php artisan passport:install
使用HasApiTokens、HasFactory和Notifiable; 在AuthServiceProvider的boot方法中, 添加Passport::routes(); 在config/auth.php文件中进行配置
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
我的注册控制器函数
$validator = Validator::make($request->all(), [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|confirmed|string|min:6',
]);
if ($validator->fails()) {
return response(['errors' => $validator->errors()->all()], 422);
}
$request['password'] = Hash::make($request['password']);
$request['remember_token'] = Str::random(10);
$user = User::create($request->toArray());
$token = $user->createToken('Laravel Password Grant Client')->accessToken;
return response(["ok" => true, "message" => "You have successfully registered", "user" => $user, "token" => $token], 200);