vikasarul's avatar

Authentication Issue while Using differnt table instead of users table

I am working on a Laravel project where I have created a login system using the built-in authentication feature. I am storing user details in a table called "detailsofuser" in a MySQL database. The table has the following columns: "MemberID", "FullName", "email", "mobile", and "MPassWord".

I have used bcrypt to hash the password while storing it in the database. In my login form, I am using the "auth()->attempt" method to authenticate the user.

However, the authentication process always fails, even though I am sure that the credentials are correct. I have tried various solutions such as checking the password hash manually, checking the user details using the Eloquent model, and modifying the authentication middleware, but none of them have worked.

It is worth noting that I have not used migrations to create the "detailsofuser" table, but have created it manually in the database.

I would appreciate any help in solving this issue.

0 likes
5 replies
LaryAI's avatar
Level 58

To use a different table for authentication in Laravel, you need to modify the authentication configuration file. By default, Laravel uses the "users" table for authentication. To use a different table, follow these steps:

  1. Open the "config/auth.php" file.
  2. In the "providers" array, add a new provider for your custom table. For example:
'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\Models\User::class,
    ],

    'details' => [
        'driver' => 'eloquent',
        'model' => App\Models\DetailsOfUser::class,
    ],
],
  1. In the "guards" array, modify the default guard to use your custom provider. For example:
'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'details',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'details',
        'hash' => false,
    ],
],
  1. In your login controller, modify the "attempt" method to use the correct credentials and guard. For example:
if (Auth::guard('details')->attempt(['email' => $email, 'password' => $password])) {
    // Authentication passed...
}

Make sure that your "DetailsOfUser" model implements the "Illuminate\Contracts\Auth\Authenticatable" interface and that the "MPassWord" column is named "password" in the model.

vikasarul's avatar

@LaryAI "<?php

return [

'defaults' => [
    'guard' => 'web',
    'passwords' => 'users',
],



'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'detailsofusers',
    ],
],

'api' => [
    'driver' => 'token',
    'provider' => 'detailsofusers',
    'hash' => false,
],



'providers' => [

    'users' => [
        'driver' => 'eloquent',
        'model' => App\Models\User::class,
    ],




    // 'users' => [
    //     'driver' => 'database',
    //     'table' => 'users',
    // ],

    'detailsofusers' => [
        'driver' => 'eloquent',
        'model' => App\Models\detailsofuser::class,
    ],
],



'passwords' => [
    'users' => [
        'provider' => 'users',
        'table' => 'password_resets',
        'expire' => 60,
        'throttle' => 60,
    ],
],





//////// new code end 

'password_timeout' => 10800,

]; " My table name is 'detailsofuser' . This is my auth.php file code anything wrong with it ?

vikasarul's avatar

Tried to last attempted credentials "dd(Auth::guard()->getLastAttempted());". It returns null.

vikasarul's avatar

@Snapey <?php

namespace App\Models;

//use Illuminate\Auth\Authenticatable; use Illuminate\Contracts\Auth\Authenticatable;

use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\Access\Authorizable; use Illuminate\Foundation\Auth\User as AuthenticatableUser; use Illuminate\Notifications\Notifiable;

class detailsofuser extends Model implements Authenticatable { use HasFactory;

protected $table = 'detailsofuser';
protected $primaryKey = 'id';

/* protected $fillable = [ 'MemberID', 'FullName', 'email', 'mobile',

];   */

protected $fillable = ['*'];


public $timestamp = false;

protected $hidden = [
    'MPassWord',
    'remember_token',
];

public function getAuthIdentifierName()
{
    return 'id';
}

public function getAuthIdentifier()
{
    return $this->getKey();
}

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

public function getRememberToken()
{
    return $this->remember_token;
}

public function setRememberToken($value)
{
    $this->remember_token = $value;
}

public function getRememberTokenName()
{
    return 'remember_token';
}

}

Please or to participate in this conversation.