How to Use Laravel Passport with a Custom Username Column

How to use Laravel Passport with a custom username column

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();
}
}

Laravel Passport: create token with custom username and password

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

laravel passport custom password column

There's a method the Passport/Bridge asks for called validateForPassportPasswordGrant($password) that you can override in your user model, if you don't override this it will look for a password column in your user table. I'm not entirely sure why they haven't configured it to use Authenticatable method getAuthPassword...

Laravel Authentication change column name

As answered here:

class User extends Authenticatable
{
use HasApiTokens, Notifiable;

// ... some code

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

Laravel Passport: Custom column name SQL error

Derek's comment is correct, just set protected $primaryKey = 'user_id'. If you're interested in the nuts and bolts of it though, read on:

That issue is actually not specific to Passport, it has more to do with the Auth Guard behavior, which is abstracted from Passport.

Laravel's out-of-the-box App\User Model extends Illuminate\Foundation\Auth\User, which uses the Illuminate\Auth\Authenticatable trait. That trait is just a convenience for satisfying the conditions of the Illuminate\Contracts\Auth\Authenticatable interface, one of which is to have basically a getter for whatever your username and password fields are.

This trait defaults the username field to be the same as that tables primary key. But it doesn't have to be, if you were to override the getAuthIdentifierName() in your own Model class and make it return a field of your choosing.

Laravel Passeport change user email authentication to username

In your User model, you can add this following method to login by username instead of email :

/**
* Find the user instance for the given username.
*
* @param string $username
* @return \App\User
*/
public function findForPassport($username)
{
return $this->where('username', $username)->first();
}

https://laravel.com/docs/7.x/passport#customizing-the-username-field

Laravel Passport: login using only username without password

use findForPassport method in your user model.

it take username as argument, and return user model

Already answered here

class User extends Authenticatable
{
use Notifiable, HasApiTokens;

// Set as username any column from users table
public function findForPassport($username)
{
$customUsername = 'phone';
return $this->where($customUsername, $username)->first();
}
// Owerride password here
public function validateForPassportPasswordGrant($password)
{
$owerridedPassword = 'password';
return Hash::check($password, $this->password);
}
}

Laravel Passport: Use custom password

That issue is actually not specific to Passport, it has more to do with the Auth Guard behavior, which is abstracted from Passport. Simply add this to your user Model:

/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return $this->user_password;
}

Laravel's out-of-the-box App\User Model extends Illuminate\Foundation\Auth\User, which uses the Illuminate\Auth\Authenticatable trait. That trait is just a convenience for satisfying the conditions of the Illuminate\Contracts\Auth\Authenticatable interface, one of which is to have basically a getter for whatever your password field is named. You'd be overriding this.

The word 'password' in the context of grant-types is an entirely different thing by the way, and not something you should have to change like that. That has more to do with a configuration reference, and not a database field.

Customize Laravel 8 Passport methods

To customize you need add the validateForPassportPasswordGrant() at User method, example:

public function validateForPassportPasswordGrant($password)
{
$password = strtoupper(md5($request->password));
$system = env("CODE_SYSTEM", 12);

$sql = "SELECT user.validate( '$system' , '$this->username', '$password')";

$stmt = DB::select(DB::raw($sql));
$result = $stmt[0]->validation;

if ($result == "ok") {
return true;
}

return false;
}

If you need to change the user column name where the passport search username:

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


Related Topics



Leave a reply



Submit