PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, October 19, 2022

[FIXED] How to redirect Admin to a specific page and users to another - Laravel 8 jetstream

 October 19, 2022     admin, laravel, middleware, panel, php     No comments   

Issue

I'm trying to redirect admin to the admin panel page and the users to the homepage

i created new field for usertype and i add the value in database which is "admin"

$table->string('usertype')->nullable();

then i created this Midlleware (AdminMiddleware):

<?php

 namespace App\Http\Middleware;

 use Closure;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Auth;

 class AdminMiddleware
{
/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @return mixed
 */
public function handle(Request $request, Closure $next)
{
    if(Auth::user()->usertype == 'admin')
    {
        return $next($request);
    }
    else
    {
        return redirect('/dashboard');
    }
    
   }
  }

then i add it to Kernel:

        'admin' => \App\Http\Middleware\AdminMiddleware::class,

and i add the function to RouteServiceProvider and imported the class:

public const HOME = '/dashboard';
    protected function redirectTo()
    {
        if(Auth::user()->usertype == 'admin')
        {
            return 'panel';
        }
        else
        {
            return 'dashboard';
        }
    }

then i added the route to web.php:

Route::get('/', function () {
return view('index');
});

Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
return view('index');
})->name('dashboard');





Route::group(['middleware' => ['auth:sanctum','auth','admin','verified']],function() {
Route::get('/panel', function () {
    return view('admin.dashboard');
});

});

and i created two accounts one of them is the admin with value of "admin" for usertype but when i login it still redirecting me to the homepage

what am i doing wrong here?


Solution

There is a simple way which I have been using in my app.

You don't need a middleware for this purpose. Create a controller in the name of HomeController or whatever you want. In you LoginController change the property redirectTo to a simple route. For example '/home' or even / for super simple routes. Now in routes/web.php add this route pointing to a method in HomeController.

Route::get('/home', 'HomeController@redirectUser');

Or

Route::get('/', 'HomeController@redirectUser');

Additionally create your own routes which you want to redirect to.

Route::group(['middleware' => ['auth:sanctum', 'verified']], function () {
    Route::get('/panel', function () {
        return view('admin.dashboard');
    });
});

 You can also create routes for number of users.

Now in your HomeController, in redirectUser() method, add your needs.

if (auth()->user()->usertype == 'admin') {
    return redirect(url('route url'));
} else {
    return redirect (url('some other url'));
}

Please make sure that you have defined the same URI in your routes file.

You can also redirect users in your LoginController as well. But it works only after logging in. this method will also redirects user to the desired location when they click home button or the logo which contains homepage url.

Hope this one will help you.

Thank you.



Answered By - Sharan
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing