Pranav Mandlik
Pranav Mandlik

Reputation: 644

auth:admin not working

i have created admin login system but on auth::attempt its always returning false here is my code

$credentials= ['email' => $request->get('email'), 'password'=>$request->get('password')]; 

   if (Auth::guard('admin')->attempt($credentials)) {
          return redirect()->intended(route('admin.dashboard'));
   }

in Admin model

    <?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;

class Admin extends Authenticatable
{
    protected $guard = 'admin';
    protected $primary_key = 'admin_id';
    protected $table = 'admins';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'admin_firstname', 'admin_lastname', 'email', 'admin_username', 'admin_phone', 'admin_picture', 'admin_gender', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
   }

in auth.php

'guards' => [
        'admin' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],

auth::attempt is failing dont know why its not working

its logging but problem is found in admin middleware $this->middleware('auth:admin');

my auth.php

 'guards' => [
    'admin' => [
        'driver' => 'session',
        'provider' => 'admins',
    ],
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
    ],

],


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

    'admins' => [
        'driver' => 'eloquent',
        'model' => App\Models\Admin::class,
    ],
],

degault guard is set as web but in dashboard controller i am using auth:admin please check

Upvotes: 2

Views: 1199

Answers (1)

Rahul
Rahul

Reputation: 1615

your code looking like good.you need to make sure.did you use bcrypt() in registration.like this

$password=bcrypt($request['password']);

and you need to change in config/auth.php your guard should be like this

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

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],
        'admin' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],
        'admin-api' => [
            'driver' => 'token',
            'provider' => 'admins',
        ],
    ], 

and provider should be

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
        'admins' => [
            'driver' => 'eloquent',
            'model' => App\Admin::class,
        ],


    ],

and password

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

this is demonstration change your model and driver accordingly

Upvotes: 1

Related Questions