<bdo id='3hwPf'></bdo><ul id='3hwPf'></ul>

    1. <legend id='3hwPf'><style id='3hwPf'><dir id='3hwPf'><q id='3hwPf'></q></dir></style></legend>
      <i id='3hwPf'><tr id='3hwPf'><dt id='3hwPf'><q id='3hwPf'><span id='3hwPf'><b id='3hwPf'><form id='3hwPf'><ins id='3hwPf'></ins><ul id='3hwPf'></ul><sub id='3hwPf'></sub></form><legend id='3hwPf'></legend><bdo id='3hwPf'><pre id='3hwPf'><center id='3hwPf'></center></pre></bdo></b><th id='3hwPf'></th></span></q></dt></tr></i><div id='3hwPf'><tfoot id='3hwPf'></tfoot><dl id='3hwPf'><fieldset id='3hwPf'></fieldset></dl></div>
    2. <tfoot id='3hwPf'></tfoot>
      1. <small id='3hwPf'></small><noframes id='3hwPf'>

        多用户模型 Laravel JWT Auth

        Multiple User Models Laravel JWT Auth(多用户模型 Laravel JWT Auth)
        <legend id='IrsWj'><style id='IrsWj'><dir id='IrsWj'><q id='IrsWj'></q></dir></style></legend>

          <small id='IrsWj'></small><noframes id='IrsWj'>

          <i id='IrsWj'><tr id='IrsWj'><dt id='IrsWj'><q id='IrsWj'><span id='IrsWj'><b id='IrsWj'><form id='IrsWj'><ins id='IrsWj'></ins><ul id='IrsWj'></ul><sub id='IrsWj'></sub></form><legend id='IrsWj'></legend><bdo id='IrsWj'><pre id='IrsWj'><center id='IrsWj'></center></pre></bdo></b><th id='IrsWj'></th></span></q></dt></tr></i><div id='IrsWj'><tfoot id='IrsWj'></tfoot><dl id='IrsWj'><fieldset id='IrsWj'></fieldset></dl></div>
            <tbody id='IrsWj'></tbody>
                <bdo id='IrsWj'></bdo><ul id='IrsWj'></ul>
                <tfoot id='IrsWj'></tfoot>
                • 本文介绍了多用户模型 Laravel JWT Auth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我必须在我的 eloquent 中使用模型:

                  I have to user models in my eloquent:

                  • 用户
                  • 办公用户

                  OfficeUser 在 JWT 配置中定义为标准模型.现在我已经编写了一个中间件来验证它们中的每一个

                  OfficeUser is in defined in the JWT config as standard model. Now I have written a Middleware for authenticate each of them

                  授权用户:

                  public function handle($request, Closure $next)
                  {
                      Config::set('auth.providers.users.model', AppUser::class);
                      try {
                  
                          if (! $user = JWTAuth::parseToken()->authenticate()) {
                              return response()->json(['user_not_found'], 404);
                          }
                  
                      } catch (TymonJWTAuthExceptionsTokenExpiredException $e) {
                  
                          return response()->json(['token_expired'], $e->getStatusCode());
                  
                      } catch (TymonJWTAuthExceptionsTokenInvalidException $e) {
                  
                          return response()->json(['token_invalid'], $e->getStatusCode());
                  
                      } catch (TymonJWTAuthExceptionsJWTException $e) {
                  
                          return response()->json(['token_absent'], $e->getStatusCode());
                  
                      }
                  
                      return $next($request);
                  }
                  

                  authOffice用户

                  authOfficeUser

                  public function handle($request, Closure $next)
                  {
                      try {
                  
                          if (! $user = JWTAuth::parseToken()->authenticate()) {
                              return response()->json(['user_not_found'], 404);
                          }
                  
                      } catch (TymonJWTAuthExceptionsTokenExpiredException $e) {
                  
                          return response()->json(['token_expired'], $e->getStatusCode());
                  
                      } catch (TymonJWTAuthExceptionsTokenInvalidException $e) {
                  
                          return response()->json(['token_invalid'], $e->getStatusCode());
                  
                      } catch (TymonJWTAuthExceptionsJWTException $e) {
                  
                          return response()->json(['token_absent'], $e->getStatusCode());
                  
                      }
                  
                      return $next($request);
                  }
                  

                  另外我为他们每个人都有一个登录功能:

                  Additionally I have a login function for each of them:

                  登录用户

                  if ($user){
                          if (Hash::check($request->password, $user->password)) {
                              // grab credentials from the request
                              $credentials = $request->only('email', 'password');
                  
                              try {
                                  // attempt to verify the credentials and create a token for the user
                                  Config::set('auth.providers.users.model', AppUser::class);
                                  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);
                              }
                  

                  登录办公室用户

                  if ($user){
                          if (Hash::check($request->password, $user->password)) {
                              // grab credentials from the request
                              $credentials = $request->only('email', 'password');
                  
                              try {
                                  // attempt to verify the credentials and create a token for the user
                                  Config::set('auth.providers.users.model', AppOfficeUser::class);
                                  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);
                              }
                  

                  不幸的是,当我登录并尝试调用 authUser 中间件后面的路由时,我得到一个user_not_found"

                  Unfortunately when I login and try to call a route behind the authUser Middleware I get an "user_not_found"

                  有人知道为什么会这样吗?OfficeUser 身份验证工作正常

                  Does anybody have an idea why this happens? OfficeUser authentication works fine

                  推荐答案

                  发帖给任何发现此问题的人

                  虽然不建议有两个用户表,但我有一个类似的要求,即与我们的一个客户设置 JWT.这就是我解决问题的方法.

                  Posting for anyone who finds this questions

                  Although it's not recommended to have two user tables, but I had a similar requirement of setting up JWT with one of our clients. This is how I solved the issue.

                  无需对 `config/auth.php' 中的提供者进行任何更改

                  'providers' => [
                      'users' => [
                          'driver' => 'eloquent',
                             'model' => AppUser::class,
                       ],
                  
                  ]
                  

                  在您的身份验证控制器中,通过设置动态修改提供者使用的模型

                  In your authentication controller, dynamically modify the model used by the providers by setting

                  Config::set('auth.providers.users.model', AppTrainer::class);

                  示例代码

                  在 authenticate() 方法中

                  if ($credentials['user_type'] == 'consultant') {
                  
                  Config::set('auth.providers.users.model', AppTrainer::class);
                  
                  } else {
                      Config::set('auth.providers.users.model', AppUser::class);
                  }
                  
                  //Find the user
                  
                  //Create the token
                  if ($user) {
                     $customClaims = ['user_type' => $credentials['user_type']];
                     $token = JWTAuth::fromUser($user,$customClaims);
                  } else {
                    return response()->json(['error' => 'invalid_credentials'], 401);
                  }
                  

                  在解析令牌以验证用户身份时,您也必须这样做.示例代码

                  You will have to do the same while parsing the token to authenticate the user as well. Example code

                  在 getAuthenticatedUser() 方法中

                  $payload = JWTAuth::parseToken()->getPayload();
                  $user_type = $payload->get('user_type');
                  
                  if($user_type === 'consultant'){
                     Config::set('auth.providers.users.model', AppTrainer::class);
                  }else{
                     Config::set('auth.providers.users.model', AppUser::class);
                  }
                  
                  if (!$user = JWTAuth::parseToken()->authenticate()) {
                      return response()->json(['user_not_found'], 404);
                  }
                  

                  这篇关于多用户模型 Laravel JWT Auth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                • <legend id='uM0iF'><style id='uM0iF'><dir id='uM0iF'><q id='uM0iF'></q></dir></style></legend>
                  1. <small id='uM0iF'></small><noframes id='uM0iF'>

                      <tbody id='uM0iF'></tbody>
                    <tfoot id='uM0iF'></tfoot>
                      <bdo id='uM0iF'></bdo><ul id='uM0iF'></ul>
                    • <i id='uM0iF'><tr id='uM0iF'><dt id='uM0iF'><q id='uM0iF'><span id='uM0iF'><b id='uM0iF'><form id='uM0iF'><ins id='uM0iF'></ins><ul id='uM0iF'></ul><sub id='uM0iF'></sub></form><legend id='uM0iF'></legend><bdo id='uM0iF'><pre id='uM0iF'><center id='uM0iF'></center></pre></bdo></b><th id='uM0iF'></th></span></q></dt></tr></i><div id='uM0iF'><tfoot id='uM0iF'></tfoot><dl id='uM0iF'><fieldset id='uM0iF'></fieldset></dl></div>