简体   繁体   English

Laravel sanctum 找错用户

[英]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调试

  1. I've debugged my app line by line from login to stored token in storage all showing correct token etc.我已经逐行调试了我的应用程序,从loginstored token都显示正确的令牌等。
  2. I've debugged my 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 返回错误的用户数据吗?

Update更新

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM