0

I have a problem. When guest is on the page, and want login. Laravel redirect on main page after login. How I can do, when guest login save page where he was and redirect there?

Middleware auth:

<?php

namespace App\Http\Middleware;

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

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()->guest('/login');
    }

    return $next($request);
}
}

I use default auth laravel. But back on previous page after login not working.

Jadasdas
  • 869
  • 4
  • 19
  • 39

1 Answers1

0

Since the original question was for Socialite and was later modified to the default auth, I will address both here.

1. Laravel Socialite

Laravel Socialite intended redirects dont work the way people expect them to because the first redirect is from the controller for the authentication.

Here's what you need to do.

In your redirectToProvider method, add this:

if(!session()->has('url.intended')) {
    session()->put('url.intended', url()->previous());
}

Then, in your handleProviderCallback method, add this:

if(session()->has('url.intended')) {
    $redirectUrl = session('url.intended');
} else {
    $redirectUrl = 'your fallback url here';
}
session()->forget('url.intended');
return redirect($redirectUrl);

2. Default Authentication If You Have a Separate Login Page

if (Auth::attempt(['email' => $email, 'password' => $password])) {
    // Authentication passed...
    return redirect()->intended('dashboard');
}

3. Default Authentication If You Do Not Have a Separate Login Page

What this means is that perhaps you have a modal window on each page for a login and so, there's no separate page for login per se and each page can perform a post request for login.

$redirectUrl = session('url.intended', url()->previous());
session()->forget('url.intended');
return redirect($redirectUrl);
Paras
  • 9,258
  • 31
  • 55
  • But default laravel auth? – Jadasdas Feb 01 '18 at 20:08
  • But where I can write code on login page. If I have only auth/login.blade ? But controller auth use laravel – Jadasdas Feb 01 '18 at 20:21
  • If you have a separate blade template for login, case #2 applies. You need to add that code to the post route method for login. It should work out of the box – Paras Feb 01 '18 at 20:23
  • In my routes I didn't have post login method. I use default auth laravel. I edit only template for login.blade and all. This is default laravel auth: `Auth::login` – Jadasdas Feb 01 '18 at 20:25
  • In your LoginController, add a `protected function authenticated(Request $request, $user)` and add the code in case #2. That should do the trick – Paras Feb 01 '18 at 20:28
  • undefined variable email, password? Where I can get email and password? – Jadasdas Feb 01 '18 at 20:39
  • Just add this in the authenticated method: `return redirect()->intended('dashboard');` – Paras Feb 01 '18 at 20:40
  • And I redirect on dashboard, but I have more pages, and example guest on: /cart page, how I can redirect on cart or other pages? – Jadasdas Feb 01 '18 at 20:41
  • When the guest tries to go to the cart page if unauthenticated, is he/she redirected to the login page first? – Paras Feb 01 '18 at 20:43
  • Yes. He redirect on login page – Jadasdas Feb 01 '18 at 20:43