Register a new user from api route using laravel passport

I installed laravel 5.3 and a passport for folders . I followed the documentation step by step. I can use the following POST /oauth/token route with the following parameters

  • Username
  • password
  • client_secret
  • grant_type
  • client_id

and I get the following answer

  { "token_type": "Bearer", "expires_in": 31536000, "access_token": "access token here", "refresh_token": "refresh token here" } 

then I ask GET /api/user

with the following heading

  • Authorization = "Subscriber access icon here"
  • accept = application / json (optional)

and this works fine, so all apis.

the problem is I have a user that I authenticated and entered his username and password in the first request and returned the access token that I created from laravel web view / register

How can I create a new user or register a new user from the api route file

like POST /api/register

the user must register for the first time to verify after that.

Should I create this route without oauth for registration, then if the registration is successful, it requests POST /oauth/token for verification or what? Did I miss something?

Updating clent_secret Is it correct to be constant in all user requests, or should each user have a different clent_secret, and if this is how to create a secret secret, if it is not identified for user authentication?

+6
source share
2 answers

The quickest way to do this is to add an exception to the verifyCsrfToken.php class

 protected $except = [ 'register' ]; 

Then you can send a message to your register model and after that access this account using oauth / token.

0
source

If I understand your question correctly, you want to register a new user and get a token after registering with /oauth/token . You can use proxies for this. I used something like this and I followed the steps below. This works for both login and registration.

  • Register the user and send the HTTP request to the endpoint /oauth/token from your registration method

     public function register(RegisterRequest $request) { $user = User::create($request->all()); $response = $this->authenticationProxy->attemptLogin($request->email, $request->password); return response()->json(compact('response', 'user'), 201); } 
  • Then in the proxy class call /oauth/token

     public function attemptLogin($email, $password) { $http = new GuzzleHttp\Client; $response = $http->post('http://your-app.com/oauth/token', [ 'form_params' => [ 'grant_type' => 'password', 'client_id' => env('CLIENT_ID'), 'client_secret' => env('CLIENT_SECRET'), 'username' => $email, 'password' => $password, 'scope' => '', ], ]); return json_decode((string) $response->getBody(), true); } 

Hope this helps. You can also use a similar approach for login.

0
source

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


All Articles