简体   繁体   中英

I want to redirect users when they after login, to profile page

I want to redirect user to profile page when they log in. But, it directs the to home ('/') page. Sometimes, it works if I open it in incognito mode. but not every time.

Following is my Login controller

class LoginController extends Controller
{  

    use AuthenticatesUsers;

    protected function redirectTo()
    {
        return '/profile';
    }

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct(Request $request)
    {
        if($email = $request->user) {

            $user = User::where('email', $email)->first();

            if($user && $user->auto_login_key == $request->key) {
                Auth::loginUsingId($user->id);
            } else {
                Redirect::to('/login')->send();
            }
        }

        $this->middleware('guest')->except('logout');
    }
}

And this is my Redirected authenticated miidleware

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            return redirect('/profile');
        }

        return $next($request);
    }
}

You can use return redirect('/profile'); inside your authentication function, for exmaple:

    public function __construct(Request $request)
{
    if($email = $request->user) {

        $user = User::where('email', $email)->first();

        if($user && $user->auto_login_key == $request->key) {
            Auth::loginUsingId($user->id);
            return redirect('/profile');
        } else {
            Redirect::to('/login')->send();
        }
    }

    $this->middleware('guest')->except('logout');
}

Don't change anything, anywhere. In your LoginController , just change your $redirectTo variable to '/profile' :

/**
 * Where to redirect users after login.
 *
 * @var string
 */
protected $redirectTo = '/profile';

As it is working in incognito, the issue is just with cache. You can hard-reload (reload and clear cache) by pressing Ctrl + F5

据我了解您的问题,您必须在进行此类更改后首先清除缓存,并确保浏览器缓存是清除的。

 return redirect('/profile');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM