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'.
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.
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:
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.
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!
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);
}
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...
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With