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

MoSalem's avatar

Multi Auth from 2 different tables

Hello I use bootstrap /ui for authentication

I have 2 tables for 2 different user roles Admins table Users table

Each roles has different route

user can reach main page using login form in main page route "/login" Route::get('/' , [HomeController::class,'index'])->name('home') ;

@extends('layouts.app')

@section('content')

            <div class="card-body">
                <form method="POST" action="{{ route('login') }}">
                    @csrf

                    <div class="row mb-3">
                        <label for="email" class="col-md-4 col-form-label text-md-end">{{ __('Email Address') }}</label>

                        <div class="col-md-6">
                            <input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus>

                            @error('email')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                            @enderror
                        </div>
                    </div>



                    <div class="row mb-3">
                        <label for="password" class="col-md-4 col-form-label text-md-end">{{ __('Password') }}</label>

                        <div class="col-md-6">
                            <input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="current-password">

                            @error('password')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                            @enderror
                        </div>
                    </div>

                    <div class="row mb-3">
                        <div class="col-md-6 offset-md-4">
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}>

                                <label class="form-check-label" for="remember">
                                    {{ __('Remember Me') }}
                                </label>
                            </div>
                        </div>
                    </div>

                    <div class="row mb-0">
                        <div class="col-md-8 offset-md-4">
                            <button type="submit" class="btn btn-primary">
                                {{ __('Login') }}
                            </button>

                            @if (Route::has('password.request'))
                                <a class="btn btn-link" href="{{ route('password.request') }}">
                                    {{ __('Forgot Your Password?') }}
                                </a>
                            @endif
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

Also , Admin can reach cpanel home page using login form in admin route "/admin/cpanel/login"

Route::get('admin/cpanel/home', [CpanelController::class, 'index'])->name('admin.cpanel.home')->middleware('auth:admin');

@extends('back.admin.cpanel.sections.admins.layouts.app')

@section('content')

            <div class="card-body">
                <form method="POST" action="{{ route('admin.cpanel.checklogin') }}">
                    @csrf
                    @include('back.inc.message')

                    <div class="row mb-3">
                        <label for="email" class="col-md-4 col-form-label text-md-end">{{ __('Email Address') }}</label>

                        <div class="col-md-6">
                            <input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus>

                            @error('email')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                            @enderror
                        </div>
                    </div>

                    <div class="row mb-3">
                        <label for="password" class="col-md-4 col-form-label text-md-end">{{ __('Password') }}</label>

                        <div class="col-md-6">
                            <input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="current-password">

                            @error('password')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                            @enderror
                        </div>
                    </div>

                    <div class="row mb-0">
                        <div class="col-md-8 offset-md-4">
                            <button type="submit" class="btn btn-primary">
                                {{ __('Login') }}
                            </button>

                            @if (Route::has('password.request'))
                                <a class="btn btn-link" href="{{ route('password.request') }}">
                                    {{ __('Forgot Your Password?') }}
                                </a>
                            @endif
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

I want to make main login form Variable, mean if user use main login to login it redirect user to main page, else if admin use main login form from main page it redirect admin to cpanel home

How can do it? Is it need to create new middleware? If so, who can do it?

0 likes
7 replies
Tray2's avatar

You are mixing up authentication and authorization here, you should only use one kind of authentication and use the users table for that, then for authorization you should use roles, and the role should determine what the user is allowed to do. In short terms, a user is a user, regardless if it it a regular user, a manage, and admin, or any other kind of user.

Start here and watch the videos on authentication and authorization

https://laracasts.com/series/30-days-to-learn-laravel-11/episodes/20

MoSalem's avatar

@Tray2 you mean i need to use 1 user tables instead of 2 tables , and it contains field like " roles" ? then i make authorize based on this field ?

Tray2's avatar
Tray2
Best Answer
Level 73

@MoSalem A single users table yes, and I would suggest a roles table, and a role_user table instead of adding an is_admin field to the users table, that way you are separating authentication and authorization concerns in your database. This also future profs your application, and makes it easier to add other roles in the future.

1 like
MoSalem's avatar

@martinbean Super

It is clear and provides just what I am searching for. I tried your explanation on a tiny project and it worked well. I will apply it to the main project and let you know if I face any difficulties.

1 like
MoSalem's avatar

@martinbean Hello, I included a function in the logincontroller to direct users or admins to the appropriate home page based on their role.

public function login (Request $request){

$input = $request->all() ;

$this->validate($request ,[

'email'=>'required | email' ,
'password'=>'required'

]);

if(auth()->attempt(array( 'email'=>$input['email'], 'password'=>$input['password'] ))){

if(auth()->user()->role =='admin'){ return redirect()->route('admin.cpanel.home'); }else if(auth()->user()->role =='user'){ return redirect()->route('front.home');

} }

Please or to participate in this conversation.