amit
amit

Reputation: 373

Laravel Passport: create token with custom username and password

In my application, I am username as email or phone number. so username can be email or phone number. But laravel passport accepts username as email. And I want to generate password grant type token with email and password or phone_no and password. so required values to generate token will be

  1. email/password
  2. phone_no/password How can I generate token with phone_no as username?

Upvotes: 1

Views: 490

Answers (1)

abofazl rasoli
abofazl rasoli

Reputation: 116

Use findForPassport method in user model . Like below

class User  extends Authenticatable {

        use HasApiTokens;
    
        public function findForPassport($username) {
            return self::firstWhere('email', $username) ?? self::firstWhere('phone_no',  $username);
        }

 }

Also you can read this document https://laravel.com/docs/8.x/passport#customizing-the-username-field

Upvotes: 2

Related Questions