【问题标题】:Laravel sanctum getting the right userLaravel sanctum 获得合适的用户
【发布时间】:2021-07-29 08:41:05
【问题描述】:

我正在尝试使用 Laravel 和 Sanctum 为具有我无法修改的数据库的现有系统构建 API,但我遇到了身份验证问题。

系统有一个不太常见的用户表,其中一个用户可以有相同的 id 但有 3 种不同的“类型”。因此,在登录时,我会发送电子邮件、密码和类型。没问题,成功登录。

但是当试图恢复登录用户时,它显示了错误的用户。示例:

Login:
(this user has id 8)
email: dummy@gmail.com
password: xxxxx
type: 2
Recovered user:
(this user also has id 8)
email: dummy@gmail.com
passsword: xxxxx
type: 1

我认为是因为他们的 ID 相同,而 Laravel 占用了第一个用户,这不一定正确。

我被这个困住了……我该怎么办?有没有办法获得合适的用户? 谢谢

登录功能

public function login(Request $request)
{
    $validator = Validator::make($request->all(), [
        'email' => 'required|email',
        'password' => 'required',
        'tip_id' => 'required|int'
    ]);

    if (!Auth::attempt($validator->validated())) {
        return response()->json(['error' => 'Email e/ou senha incorretos'], 400);
    } else {
        $user = User::where('email', $request->input('email'))->where('tip_id', $request->input('tip_id'))->first();

        $item = time().rand(0, 9999);
        $token = $user->createToken($item)->plainTextToken;

        return response()->json(['token' => $token]);
    }
}

用户档案功能

public function profile()
{
    return response()->json(['user' => auth()->user()]);
}

【问题讨论】:

    标签: php laravel authentication laravel-sanctum


    【解决方案1】:

    我在Reddit 上问了同样的问题,我得出了预期的结论,即如果不更改数据库,就无法解决这个问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-08
      • 2020-11-06
      • 1970-01-01
      • 2021-09-29
      • 2020-11-12
      • 2022-10-20
      • 2021-07-04
      • 2021-08-05
      相关资源
      最近更新 更多