<bdo id='5ciTm'></bdo><ul id='5ciTm'></ul>
    1. <small id='5ciTm'></small><noframes id='5ciTm'>

      <tfoot id='5ciTm'></tfoot>

    2. <legend id='5ciTm'><style id='5ciTm'><dir id='5ciTm'><q id='5ciTm'></q></dir></style></legend>
    3. <i id='5ciTm'><tr id='5ciTm'><dt id='5ciTm'><q id='5ciTm'><span id='5ciTm'><b id='5ciTm'><form id='5ciTm'><ins id='5ciTm'></ins><ul id='5ciTm'></ul><sub id='5ciTm'></sub></form><legend id='5ciTm'></legend><bdo id='5ciTm'><pre id='5ciTm'><center id='5ciTm'></center></pre></bdo></b><th id='5ciTm'></th></span></q></dt></tr></i><div id='5ciTm'><tfoot id='5ciTm'></tfoot><dl id='5ciTm'><fieldset id='5ciTm'></fieldset></dl></div>
    4. 使用 JWT Laravel 5 进行身份验证,无需密码

      Authentication with JWT Laravel 5 without password(使用 JWT Laravel 5 进行身份验证,无需密码)
    5. <i id='y5862'><tr id='y5862'><dt id='y5862'><q id='y5862'><span id='y5862'><b id='y5862'><form id='y5862'><ins id='y5862'></ins><ul id='y5862'></ul><sub id='y5862'></sub></form><legend id='y5862'></legend><bdo id='y5862'><pre id='y5862'><center id='y5862'></center></pre></bdo></b><th id='y5862'></th></span></q></dt></tr></i><div id='y5862'><tfoot id='y5862'></tfoot><dl id='y5862'><fieldset id='y5862'></fieldset></dl></div>
        <tbody id='y5862'></tbody>

          <tfoot id='y5862'></tfoot>

              <bdo id='y5862'></bdo><ul id='y5862'></ul>
            • <legend id='y5862'><style id='y5862'><dir id='y5862'><q id='y5862'></q></dir></style></legend>

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

                本文介绍了使用 JWT Laravel 5 进行身份验证,无需密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试学习 Laravel,我的目标是能够构建一个 RESTful API(不使用视图或刀片,只有 JSON 结果.稍后,AngularJS 网络应用程序和 Cordova 混合移动应用程序将使用此 API.

                I'm trying to learn Laravel and my goal is to be able to build a RESTful API (no use of views or blade, only JSON results. Later, an AngularJS web app and a Cordova hybrid mobile app will consume this api.

                经过一些研究,我倾向于选择 JWT-Auth 库以获得完全无状态的好处.我的问题是:我有两种主要类型的用户:客户版主.客户不需要密码.我需要能够生成一个令牌,以便仅使用提供的电子邮件进行访问.如果该电子邮件存在于数据库中并且属于客户,它将生成并返回令牌.如果它存在并且属于版主,它将返回 false 以便接口可以请求密码.如果电子邮件不存在,则会引发无效参数错误.

                After some research, I'm inclining to choose JWT-Auth library for completely stateless benefit. My problem is: I have 2 main types of users: customers and moderators. Customers are not required to have a password. I need to be able to generate a token for access with the provided email only. If that email exists in the database and it belongs to a customer, it will generate and return the token. If it exists and belongs to a moderator, it will return false so the interface can request a password. If the email doesn't exist, it throws an invalid parameter error.

                我在 这里阅读了文档,它说可以使用自定义声明.但是文档没有解释什么是声明以及作为自定义声明传递的数组的含义.我想要一些关于如何实现我上面解释的内容的意见.

                I read the docs here and it says it's possible to use Custom Claims. But the docs doesn't explain what are claims and what it means the array being passed as custom claims. I'd like some input on how to go about achieving what I explain above.

                    <?php
                
                namespace AppHttpControllers;
                
                use IlluminateHttpRequest;
                
                use AppHttpRequests;
                use AppHttpControllersController;
                use JWTAuth;
                use TymonJWTAuthExceptionsJWTException;
                
                
                class AuthenticateController extends Controller
                {
                
                    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'));
                    }
                }
                

                谢谢.

                赏金代码

                public function authenticate(Request $request) { 
                    $email = $request->input('email');
                    $user = User::where('email', '=', $email)->first();
                    try { 
                        // verify the credentials and create a token for the user
                        if (! $token = JWTAuth::fromUser($user)) { 
                            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')); 
                }
                

                推荐答案

                试试这个:

                $user=User::where('email','=','user2@gmail.com')->first();
                
                if (!$userToken=JWTAuth::fromUser($user)) {
                            return response()->json(['error' => 'invalid_credentials'], 401);
                        }
                
                return response()->json(compact('userToken'));
                

                对我有用,希望能帮到你

                it works for me, hope can help

                这篇关于使用 JWT Laravel 5 进行身份验证,无需密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                PHP Upload File Validation(PHP 上传文件验证)
                PHP Error - Uploading a file(PHP 错误 - 上传文件)
                How can I write tests for file upload in PHP?(如何在 PHP 中编写文件上传测试?)
                php resizing image on upload rotates the image when i don#39;t want it to(php在上传时调整图像大小会在我不想要它时旋转图像)
                How to send additional data using PLupload?(如何使用 PLupload 发送附加数据?)
                change button text in js/ajax after mp4 =gt;mp3 conversion in php(在 php 中的 mp4 =gt;mp3 转换后更改 js/ajax 中的按钮文本)
              • <legend id='yLzSc'><style id='yLzSc'><dir id='yLzSc'><q id='yLzSc'></q></dir></style></legend>

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

                      <tbody id='yLzSc'></tbody>

                      • <bdo id='yLzSc'></bdo><ul id='yLzSc'></ul>

                        <tfoot id='yLzSc'></tfoot>

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