Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Authentication for multiple users

I have 4 types of users in my application and details are stored in 4 different tables, so how can I implement Laravel's Authentication?

Route::post('/adlogin', 'mainController@adminlogin');
Route::get('/sellerlogin', function () {
    return view('seller.pages.login');
});

Route::post('/sellerlog_in', 'mainController@sellerlogin');
like image 369
syed mahroof Avatar asked Jun 21 '26 02:06

syed mahroof


1 Answers

Use this as your controller

<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

//Use Dependencies
use Auth;

class AdminLoginController extends Controller
{

Middleware used here is admin which you have to create in config/auth.php

public function __construct()
{
    $this->middleware('guest:admin', ['except'=>'logout']);
}

Create a view for your admin Login

public function showLoginForm()
{
    return view('auth.admin_login');
}

public function login(Request $request)
{
    //Validate the Form Data
    $this->validate($request, [
        'email'=>'required|email',
        'password'=>'required|min:5'
    ]);

    //Attempt to log the Admin In
    $email= $request->email;
    $password= $request->password;
    $remember= $request->remember;

After Successfully login where you want to redirect like here I am redirecting toadmin.dashboard

    //If Successful redirect to intended location
    if (Auth::guard('admin')->attempt(['email' => $email, 'password' => $password], $remember)) {
        return redirect()->intended(route('admin.dashboard'));
    }

    //If Unsuccessful redirect back to login form with form data
    return redirect()->back()->withInput($request->only('email', 'remember'));
}

/**
 * Log the Admin out of the application.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function logout()
{
    Auth::guard('admin')->logout();

    return redirect()->route('admin.login');
}
}

After logout redirect back to login page

Make sure you are using right guard for right User. Create same functionality for more user types as you want create guards for them.

In config/app.php

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

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

As admin and admin-api created here create for your user types

Add providers

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

At last for resetting passwords use this.

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

In http/middleware/redirectIfAuthenticated.php add this to your handle function

//Check for admin login guard
        switch ($guard) {
            case 'admin':
                if (Auth::guard($guard)->check()) {
                    return redirect()->route('admin.dashboard');
                }
                break;

            default:
                if (Auth::guard($guard)->check()) {
                    return redirect('/dashboard');
                }
                break;
        }

You can add more cases for your user types.

This is all you want to create multi auth. As you created different table for different roles you have to follow this process.

  • Create LoginController for all user types or use your logic to log them in.
  • Add your guards for every user type in auth.php
  • Add your cases to RedirectIfAuthenticated
like image 88
Ashutosh Sharma Avatar answered Jun 22 '26 14:06

Ashutosh Sharma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!