简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM