简体   繁体   English

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

[英]Redirect to URL after login with Socialite in Laravel

I need to register in a tournament with the URL: 我需要使用以下网址注册比赛:

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

This URL is in the middleware 'auth', so if the user is not logged, he is redirected to login page. 此URL在中间件“ auth”中,因此,如果未登录用户,则将其重定向到登录页面。

What I need is to redirect to 我需要重定向到

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

After social login ( I have 2 providers, fb and google) 社交登录后(我有2位提供者,FB和Google)

In this case, I don't want the user to be redirected to the redirected url defined in .env 在这种情况下,我不希望用户被重定向到.env中定义的重定向URL。

This is the function I use to login with Socialite: 这是我用来使用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);
}

How can I do to make the system redirect me to the initial action and not the default redirect_url param. 如何使系统将我重定向到初始操作,而不是默认的redirect_url参数。

I have it resolved with normal login with the guest() function, but I don't know how to implement it in this case. 我已使用guest()函数通过正常登录解决了该问题,但在这种情况下我不知道如何实现。

Problem is quite similar to this post 问题与这篇文章非常相似

The key functions are guest() and intended() : 关键函数是guest()intended()

  • guest() creates a redirect response to a URL of your choice, ideally your login page, whilst storing the current url where you can redirect your user to, when the latter successfully completes the authentication process. guest()创建对您选择的URL(最好是登录页面guest()的重定向响应,同时存储当前 URL( 后者成功完成身份验证过程 ,您可以将用户重定向到该URL)。

  • intended() fetches the intended redirect URL after the user completes the authentication. intended()在用户完成身份验证获取预期的重定向URL。

Demo 演示版

Authenticate Middleware - used to verify if user who is accessing the url is logged in. 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 , where we handle all login attempts 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();
    }
}

Declarations of functions already in framework 框架中已经存在的功能声明

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

Use it like this: return redirect()->guest('auth/login'); 像这样使用它: return redirect()->guest('auth/login'); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM