Home >Backend Development >PHP Tutorial >How to Redirect Users to Their Original Destination After Login in Laravel?

How to Redirect Users to Their Original Destination After Login in Laravel?

Barbara Streisand
Barbara StreisandOriginal
2024-12-05 21:16:12937browse

How to Redirect Users to Their Original Destination After Login in Laravel?

Redirect Back to Original Destination After Login in Laravel

Problem

Upon user authentication, it's required to redirect to the original page that prompted the login requirement. However, determining this original destination can be challenging.

Solution

For Laravel 5.3 and Above

Refer to Scott's answer provided below.

For Laravel 5 Up to 5.2

Auth Middleware:

// redirect the user to "/login"
// and stores the url being accessed on session
if (Auth::guest()) {
    return redirect()->guest('login');
}
return $next($request);

Login Action:

// redirect the user back to the intended page
// or defaultpage if there isn't one
if (Auth::attempt(['email' => $email, 'password' => $password])) {
    return redirect()->intended('defaultpage');
}

For Laravel 5.3 and Above

// auth middleware
Auth::routes(); // generates route for all authentication
// redirect to original page after auth
Redirect::intended('/profile');

The above is the detailed content of How to Redirect Users to Their Original Destination After Login in Laravel?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn