[英]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. 我正在整理一个网站,该网站在Laravel 5.4中通过Socialite具有Twitter登录。 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. 但是,我希望能够将用户重定向到他激活“使用Twitter登录”链接的页面。 I tried using return redirect()->back()
, but since the last request was to Twitter's api, that just causes an infinite loop. 我尝试使用return redirect()->back()
,但是由于最后一个请求是对Twitter的api的请求,因此只会导致无限循环。 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. 您想要的是默认的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
. 如果用户访问/privatepage
并且该页面受auth middleware
保护,则该用户将被重定向到登录页面,并且/privatepage
被保存为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. 然后,在成功通过身份验证后, Laravel
检查会话是否具有url.intended
密钥,如果存在,则将重定向到会话中保存的URL。 If not, it will redirect to whatever page you have defined, default is /home
. 如果没有,它将重定向到您定义的任何页面,默认值为/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. 希望这能解决您的问题。
返回redirect()-> intended('/ home#');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.