web guard allows login but admin guard does not allow login

248 views Asked by At

I define a new guard "Admin" to have a multi Auth System User and admin in my project . web guard allows login.But admin guard does not allow login when I try to login into Admin ,it gives

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'last_sign_in_at' in 'field list' (SQL: update `admins` set `updated_at` = 2020-09-27 12:49:24, `last_sign_in_at` = 2020-09-27 12:49:24, `current_sign_in_at` = 2020-09-27 12:49:24 where `id` = 1)

My users table

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('user_type');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->boolean('status')->default(0);
        $table->timestamp('last_sign_in_at')->nullable();
        $table->timestamp('current_sign_in_at')->nullable();
        $table->string('user_click');
        $table->timestamp('user_click_time')->nullable();
        $table->rememberToken();
        $table->timestamps();
    });
}

My admin table

  public function up()
    {
        Schema::create('admins', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('user_type');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->boolean('status');
            $table->rememberToken();
            $table->timestamps();

             });
    }

auth.php

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

        //admin guard
        'admin' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],

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





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

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

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



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

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

My Middleware CheckRole

  public function handle($request, Closure $next)
    {
        
        if (!Auth::guard('admin')->check()){
            return redirect('admin/login');
        }
        return $next($request);
        
    }

My Admin.php Model

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
//guard
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
//guard End

class Admin extends Authenticatable
{

    use Notifiable;
    protected $guard ='admin';
    protected $hidden = [
        'password', 'remember_token',
    ];
    protected $guarded=[];

    protected $casts = [
        'email_verified_at' => 'datetime',
    ];


}

My AdminController

   public function adminLogin(Request $request){
        if ($request->ismethod('post')) {
            $data = $request->input();

            if ( Auth::guard('admin')->attempt(['email' => $data['email'], 'password' => $data['password'],
                'user_type'=>'admin', 'status' => '1'])){
                return view('admin.dashboard');
            }
            else {

                return back()->with('error',' Invalid UserName Or Password');
            }

        }

    }

When I tried to login into Admin, It gives error. Any solution ps !

1

There are 1 answers

3
lagbox On

It seems like you have an event listener listening for Auth's LoginEvent and it is setting the last_sign_in_at field on the Model and saving it. Since you are using different models for Authentication it will end up trying to do this on what ever Model is in that event; in this case the Admin model.

You will need to add this field to your admin table, or you will have to check in the listener which Model the event is holding and decide whether to update this field depending on what type that model is.