在Laravel中使用Socialite登录后重定向到URL [英] Redirect to URL after login with Socialite in Laravel

查看:109
本文介绍了在Laravel中使用Socialite登录后重定向到URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用以下网址注册比赛:

I need to register in a tournament with the URL:

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

此URL在中间件'auth'中,因此,如果未登录用户,则将其重定向到登录页面.

This URL is in the middleware 'auth', so if the user is not logged, he is redirected to login page.

我需要重定向到

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

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

After social login ( I have 2 providers, fb and google)

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

In this case, I don't want the user to be redirected to the redirected url defined in .env

这是我用来使用Socialite登录的功能:

This is the function I use to login with 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参数.

How can I do to make the system redirect me to the initial action and not the default redirect_url param.

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

I have it resolved with normal login with the guest() function, but I don't know how to implement it in this case.

问题与该帖子十分相似

推荐答案

关键功能是guest()intended():

  • guest()创建对您选择的URL的重定向响应, 最理想的是您的登录页面,同时将当前网址存储在您的 可以将您的用户重定向到 成功完成的时间 认证过程.

  • 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.

intended()在用户之后 获取预期的重定向URL 完成身份验证.

intended() fetches the intended redirect URL after the user completes the authentication.

演示

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

Authenticate Middleware - used to verify if user who is accessing the url is logged in.

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,我们在其中处理所有登录尝试

RESTful UserController, where we handle all login attempts

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

这篇关于在Laravel中使用Socialite登录后重定向到URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆