You are on page 1of 1

Stack Overflow sign up log in

Questions Jobs Tags Users Badges Ask

1 How to create multi auth in laravel 7?


laravel laravel-7

I used to be for laravel 5.5 and earlier than


https://github.com/Hesto/multi-auth .

But this repository don't update for laravel 7.0

How to create multi auth in Laravel 7.0 ?

share improve this question follow

mySun asked
1,426 ● 2 ● 23 ● 43 Mar 19 at 17:21

Try this package which support laravel 7 too –


Andreas Hunter Mar 19 at 17:29

add a comment

1 Answer order by votes

If you want to use a package the you can


4 use this package laravel-multiauth

OR

if you want to create custom multi-auth


based on a field in your users table for.e.g
is_admin then follow the below steps:

Assuming you have installed Laravel and


made a connection to database

Step1: Add new row is_admin in users


table and model. then run the migration.

public function up()


{
Schema::create('users',
function (Blueprint $table) {
$table->bigIncremen
ts('id');
$table->string('nam
e');
$table->string('ema
il');
$table->timestamp('
email_verified_at')->nullable();
$table->boolean('is
_admin')->nullable(); // add this
$table->string('pas
sword');
$table->rememberTok
en();
$table->timestamps(
);
app/User.php

protected $fillable = [
'name', 'email', 'password', 'i
s_admin' //add here
];

Then run the migration

php artisan migrate

Step2: Create Auth using scaffold

Install laravel/ui package using below


command

composer require laravel/ui

Generate auth

php artisan ui bootstrap --auth

npm install

npm run dev

Step3: Create IsAdmin Middleware will


allows only admin access users to that
routes

php artisan make:middleware IsAdmin

app/Http/middleware/IsAdmin.php

Add this in IsAdmin middleware

public function handle($request, Cl


osure $next)
{
if(auth()->user()->is_admin ==
1){
return $next($request);
}
return redirect(‘home’)->with(‘
error’,"You don't have admin access
.");
}

Register your IsAdmin middleware in


app/Http/Kernel.php

protected $routeMiddleware = [
'auth' => \App\Http\Middlewa
re\Authenticate::class,
'auth.basic' => \Illuminate
\Auth\Middleware\AuthenticateWithBa
sicAuth::class,
'bindings' => \Illuminate\R
outing\Middleware\SubstituteBinding
s::class,
'cache.headers' => \Illumin
ate\Http\Middleware\SetCacheHeaders
::class,
'can' => \Illuminate\Auth\M
iddleware\Authorize::class,
'guest' => \App\Http\Middle
ware\RedirectIfAuthenticated::class
,
'signed' => \Illuminate\Rout
ing\Middleware\ValidateSignature::c
lass,
Step4: Create your route for admin in
routes/web.php

Route::get('admin/home', 'HomeContr
oller@adminHome')->name('admin.home
')->middleware('is_admin');

Step5: Add adminHome() method for admin


route in
app/Http/Controllers/HomeController.p
hp

public function adminHome()


{
return view('adminHome');
}

Step6: Change LoginController, when


user will login than we redirect according to
user access. if normal user than we will
redirect to home route and if admin user
than we redirect to admin route in
app/Http/Controllers/Auth/LoginContro
ller.php

public function login(Request $requ


est)
{
$input = $request->all();
$this->validate($request, [
'email' => 'required|email'
,
'password' => 'required',
]);

if(auth()->attempt(array('email
' => $input['email'], 'password' =>
$input['password'])))
{
if (auth()->user()->is_admi
n == 1) {
return redirect()->rout
e('admin.home');
}else{
return redirect()->rout

share improve this answer follow

Sehdev answered
4,362 ● 3 ● 7 ● 27 Mar 24 at 2:58

Your Answer

Body

Add picture

Log in

OR

Name

Email

By clicking “Post Your Answer”, you agree to our terms of service,


privacy policy and cookie policy

Post Your Answer

meta chat tour help blog privacy policy legal contact us full
site
2020 Stack Exchange, Inc. user contributions under cc by-sa

You might also like