Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Laravel Passport with a custom username column

Now I'm using something like that for authenticating the user on my base site:

if (Auth::attempt($request->only(['id', 'password']))) {             // } 

How can I modify this code for using custom column as username? https://laravel.com/docs/5.3/passport#password-grant-tokens

like image 376
tehcpu Avatar asked Aug 28 '16 19:08

tehcpu


People also ask

Which is better JWT or Passport in Laravel?

The "tymondesigns/jwt-auth" is a PHP Laravel implementation of the JWT protocol. On the other hand, Passport also uses JWT by default plus a huge extra, a complete Oauth2 implementation. Regarding the functionality, as I said they both use JWT thus you can use whichever you like to authentication via tokens.

Can I use Laravel Passport with Lumen?

Lumen, as we already know is a micro-framework by Laravel well suited for developing micro-services and APIs. To fix this, Denis Mysenko helped with a workaround by developing 'lumen-passport', a simple provider that makes Laravel passport work with Lumen.

Does Laravel Passport use JWT?

Passport uses JWT authentication as standard but also implements full OAuth 2.0 authorization.

How to get user info in Laravel 9 passport authentication?

Laravel 9 Get User Info. API: You need to set this access token as a Bearer Token in the Authorization header. In this tutorial we have learn about the Laravel 9 User Registration Login Api with Passport Authentication and its application with practical example.

Does Laravel use tokens for authentication?

Since APIs are generally stateless and do not use sessions, we generally use tokens to keep state between requests. Laravel uses the Passport library to implement a full OAuth2 server we can use for authentication in our API. With the above installed, we’re ready to get started.

Should I use Laravel sanctum or Laravel OAuth2?

However, if you are attempting to authenticate a single-page application, mobile application, or issue API tokens, you should use Laravel Sanctum. Laravel Sanctum does not support OAuth2; however, it provides a much simpler API authentication development experience.

How does passport use the username of an authenticatable model?

When authenticating using the password grant, Passport will use the email attribute of your authenticatable model as the "username". However, you may customize this behavior by defining a findForPassport method on your model:


1 Answers

You can use findForPassport method in your user model.

This method take username as argument, and return user model

For example:

class User extends Authenticatable {     use HasApiTokens, Notifiable;      // ... some code      public function findForPassport($username) {         return $this->where('id', $username)->first();     } } 
like image 91
Alexander Avatar answered Sep 23 '22 13:09

Alexander