Laravel 令牌生成
Laravel token generation
我正在尝试使用护照在 laravel 中创建一个用户令牌,当我尝试调用该服务时,它会给我令牌的数据,而不是令牌的数据。
使用用户模型中的护照:
<?php
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;
protected $fillable = [
'name',
'email',
'password',
];
protected $hidden = [
'password',
'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
}
以及用于登录的控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Validator;
class UserController extends Controller
{
public $successStatus = 200;
public function login()
{
if (Auth::attempt(['email' => request('email'), 'password' => request('password')])) {
$user = Auth::user();
$success['token'] = $user->createToken('MyApp')-> accessToken;
return response()->json(['success' => $success], $this-> successStatus);
} else {
return response()->json(['error'=>'Unauthorised'], 401);
}
}
}
服务的响应是:
{
"success": {
"token": {
"name": "MyApp",
"abilities": [
"*"
],
"tokenable_id": 1,
"tokenable_type": "App\Models\User",
"updated_at": "2022-03-29T22:52:58.000000Z",
"created_at": "2022-03-29T22:52:58.000000Z",
"id": 2
}
}
}
我想得到这样的回应
使用:
'use Laravel\Passport\HasApiTokens'
而不是
'use Laravel\Sanctum\HasApiTokens'
我正在尝试使用护照在 laravel 中创建一个用户令牌,当我尝试调用该服务时,它会给我令牌的数据,而不是令牌的数据。
使用用户模型中的护照:
<?php
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;
protected $fillable = [
'name',
'email',
'password',
];
protected $hidden = [
'password',
'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
}
以及用于登录的控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Validator;
class UserController extends Controller
{
public $successStatus = 200;
public function login()
{
if (Auth::attempt(['email' => request('email'), 'password' => request('password')])) {
$user = Auth::user();
$success['token'] = $user->createToken('MyApp')-> accessToken;
return response()->json(['success' => $success], $this-> successStatus);
} else {
return response()->json(['error'=>'Unauthorised'], 401);
}
}
}
服务的响应是:
{
"success": {
"token": {
"name": "MyApp",
"abilities": [
"*"
],
"tokenable_id": 1,
"tokenable_type": "App\Models\User",
"updated_at": "2022-03-29T22:52:58.000000Z",
"created_at": "2022-03-29T22:52:58.000000Z",
"id": 2
}
}
}
我想得到这样的回应
使用:
'use Laravel\Passport\HasApiTokens'
而不是
'use Laravel\Sanctum\HasApiTokens'