[英]Laravel /w Ionic JWT authentication
So I am developing Ionic app with Laravel back-end and using JWT authentication.所以我正在使用 Laravel 后端并使用 JWT 身份验证开发 Ionic 应用程序。
My question is...since im using 4 fields when registering a user, and only 2 when logging in (email and pass), I suppose that upon registration the token should be made of only those 2 fields...我的问题是...因为我在注册用户时使用了 4 个字段,而在登录时只使用了 2 个字段(电子邮件和密码),我想在注册时,令牌应该只由这两个字段组成...
This is the working sign up function:这是工作注册功能:
public function signUp()
{
$credentials = Input::all();
if (User::whereEmail($credentials['email'])->first()) {
return Response::json([
'error' => 'User with given e-mail already exists',
], 409);
} elseif (User::wherePhone($credentials['phone'])->first()) {
return Response::json([
'error' => 'User with given phone number already exists',
], 409);
} else {
$user = User::create($credentials);
$token = JWTAuth::fromUser($user);
return Response::json(compact('token'));
}
}
However if I change $credentials = Input::only('email', 'password')
the full user won't be created (since there are fields missing).但是,如果我更改
$credentials = Input::only('email', 'password')
将不会创建完整的用户(因为缺少字段)。
But even if I leave $credentials
as-is, and make combinations like $token = JWTAuth::fromUser(Input::only('email', 'password'))
or parse e-mail and password to JSON, or something similar...I get a "Trying to get a property of non-object" error, or that array is given instead of an object to JWTAuth...但即使我保留
$credentials
原样,并进行组合,如$token = JWTAuth::fromUser(Input::only('email', 'password'))
或将电子邮件和密码解析为 JSON,或类似的东西...我收到“试图获取非对象的属性”错误,或者该数组被提供而不是 JWTAuth 的对象...
JWTAuth::fromUser(Input::only('email', 'password'))
expects a User object. JWTAuth::fromUser(Input::only('email', 'password'))
需要一个 User 对象。
If you wish to use credentials you can do something like this:如果您想使用凭据,您可以执行以下操作:
// grab credentials from the request
$credentials = Input::only('email', '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'));
https://github.com/tymondesigns/jwt-auth/wiki/Creating-Tokens https://github.com/tymondesigns/jwt-auth/wiki/Creating-Tokens
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.