Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel passport custom password column

How can I use Laravel's Passport package to authenticate a different password column.

If i want to authenticate from a different 'username' column, it can be done with the following code:

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

It will take Id, as the column. What if I want to use a different 'password' column. A column in the table with a different name such as 'uid_token'.

like image 444
JGCW Avatar asked Nov 15 '16 09:11

JGCW


People also ask

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.

How to create a password reset table in Laravel 8?

first of all we need to get fresh Laravel 8 version application using bellow command, So open your terminal OR command prompt and run bellow command: basically, it will already created "password_resets" table migration but if it's not created then you can create new migration as like bellow code:

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.

Can I override the migrations that Laravel passport uses?

Looking through the Laravel Passport docs, it appears that you can publish the migrations that it uses. This means that we can override them and update the migrations to use a UUID field instead of an incremental one. So first we will publish the Laravel Passport migrations: Great!


3 Answers

Adding this validateForPassportPasswordGrant method to User model did the job for me ("PasswMd" - custom column name):

public function validateForPassportPasswordGrant($password)
{
    return Hash::check($password, $this->PasswMd);
}
like image 181
ego Avatar answered Sep 28 '22 05:09

ego


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...

like image 21
CharlieTap Avatar answered Sep 28 '22 06:09

CharlieTap


While the above solutions are great but there is another way to achieve it and it worked for me in Laravel 8.

For future readers I provide the code down here, they need to add to their models and return the custom password column like so.

    public function getAuthPassword()
    {
        return $this->PasswMd;
    }
like image 23
Ismail Avatar answered Sep 28 '22 04:09

Ismail