[英]Laravel sanctum getting wrong user
I have logged user with my app and when I'm sending user token to get data from server its returning data of the wrong user.我已经用我的应用程序记录了用户,当我发送用户令牌以从服务器获取数据时,它返回了错误用户的数据。
Debug调试
login
to stored token
in storage all showing correct token etc.login
到stored token
都显示正确的令牌等。API login function
line by line and it gets correct user and assign the token to it.API login function
并获得了正确的用户并将令牌分配给它。 Code代码
public function index(Request $request)
{
$user = $request->user();
Log::info($user); // wrong user will be printed in log file
$will = Will::where('user_id', $user->id)->with(['documents', 'videos', 'user'])->first();
return response()->json([
'data' => new WillResource($will),
'message' => 'Data is ready.'
], 200);
}
Route路线
Route::group(['middleware' => 'auth:sanctum', 'prefix' => 'userdata'], function() {
Route::get('mywill', 'Api\WillController@index');
});
Any idea why sanctum returning wrong user data?知道为什么 sanctum 返回错误的用户数据吗?
Here is when I try it by postman这是我通过 postman 尝试的时候
Login with user postman@test.com
使用用户
postman@test.com
登录
Receive user info of admin@admin.com
接收
admin@admin.com
的用户信息
$token = $user->createToken($request->deviceName)->plainTextToken;
$user['token'] = $token;
use approach like this create a associated token when user login /signup then add new key in $user['token'] and then return the response trough resource使用这样的方法在用户登录/注册时创建一个关联的令牌,然后在 $user['token'] 中添加新密钥,然后返回响应槽资源
Solved解决了
The problem was that I am using uuid
for my users table and personal_access_tokens
table is morphable with id
so I changed it to uuidMorphs
and now it's working问题是我将
uuid
用于我的 users 表,而personal_access_tokens
表可以使用id
进行变形,所以我将其更改为uuidMorphs
现在它正在工作
Schema::create('personal_access_tokens', function (Blueprint $table) {
$table->bigIncrements('id');
$table->uuidMorphs('tokenable'); // <--- here
$table->string('name');
$table->string('token', 64)->unique();
$table->text('abilities')->nullable();
$table->timestamp('last_used_at')->nullable();
$table->timestamps();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.