【问题标题】:JWTAuth authentication with username instead of email使用用户名而不是电子邮件进行 JWTAuth 身份验证
【发布时间】:2016-07-26 17:48:58
【问题描述】:

有没有办法在 laravel jwt-auth 中使用 username(或任何自定义列)而不是 email 进行身份验证?

这是现有的代码

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');

一种方法是将 usernames 放在 email 列中,但这是一个不好的解决方案。

【问题讨论】:

    标签: php api laravel authentication


    【解决方案1】:
    //Use case for login with email or username    
    public function login(Request $request){
                // Request input contains username, email and password
                if ($request->has(['username', 'email', 'password'])) {
                    $credentials = $request->only('email', 'password');
                } // Request input contains `enter code here`username and password
                elseif ($request->has(['username', 'password'])) {
                    $credentials = $request->only('username', 'password');
                } // Request input contains email and password
                elseif ($request->has(['email', 'password'])) {
                    $credentials = $request->only('email', 'password');
                }
                else {
                    $credentials = $request->only('email', 'password');
                }
    
                try {
                   if (!$token = JWTAuth::attempt($credentials)) {
                    return response()->json(['invalid_email_or_password']);
                   }
                } catch (JWTAuthException $e) {
                    return response()->json(['failed_to_create_token'], 500);
                }
                return response()->json(compact('token'));
            }
    

    【讨论】:

    • 请为您的代码添加解释,因为仅包含代码的帖子并不能真正回答问题(并且对这个问题的其他读者没有多大帮助)。
    【解决方案2】:

    我找到了方法here

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

    // 抓取一些用户

    $user = User::first();
    

    并手动进行身份验证。

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

    【讨论】:

      【解决方案3】:

      Looking around,看起来可以通过像 $credentials = $request->only('username', 'password'); 这样设置凭据来做到这一点。

      所以它看起来像这样:

      public function authenticate(Request $request)
          {
              // grab credentials from the request
              //$credentials = $request->only('email', 'password');
              // Change email to username
              $credentials = $request->only('username', 'password');
      
              try {
                  // attempt to 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 whilst attempting to encode the token
                  return response()->json(['error' => 'could_not_create_token'], 500);
              }
      
              // all good so return the token
              return response()->json(compact('token'));
          }
      

      【讨论】:

      • 这对在 Laravel 8 中使用有用吗?
      • @FernandoTorres 抱歉,我不明白你的问题? $request->only() 在 Laravel 8 中仍然有效...
      • 我在问你,如果这个解决方案在 Laravel 版本 8.x 中使用最新版本的 jwt 有效
      • @FernandoTorres 我不知道。我不积极使用这个包。你可以试试看。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-11
      • 1970-01-01
      • 2010-10-16
      • 2015-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多