Registering User with Laravel Passport(使用 Laravel Passport 注册用户)
问题描述
我设置了密码授权(它是应用程序的后端).现在,我可以向 oauth/token
发送一个 post 请求,它可以在 Postman 上工作.但是,如果我也想从 api 注册用户怎么办?
I set up password grant (it's backend for an app). Now, I can send a post request to oauth/token
and it works on Postman. However, what if I want to register user from the api too?
我知道我可以使用当前的 /register
路由,但是,那么我是否需要将用户重定向回登录页面并让他使用他的凭据再次登录?
I understand I can use current /register
route, however, then will I need to redirect the user back to the login page and he logs in again with his credentials?
或者在 RegisterController 中,在 registered()
函数中,我应该重定向到 oauth/token
路由吗?(为此,请注意我正在发送 'x-www-form-urlencoded' 中的所有 5 个数据,它似乎有效.但是,我需要在标题中分离一些数据吗?对我来说很模糊,所以只是想要问我什么时候有机会).
Or in the RegisterController, in registered()
function, should I do I redirect to the oauth/token
route? (For this, please note that I am sending, all the 5 data in 'x-www-form-urlencoded' and it seems to work. However, do I need to separate some in headers? It's blurry for me, so just wanted to ask when I have the chance).
或者我应该在 oauth/token
方法中添加一些东西,比如 这个人?实际上,我试图在库内的 AccessTokenController@issueToken
方法上捕获发布的 $request
数据,但是我不知道如何操作 parsedBody代码>数组.如果我从实际库中触发我的注册功能,我怎么知道它是注册还是登录?
Or should I add something in the oauth/token
method like this guy? Actually, I tried to catch the posted $request
data on AccessTokenController@issueToken
method inside library, however I couldn't figure out how to manipulate the parsedBody
array. If I trigger my register function from the actual library, how would I know if it's register or login?
也许我遗漏了一些信息,但我找不到基于此主题的任何信息.在 Passport 中处理注册用户的正确方法是什么?
Maybe I am missing out some information, but I couldn't find anything based on this topic. What is the proper way of handling registering user in Passport?
更新:接受的答案显示注册"周期;在它下面我添加了登录"和刷新令牌"实现.希望它有帮助:)
Update: Accepted answer shows the 'register' cycle; and below it I have added 'login' and 'refresh token' implementations. Hope it helps :)
推荐答案
In your API create route as
In your API create route as
Route::post('register','ApiUsersController@create');
并在 UsersController 中创建方法 create()
And in UsersController create method create()
function create(Request $request)
{
/**
* Get a validator for an incoming registration request.
*
* @param array $request
* @return IlluminateContractsValidationValidator
*/
$valid = validator($request->only('email', 'name', 'password','mobile'), [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6',
'mobile' => 'required',
]);
if ($valid->fails()) {
$jsonError=response()->json($valid->errors()->all(), 400);
return Response::json($jsonError);
}
$data = request()->only('email','name','password','mobile');
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'mobile' => $data['mobile']
]);
// And created user until here.
$client = Client::where('password_client', 1)->first();
// Is this $request the same request? I mean Request $request? Then wouldn't it mess the other $request stuff? Also how did you pass it on the $request in $proxy? Wouldn't Request::create() just create a new thing?
$request->request->add([
'grant_type' => 'password',
'client_id' => $client->id,
'client_secret' => $client->secret,
'username' => $data['email'],
'password' => $data['password'],
'scope' => null,
]);
// Fire off the internal request.
$token = Request::create(
'oauth/token',
'POST'
);
return Route::dispatch($token);
}
创建新用户后,返回访问令牌.
And after creating new user, return access token.
这篇关于使用 Laravel Passport 注册用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 Laravel Passport 注册用户
基础教程推荐
- PHP 守护进程/worker 环境 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 使用 PDO 转义列名 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01