User registration with Laravel passport

I have set up a password (this is the backend for the application). Now I can send a request for mail in oauth/token , and it works on the Postman. But what if I also want to register a user from the API?

I understand that I can use the current /register route, however, then I will need to redirect the user back to the login page, and he will again be logged in with his credentials?

Or in RegisterController, in the registered() function, should I redirect to the oauth/token route? (For this, please note that I am sending all 5 data to 'x-www-form-urlencoded', and this seems to work. However, do I need to separate some data in the headers? It is blurry for me, so just I wanted to ask when I have the opportunity).

Or should I add something to the oauth/token method, for example, this guy ? In fact, I was trying to catch the published $request data about the AccessTokenController@issueToken method inside the library, however I could not figure out how to manipulate the parsedBody array. If I activate my registration function from a real library, how do I know if it has registered or entered?

Maybe I'm missing some information, but I could not find anything on this topic. How to handle user registration in the passport?


Update: The accepted answer shows the "register" cycle; and under it I added implementations of 'login' and 'refresh token'. Hope this helps :)

+15
source share
3 answers

In your API, create a route as

 Route::post('register','Api\ UsersController@create '); 

And in the UserController create create() method

 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 a new user, enter the access token.

+14
source

And a year later, I realized how to implement the full cycle.

The @Nileshsinh method shows a register loop.

And here is the login and update of the token parts:

 Route::post('auth/token', 'Api\ AuthController@authenticate '); Route::post('auth/refresh', 'Api\ AuthController@refreshToken '); 

Methods:

 class AuthController extends Controller { private $client; /** * DefaultController constructor. */ public function __construct() { $this->client = DB::table('oauth_clients')->where('id', 1)->first(); } /** * @param Request $request * @return mixed */ protected function authenticate(Request $request) { $request->request->add([ 'grant_type' => 'password', 'username' => $request->email, 'password' => $request->password, 'client_id' => $this->client->id, 'client_secret' => $this->client->secret, 'scope' => '' ]); $proxy = Request::create( 'oauth/token', 'POST' ); return \Route::dispatch($proxy); } /** * @param Request $request * @return mixed */ protected function refreshToken(Request $request) { $request->request->add([ 'grant_type' => 'refresh_token', 'refresh_token' => $request->refresh_token, 'client_id' => $this->client->id, 'client_secret' => $this->client->secret, 'scope' => '' ]); $proxy = Request::create( 'oauth/token', 'POST' ); return \Route::dispatch($proxy); } } 
+7
source

Reading this while Laravel 6 was recently deployed, my solution for this is as follows.

If you follow the steps specified in the LaRavel passport documentation and added the HasApiTokens to the User model, you can call the createToken function for your user entities.

In addition, your RegisterController has a registered function from the RegistersUsers trait that you can implement, which is called when the user is successfully registered. So you can implement this as follows:

 protected function registered(Request $request, User $user) { $token = $user->createToken('tokenName'); return response()->json([ 'user' => $user, 'token' => $token->accessToken, ]); } 

See the register function in the characteristic RegistersUsers for more information about the registration cycle.

0
source

Source: https://habr.com/ru/post/1268202/


All Articles