0

I am attempting to make custom auth roles for a site, i have followed another stack overflow post that seems to get it working but mine always fails, i know the information exists since im dumping and dying the information to screen once Auth::guard fails. The problem is that even though the information exists auth guard fails

if (Auth::guard('Admin')->attempt(['Username' => $Request->input('UName'), 'Password' => $Request->input('Password')])) 

The information is validated and approved and manages to pass

 if (Admins::where('Username', $Request->input('UName'))->exists()) { $AdminDetails = Admins::Where('Username', $Request->input('UName'))->first(); if(Hash::check($Request->input('Password'), $AdminDetails->Password)){ //user details are correct } } 

I have made the authenticatable for both the user and the admin the code above attempts to authorize the admin, i have also changed the config/auth.php file as shown below

'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', 'hash' => false, ], 'Admin' => [ 'driver' => 'session', 'provider' => 'Admin', ], 'User' => [ 'driver' => 'session', 'provider' => 'User', ], ], 
'providers' => [ 'User' => [ 'driver' => 'eloquent', 'model' => App\Models\User::class, ], 'Admin' => [ 'driver' => 'eloquent', 'model' => App\Models\Admins::class, ] // 'users' => [ // 'driver' => 'database', // 'table' => 'users', // ], ], 

The models are within the correct namespace as shown below with the admin authenticatable

<?php namespace App\Models; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class Admins extends Authenticatable { use Notifiable; protected $guard = 'Admin'; protected $fillable = [ 'FName', 'LName', 'Email', 'Password', 'Username' ]; protected $hidden = [ 'password' ]; } 

and the user authenticatable

<?php namespace App\Models; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class Users extends Authenticatable { use Notifiable; protected $guard = 'User'; protected $fillable = [ 'FullName', 'Email', 'CompanyName','ReferedBy','PhoneNumber','MemberStatus','CustomMessage', ]; protected $hidden = [ 'password', 'remember_token', ]; } 

Laravel 7 displays no errors or anything but the if stament that attempts to log the user in fails

Also the migration for the Admin Model is

<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateAdminsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('admins', function (Blueprint $table) { $table->id(); $table->string('Username'); $table->string('FName'); $table->string('LName'); $table->string('Email'); $table->string('Password'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('admins'); } } 

I am aware this is a lot of information to put in a single question but i know that its all relevant to the problem. Thanks for any help provided.

0

1 Answer 1

2

When passing an array of credentials to Auth::attempt the element that holds the password MUST be named password, it has no relation to any field name on the table. This is how the User Provider knows which field is supposed to be the "password" since it needs to do a hash check; every other field except password in the credentials are WHERE conditions of a query.

['Username' => ..., 'password' => ...] 

If your Model/table is using a different field for the password besides password you will have to tell the model this by defining the getAuthPassword method:

public function getAuthPassword() { return $this->Password; } 
Sign up to request clarification or add additional context in comments.

4 Comments

The model is using Password instead of password so if i change the p to lower case the problem will be fixed?
if you change the field of the table to password and you adjust the credentials array, then you will not have to define the getAuthPassword method yourself as it will be using the default which is the password field
Thanks is this common in Laravel or is this an exception. Either way i will start using lower case letters, thanks for your help.
you can name the fields what you want, but if that particular field isn't password you have to let the auth system know this as by default getAuthPassword returns the value of the password field ... but yes its common to have fields with lowercase

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.