1

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

1
  • What IssueTokensController@login really do? Commented Jun 19, 2018 at 16:31

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

for some reason I'm getting an error saying: Call to undefined method App\\User::createToken(). My User model has use HasApiTokens, Notifiable; traits but still not working. Any idea? Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.