使用JWT Laravel 5进行身份验证,无需密码 [英] Authentication with JWT Laravel 5 without password

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

问题描述

我正在尝试学习Laravel,我的目标是能够构建RESTful API(不使用视图或刀片,仅使用JSON结果.后来,AngularJS Web应用程序和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 App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;


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进行身份验证,无需密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆