Laravel 5.4 and Socialite redirect to calling page after Twitter login
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:
return redirect()->back()
<?php
namespace AppHttpControllersAuth;
use AppHttpControllersController;
use AppUser;
use IlluminateSupportFacadesAuth;
use LaravelSocialiteFacadesSocialite;
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,
]);
intended
2 Answers
2
What you want is default Laravel
authentication behavior.
Laravel
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
.
/privatepage
auth middleware
/privatepage
url.intended
Laravel
url.intended
/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.
authenticated()
OR
If you have common page for guest
visitors and logged in
users you can store URL
into Session
something like this:
guest
logged in
URL
Session
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.
I ended up doing something similar to the
Session::flash('url', Request::server('HTTP_REFERER'));
solution. I attached the current path as a parameter to the end of the href like so in my link: href=" url('/auth/facebook') ?last-url=!! htmlentities(request()->path()) !!"
Then I added this to my redirectToProvider()
function: request()->session()->put('last_url', request()->query('last-url'));
I prefer your answer though, as it involves less code, and adding the parameter is redundant since the data is already being sent in the request through the $__SERVER
variable– Ely Bascoy
Jul 22 '17 at 21:57
Session::flash('url', Request::server('HTTP_REFERER'));
href=" url('/auth/facebook') ?last-url=!! htmlentities(request()->path()) !!"
redirectToProvider()
request()->session()->put('last_url', request()->query('last-url'));
$__SERVER
return redirect()->intended('/home#');
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You can use
intended
function– Aman Kumar
Jul 22 '17 at 7:30