Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Middleware, how to redirect after check Laravel 5

I need after check if user is logged as editor, to redirect to profile page...

Here is my code:

<?php namespace App\Http\Middleware;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;

use Closure;

class AdminMiddleware {

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(Auth::check()){
                    if(Auth::user()->roles->toArray()[0]['role'] == 'editor'){
                       return redirect('/profile');
                    }
                    return $next($request);
                }
                else
                {

                   return $next($request);
                }
    }

}

Problem with this code is when user is editor I get infinite loop....

Here is my routs:

Route::group(['middleware' => 'auth'], function(){

    Route::get('home', ['middleware' => 'admin', function()
        {
        return view('home');
        }]);
    Route::get('profile', array(
       'as' => 'profile',
       'uses' => 'UserController@getProfile'
    ));


});

Anyone know what is problem?

like image 564
Vladimir Djukic Avatar asked Mar 15 '15 15:03

Vladimir Djukic


People also ask

How do I redirect a user to another page after login in Laravel?

By default laravel will redirect a user to the home page like so: protected $redirectTo = '/home'; To change that default behaviour, add the following code in App/Http/Controllers/Auth/LoginController. php . This will redirect users (after login) to where ever you want.

How do I redirect a route in Laravel?

Laravel offers an easy way to redirect a user to a specific page. By using the redirect() helper you can easily create a redirect like the following: return redirect('/home'); This will redirect the user to the /home page of your app.

How do I redirect back to original URL after successful login in Laravel?

You may use Redirect::intended function. It will redirect the user to the URL they were trying to access before being caught by the authenticaton filter. A fallback URI may be given to this method in case the intended destinaton is not available.

How do I redirect back in Laravel?

If you just want to redirect a user back to the previous page (the most common example - is to redirect back to the form page after data validation failed), you can use this: return redirect()->back();


2 Answers

I found this to be a less code and less decisions for redirecting users based on roles, Put this in your AuthController.php

protected function authenticated( $user)
{

    if($user->user_group == '0') {
        return redirect('/dashboard');
    }

    return redirect('my-account');
}

https://laracasts.com/discuss/channels/laravel/how-best-to-redirect-admins-from-users-after-login-authentication

like image 95
Harry Bosh Avatar answered Oct 08 '22 02:10

Harry Bosh


Where did you register your middleware in App\Http\Kernel?

Is it in protected $middleware = [] or protected $routeMiddleware = [] ?

If registered in $middleware it will run on each very request thereby causing infinite loop, if so use only $routeMiddleware

like image 24
Emeka Mbah Avatar answered Oct 08 '22 04:10

Emeka Mbah