TypeError: Illuminate\Auth\SessionGuard::login(): Argument #1 ($user) must be of type Illuminate\Contracts\Auth\Authenticatable,, App\Models\Admin giv

Viewed 5629

I am a beginner in laravel .I am using guard for multi authorization. I am having this issue in my admin login: TypeError Illuminate\Auth\SessionGuard::login(): Argument #1 ($user) must be of type Illuminate\Contracts\Auth\Authenticatable, App\Models\Admin given, called in C:\Users\DELL\survey\app\Http\Controllers\AdminController.php on line 34

This is the AdminController.php file

<?php

namespace App\Http\Controllers;
use App\Models\Admin;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Contracts\Auth\Authenticatable;

class AdminController extends Controller
{
    public function login()
    {
        return view('admin.login');
    }
    public function authenticate(Request $request)
    {
        $request->validate([
            'email' => ['required', 'email'],
            'password' => ['required']
        ]);

        /** @var Admin $model */
        $model = Admin::query()->where('email', $request->get('email'))->firstOrFail();

        if(!$model){
            return back()->with('error', 'Email or password is incorrect');
        }

        if (!Hash::check($request->get('password'), $model->password)) {
            return back()->with('error', 'Email or password is incorrect');
        }

        Auth::guard('admin')->login($model);

        return redirect()->route('dashboard')
            ->with('success', 'Welcome ' . $model->name . '!');
    }
}

This is the login.blade.php

<x-guest-layout>
    <x-auth-card>
        <x-slot name="logo">
            <a href="/">
                <x-application-logo class="w-20 h-20 fill-current text-gray-500" />
            </a>
        </x-slot>

        <!-- Session Status -->
        <x-auth-session-status class="mb-4" :status="session('status')" />

        <!-- Validation Errors -->
        <x-auth-validation-errors class="mb-4" :errors="$errors" />
@include('partials.session-messages')
        <form method="POST" action="{{ route('admin-authenticate') }}">
            @csrf
<h3>Admin Login</h3><br>
            <!-- Email Address -->
            <div>
                <x-label for="email" :value="__('Email')" />

                <x-input id="email" class="block mt-1 w-full" type="email" name="email" :value="old('email')" required autofocus />
            </div>

            <!-- Password -->
            <div class="mt-4">
                <x-label for="password" :value="__('Password')" />

                <x-input id="password" class="block mt-1 w-full"
                                type="password"
                                name="password"
                                required autocomplete="current-password" />
            </div>

            <!-- Remember Me -->
            <div class="block mt-4">
                <label for="remember_me" class="inline-flex items-center">
                    <input id="remember_me" type="checkbox" class="rounded border-gray-300 text-indigo-600 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50" name="remember">
                    <span class="ml-2 text-sm text-gray-600">{{ __('Remember me') }}</span>
                </label>
            </div>

            <div class="flex items-center justify-end mt-4">
                @if (Route::has('password.request'))
                    <a class="underline text-sm text-gray-600 hover:text-gray-900" href="{{ route('password.request') }}">
                        {{ __('Forgot your password?') }}
                    </a>
                @endif

                <x-button class="ml-3">
                    {{ __('Log in') }}
                </x-button>
            </div>
        </form>
    </x-auth-card>
</x-guest-layout>

How do I fix this?

2 Answers

You Admin model should be extends with authenticatable!

use Illuminate\Foundation\Auth\User as Authenticatable;   
class Admin extends Authenticatable
{
   //Your code
}

Since you are not returning the model just created, on code:

 /** @var Admin $model */
    $model = Admin::query()->where('email', $request->get('email'))->firstOrFail();


    if(!$model){
        return back()->with('error', 'Email or password is incorrect');
    }

it expects like,

return Admin::query()->where('email', $request->get('email'))->firstOrFail();

so make sure that, when you modify or return, return expected model as well.

In my case, when I use jetstream auth, I wanted to check whether user created or not, so I stored user variable, and show some messages, but forgot to return the created user model. So, this may help someone. Make sure that you return expected Model while creating new one.

Related