gpt4 book ai didi

php - 使用用户名而不是电子邮件的 JWTAuth 身份验证

转载 作者:可可西里 更新时间:2023-10-31 23:17:26 26 4
gpt4 key购买 nike

有没有什么办法可以在laravel中使用jwt-auth使用用户名(或任何自定义列)而不是电子邮件进行身份验证?

这是现有的代码

public function authenticate(Request $request)
{
$credentials = $request->only('email', 'password');

try {
// verify the credentials and create a token for the user
if (! $token = JWTAuth::attempt($credentials)) {
return response()->json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
// something went wrong
return response()->json(['error' => 'could_not_create_token'], 500);
}

// if no errors are encountered we can return a JWT
return response()->json(compact('token'));
}

我想做这样的事情

$credentials = $request->only('username', 'password');

一种方法是将用户名放在电子邮件列中,但这是一个糟糕的解决方案。

最佳答案

我找到了一个方法 here

我们可以从模型(基于自定义字段)中获取用户,例如

//抓取一些用户

$user = User::first();

并手动验证它。

$token = JWTAuth::fromUser($user);

关于php - 使用用户名而不是电子邮件的 JWTAuth 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36449202/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com