简体   繁体   中英

Laravel 5.4 and Socialite redirect to calling page after Twitter login

I'm putting together a site that has a Twitter login through Socialite in Laravel 5.4. Right now once the user is logged in, I redirect to the home page; however, I'd like to be able to redirect the user to whichever page he was on activating the "Sign In with Twitter" link. I tried using return redirect()->back() , but since the last request was to Twitter's api, that just causes an infinite loop. I'm sure this must be pretty basic, but I can't seem to figure it out. This is the controller handling my auth requests:

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;

class AuthController extends Controller
{
    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectPath = '/home';

    /**
     * Redirect user to OAuth Provider.
     *
     * @param $provider
     * @return Response
     */
    public function redirectToProvider($provider)
    {
        return Socialite::driver($provider)->redirect();
    }

    /**
     * Obtain the user information from provider. check if user already
     * exists in our db by looking up their provider_id in the db.
     * If user exists, log them in. Otherwise, create a new user then log them in.
     * After login, redirect them to the authenticated users homepage.
     *
     * @param $provider
     * @return Response
     */
    public function handleProviderCallback($provider)
    {
        try {
            $user = Socialite::driver($provider)->user();
        } catch (Exception $e) {
            return redirect("auth/$provider");
        }
        $authUser = $this->findOrCreateUser($user, $provider);
        Auth::login($authUser, true);

        return redirect($this->redirectPath);
    }

    /**
     * If a user has registered before using social auth, return the user
     * else, create a new user object.
     *
     * @param $user Socialite user object
     * @param $provider Social auth provider
     * @return User
     */
    public function findOrCreateUser($user, $provider)
    {
        $authUser = User::where('provider_id', $user->id)->first();
        if ($authUser) {
            return $authUser;
        }

        return User::create([
            'name'        => $user->name,
            'handle'      => $user->nickname,
            'provider'    => $provider,
            'provider_id' => $user->id,
            'avatar'      => $user->avatar_original,
        ]);
    }

}

What you want is default Laravel authentication behavior.

return $this->authenticated($request, $this->guard()->user())
    ?: redirect()->intended($this->redirectPath());

If a user visits /privatepage and that page is protected by the auth middleware then the user is redirected to the login page and /privatepage is saved to session as url.intended . Then after a successful authentication Laravel checks if the session has the url.intended key, and if does, it will redirect to the URL saved in the session. If not, it will redirect to whatever page you have defined, default is /home .

If you would like to manually handle what happens when a user is authenticated, then you can create a function called authenticated() which will be called instead with manual redirects.

OR

If you have common page for guest visitors and logged in users you can store URL into Session something like this:

Session::flash('url',Request::server('HTTP_REFERER')); 

and use this after manipulation or authentication as:

return redirect(Session::get('url')); 

Hope this resolves your problem.

返回redirect()-> intended('/ home#');

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