peraleks / Laravel-redirect-back-to-original-destination-after-login

laravel5
8 stars 2 forks source link

Thank you! #1

Open sunscreem opened 8 years ago

sunscreem commented 8 years ago

This saved me a heap of time on a recent project. Even got it working with a modal for the log in form.

:)

peraleks commented 8 years ago

Very happy. Not in vain tried

kagan94 commented 7 years ago

Thanks for this solution!

I slightly modified it:

    public function showLoginForm()
    {
        // If already logged in, redirect to the admin panel
        if (Auth::guard('admin')->user()) {
            return redirect()->intended($this->redirectPath());
        }

        // Save the intended to visit page in the session
        $previous_url = Session::get('_previous.url');
        $previous_url = htmlspecialchars($previous_url);
        if ($previous_url != route('admin.login')) {
            Session::put('url.intended', $previous_url);
        }
        return view('admin.auth.login');
    }

    protected function handleUserWasAuthenticated(Request $request, $throttles)
    {
        if ($throttles) {
            $this->clearLoginAttempts($request);
        }
        if (method_exists($this, 'authenticated')) {
            return $this->authenticated($request, Auth::guard($this->getGuard())->user());
        }

        // Will return user to the previous page (intended to visit)
        return redirect()->intended(Session::pull('url.intended', $this->redirectPath()));
    }