lundi 9 octobre 2023

Laravel 10 Auth Login not working with custom Users table and column names

Info

I have custom name an columns for my Users table and I'm using MSSQL for database. My table is called 'TUsers' and for columns I'm using PascalCase so I have:

  • 'Email' instead of 'email'
  • 'Password' instead of 'password'
  • And all others like created_at, updated_at and remember_token

Registering works fine but there the user is automatically logged in with Auth::login()

The problem

Trying to login returns error "These credentials do not match our records." I have also tried to make a custom LoginController with my own function and I get the same issue using Auth::attempt

  • Tried using bcrypt() as well

What I've tried

I've added this to my User model

public $table = "TUsers";
protected $primaryKey = 'TUserID';

const EMAIL = 'Email';
const PASSWROD = 'Password';
const CREATED_AT = 'CreatedAt';
const UPDATED_AT = 'UpdatedAt';

public function getAuthPassword()
{
    return $this->Password;
}

protected $fillable = [
    'Name',
    'Email',
    'Password',
    'EmailVerifiedAt'
];

protected $hidden = [
    'Password',
    'RememberToken',
];

protected $casts = [
    'EmailVerifiedAt' => 'datetime',
    'Password' => 'hashed',
];

I've also made the following adjustments to auth.php

    'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
        'credentials' => [
            'email' => 'Email',
            'password' => 'Password'
        ]
    ],
],
    'passwords' => [
    'users' => [
        'provider' => 'users',
        'table' => 'PasswordResets',
        'expire' => 60,
        'throttle' => 60,
    ],
],

I've also tried mkaing a custom TUserProvider

<?php
namespace App\Providers;

use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\Facades\DB;

class TUsersProvider implements UserProvider
{
    public function retrieveById($identifier)
    {
        return DB::table('TUsers')->where('TUserID', $identifier)->first();
    }

    public function retrieveByToken($identifier, $token)
    {
        return DB::table('TUsers')->where('TUserID', $identifier)->where('RememberToken', $token)->first();
    }

    public function retrieveByCredentials(array $credentials)
    {
        // Retrieve a user by their credentials ('Email' and validate the 'Password')
        $user = DB::table('TUsers')->where('Email', $credentials['Email'])->first();

        if ($user && password_verify($credentials['Password'], $user->Password)) {
            return $user;
        }

        return null;
    }

    public function validateCredentials(Authenticatable $user, array $credentials)
    {
        return password_verify($credentials['Password'], $user->getAuthPassword());
    }

    public function updateRememberToken(Authenticatable $user, $token)
    {
        DB::table('TUsers')->where('TUserID', $user->getAuthIdentifier())->update([
            'RememberToken' => $token,
        ]);
    }
}

And added the following to AuthServiceProvider

   $this->app['auth']->provider('tusers', function ($app, array $config) {
        return new TUsersProvider($app['hash'], $config['model']);
    });

And updated in auth.php the 'provider' parameter under guards.web to 'tusers'.

But then I get the error:

Illuminate\Auth\SessionGuard::__construct(): Argument #2 ($provider) must be of type Illuminate\Contracts\Auth\UserProvider, null given, called in /var/www/html/clients/markj/iatse-new/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php on line 127



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire