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

Php 在Laravel中使用Socialite登录后重定向到URL,php,laravel,redirect,laravel-socialite,Php,Laravel,Redirect,Laravel Socialite,我需要使用URL注册比赛: http://laravel.dev/tournaments/1/register/ 此URL位于中间件“auth”中,因此如果用户未登录,则会重定向到登录页面 我需要的是重定向到 http://laravel.dev/tournaments/1/register/ 社交登录后(我有两个提供商,fb和谷歌) 在这种情况下,我不希望用户被重定向到.env中定义的重定向url 这是我使用社交名媛登录的功能: public function execute($reque

我需要使用URL注册比赛:

http://laravel.dev/tournaments/1/register/
此URL位于中间件“auth”中,因此如果用户未登录,则会重定向到登录页面

我需要的是重定向到

http://laravel.dev/tournaments/1/register/
社交登录后(我有两个提供商,fb和谷歌)

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

这是我使用社交名媛登录的功能:

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);
}
如何使系统将我重定向到初始操作,而不是默认的重定向url参数

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


问题与此非常相似:
return redirect()->guest('auth/login')

关键功能是
guest()
designed()

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

  • destinated()
    在用户返回后获取预期的重定向URL 完成身份验证

演示

验证
中间件-用于验证访问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();
    }
}
框架中已有功能的声明

照明\路由\重定向器

/**
 * 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);
}