Patricia Rozario
Patricia Rozario

Reputation: 151

Laravel Passport Authenticate User To Access API Data

I am trying to implement Resource Owner Password Credentials Grant in my laravel 5.6 / passport application. I have set all the basic configurations. I want the user to be able to pass only their username and password and have the authentication server pass in the grant type, client_secret, and client_id to the passport route http://dev.api.com/oauth/token

This is my routes file:

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Route::get('/login', 'IssueTokensController@login')->name('get.token');

I want the IssueTokensController to prepopulate client_id, client_secret from an env file and redirect to the passport route. Any advice would be helpful

Upvotes: 1

Views: 825

Answers (1)

rkj
rkj

Reputation: 8287

You don't need to redirect to http://dev.api.com/oauth/token. You can just add this code in your custom login (IssueTokensController@login) and then generate personal access token.

public function login(Request $request)
{
        $credentials = $request->only('username', 'password');

        if (Auth::attempt($credentials)) {
            // Authentication passed...
             $user = Auth::user();
             $token = $user->createToken('Token Name')->accessToken;

            return response()->json($token);
        }
}

Check the doc for personal access token.

https://laravel.com/docs/5.6/passport#personal-access-tokens

Upvotes: 2

Related Questions