0
votes

I am developing a REST API using Laravel. I am implementing authentication feature to my API using Passport. I am following this tutorial- https://scotch.io/@neo/getting-started-with-laravel-passport. I installed and configured successfully. The only issue I am having now is that when I request token from this endpoint "oauth/token", the API is always asking for username and password. Please, see my scenario below.

I registered a user in the database like this.

$user = [
      'name' => 'waiyanhein',
      'email' => 'iljimae.ic@gmail.com',
      'password' => bcrypt('testing')
   ];
   App\User::create($user);

The user is registered and a record is created in the database. Using the client id and secret which I got during the installation of password and user credentials, I made a request to generate the access token as in the tutorial. But I made the request from the REST Client. My request is as in the screenshot below. enter image description here

As you can see, I provided the valid user credentials and client id and secret. When I made the request, the REST client tool prompt a pop up box requesting for the user credentials again as in the screenshot below.

enter image description here

When I enter the credentials again, it keeps popping up the dialog box even if the credentials are valid. Instead of using username, I tried using email as well. Not matter how I tried, it is just asking for the user credentials. Why and what is wrong with the request? How can I correctly pass the credentials in the request?

1
Try sending the encrypted password, bcrypt('testing').Brian Lee
I solved such problem by setting the primary key of the model to name. e.g. protected $primaryKey = "name"; I hope this could help you.Miron
Yes. That helped. But since "name" becomes PK, I did not like the solution. So, I am no longer using this Passport. :(. Thanks so much for the help.Wai Yan Hein

1 Answers

0
votes

Laravel passport assumes that you have username column in the users table, since you don't have this columns but name instead, you need to tell passport about it.

1st method using Accessors:

public function getUsernameAttribute(){
   return $this->attributes['name'];
 }

2nd method using findForPassport:

public function findForPassport($username) {
   return $this->where('name', $username)->first();
}