Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

asenator's avatar

Laravel 8 Guard Login Issue

Hello, I want to implement multi-authentication in my Laravel application, which includes both admins and regular users. Below are my current codes. Admin.php Model


namespace App\Models;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\Authenticatable as AuthenticableTrait;
use Illuminate\Database\Eloquent\Model;

class Admin extends Model implements Authenticatable
{
    use AuthenticableTrait;

    protected $fillable = [
        'name',
        'email',
        'password',
        'phone',
    ];
}```

`AdminController.php Controller`
```<?php

namespace App\Http\Controllers;

use App\Http\Requests\AdminRegisterRequest;
use App\Models\Admin;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class AdminController extends Controller
{

    use AuthenticatesUsers;

    public function __construct()
    {
        $this->middleware('guest:admin');
        $this->middleware('guest:web');
    }
    

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('auth.admin.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(AdminRegisterRequest $request)
    {

        $admin = $this->createAdmin($request->all());

        $this->guard()->login($admin);

        $credential = $request->only(['email', 'password']);

        if (Auth::guard('admin')->attempt($credential, $request->filled('remember')))
            return redirect()->route('admin.dashboard');
    }

    private function createAdmin(array $data)
    {
        return Admin::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);
    }

    public function dashboard()
    {
        return view('auth.admin.dashboard');
    }

//    public function guard()
//    {
//        return Auth::guard('admin');
//    }
}

config/auth.php


return [


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



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



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


    ],



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

    'password_timeout' => 10800,

];

The issue I'm facing is that after registering in the Admins table, the user is not automatically logged in. I've tested two methods for logging in, and I'll send the codes for them below.

$admin = $this->createAdmin($request->all());

$this->guard()->login($admin);

$credential = $request->only(['email', 'password']);

if (Auth::guard('admin')->attempt($credential, $request->filled('remember')))
				return redirect()->route('admin.dashboard');

and

 $admin = $this->createAdmin($request->all());

$credential = $request->only(['email', 'password']);

if (Auth::guard('admin')->attempt($credential, $request->filled('remember')))
return redirect()->route('admin.dashboard');
0 likes
4 replies
AilonMast's avatar

Mr. Parsafar, you come every 6 months, you get caught talking to us to collect followers through us. No, you have an itch at the bottom of your body.

gych's avatar

@asenator Hey, its better to use roles instead of a separate table for admins. Just add admins and regular users to the same users table and set which role the user has. e.g. admin or user

1 like
asenator's avatar

@gych Thank you for your response, dear friend. I considered this solution to implement and learn more about multi-authentication in Laravel. In practice, the issue you mentioned is absolutely correct. In the normal state, users can be categorized into three groups: role, users, and group users.

Snapey's avatar

I dont know why you would ever want to do this. Different guards should be used for different login methods, not for establishing authorization.

And why are you adding new stuff to an out of date and unsupported version instead of focussing on upgrades.

Please or to participate in this conversation.