我正在尝试学习Laravel,我的目标是能够构建一个RESTful API(不使用视图或刀片,只有JSON结果)。之后,一个AngularJS web应用和一个Cordova混合移动应用将会使用这个api。
经过一番研究,我倾向于选择JWT-Auth库,以获得完全无状态的好处。我的问题是:我有两种主要类型的用户:客户和版主。客户不需要密码。我需要能够生成一个令牌与提供的电子邮件访问仅。如果该电子邮件存在于数据库中并且属于客户,则它将生成并返回令牌。如果它存在并且属于版主,它将返回false,以便接口可以请求密码。如果电子邮件不存在,则抛出无效参数错误。
我读了这里的文档,它说可以使用自定义声明。但是文档没有解释什么是声明,以及作为自定义声明传递的数组意味着什么。我需要一些关于如何实现我上面解释的东西的建议。
<?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'));
}
try with this:
$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'));
对我有用,希望能有所帮助
为客户生成令牌(不需要密码)可以通过
实现$user = 'App'Modules'User'Models'UserModel::whereEmail('xyz@gmail.com')->first();
$userToken=JWTAuth::fromUser($user);
$userToken
将电子邮件存在检查后的令牌存储在UserModel文件中配置的表中。
我假设您将客户和版主存储在同一个表中,必须有一些标志来区分它们。假设标志为user_type
$token = null;
$user = 'App'Modules'User'Models'UserModel::whereEmail('xyz@gmail.com')->first();
if($user['user_type'] == 'customer'){
$credentials = $request->only('email');
$token =JWTAuth::fromUser($user);
}else if($user['user_type'] == 'moderator'){
$credentials = $request->only('email','password');
$token = JWTAuth::attempt($credentials);
}else{
//No such user exists
}
return $token;
就自定义声明而言,这些是自定义的有效载荷,可以附加到令牌字符串。
例如,JWTAuth::attempt($credentials,['role'=>1]);
将尝试向令牌负载添加角色对象。一旦您通过JWT Facade JWTAuth::parseToken()->getPayload();
解码令牌字符串,您将依次获得config/JWT .php下required_claims中定义的所有有效负载,以及额外的角色有效负载。
引用https://github.com/tymondesigns/jwt-auth/wiki/Creating-Tokens creating-a-token-based-on-anything-you-like如果您还需要什么,请告诉我。
您可以为这两种用户类型添加令牌身份验证,而不是为客户和版主制定不同的登录策略。这将使您的生活更轻松,并为可伸缩性做好准备。在您的api中,您可以通过发送
来限制版主用户不能访问api。<?php
Response::json('error'=>'method not allowed')
除了这个建议,我相信@Alimnjan代码应该可以工作。
如果您还没有App'User
对象,请使用
$user = App'User::find(1);
使用JWTAuth
的fromUser()
方法生成令牌
$token = 'JWTAuth::fromUser($user)
上面的代码不验证用户,它只生成一个JWT令牌。如果需要对用户进行身份验证,则必须添加如下内容
'JWTAuth::setToken($token)->toUser();