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 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,
]);
}
}
2
Answers
What you want is default
Laravel
authentication behavior.If a user visits
/privatepage
and that page is protected by theauth middleware
then the user is redirected to the login page and/privatepage
is saved to session asurl.intended
. Then after a successful authenticationLaravel
checks if the session has theurl.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 andlogged in
users you can storeURL
intoSession
something like this:and use this after manipulation or authentication as:
Hope this resolves your problem.
return redirect()->intended(‘/home#’);