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?
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?
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).
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?
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 :)
Laravel Passport is an OAuth 2.0 server implementation for API authentication using Laravel. Since tokens are generally used in API authentication, Laravel Passport provides an easy and secure way to implement token authorization on an OAuth 2.0 server.
In your API create route as
Route::post('register','Api\UsersController@create');
And in UsersController create method create()
function create(Request $request) { /** * Get a validator for an incoming registration request. * * @param array $request * @return \Illuminate\Contracts\Validation\Validator */ $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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With