gpt4 book ai didi

php - 在 Laravel 中使用 Socialite 登录后重定向到 URL

转载 作者:行者123 更新时间:2023-12-01 22:25:45 30 4
gpt4 key购买 nike

我需要使用 URL 注册锦标赛:

http://laravel.dev/tournaments/1/register/

这个 URL 在中间件 'auth' 中,所以如果用户没有登录,他将被重定向到登录页面。

我需要的是重定向到

http://laravel.dev/tournaments/1/register/

社交登录后(我有 2 个提供商,fb 和 google)

在这种情况下,我不希望用户被重定向到 .env 中定义的重定向 url

这是我用来登录 Socialite 的函数:

public function execute($request, $listener, $provider) {

if (!$request) {

return $this->getAuthorizationFirst($provider);
}
$user = $this->users->findByUserNameOrCreate($this->getSocialUser($provider), $provider);
if (!is_null($user)){
$this->auth->login($user, true);
}else{
Session::flash('error', Lang::get('auth.account_already_exists'));
return redirect('auth/login');
}


return $listener->userHasLoggedIn($user);
}

我怎样才能使系统将我重定向到初始操作而不是默认的 redirect_url 参数。

我已经通过 guest() 函数正常登录解决了这个问题,但我不知道在这种情况下如何实现。

问题与此非常相似 post

最佳答案

关键函数是 guest()intended() :

  • guest() 创建对您选择的 URL 的重定向响应,理想情况下是您的登录页面,同时存储您所在的当前 url可以将您的用户重定向到,后者成功完成身份验证过程。

  • intended() 在用户 之后获取预期的重定向 URL完成认证。

演示

Authenticate 中间件 - 用于验证访问 url 的用户是否已登录。

class Authenticate
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
//check if user is not logged in.
if (Sentinel::check() === false) {
//If ajax, send back ajax response
if ($request->ajax()) {
return response('Unauthorized.', 401);
} else {
//redirect guest to login page
return redirect()->guest('/login');
}
}

return $next($request);
}
}

RESTful UserController,我们在其中处理所有登录尝试

/**
* POST: Login
*
* @param Request $request
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Http\Response
*/
public function postLogin(Request $request)
{
//Authenticate User if possible
if ($this->service->user->loginByEmail($request->only(['email','password','remember_me']))) {
//Authentication Successful - Redirect to url that guest was trying to access, else redirect to home page.
return redirect()->intended('/');
} else {
//Authentication error, redirect back to login page with input
return redirect('/login')->withErrors(['password_incorrect' => 'Your password is incorrect. Please try again.'])->withInput();
}
}

框架中已有的函数声明

Illuminate\Routing\Redirector

/**
* Create a new redirect response, while putting the current URL in the session.
*
* @param string $path
* @param int $status
* @param array $headers
* @param bool $secure
* @return \Illuminate\Http\RedirectResponse
*/
public function guest($path, $status = 302, $headers = [], $secure = null)
{
//Store current full URL in session
$this->session->put('url.intended', $this->generator->full());

//Redirect
return $this->to($path, $status, $headers, $secure);
}

/**
* Create a new redirect response to the previously intended location.
*
* @param string $default
* @param int $status
* @param array $headers
* @param bool $secure
* @return \Illuminate\Http\RedirectResponse
*/
public function intended($default = '/', $status = 302, $headers = [], $secure = null)
{
//Get path that guest was trying to access
$path = $this->session->pull('url.intended', $default);

//Redirect
return $this->to($path, $status, $headers, $secure);
}

关于php - 在 Laravel 中使用 Socialite 登录后重定向到 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35022774/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com