Redirect to URL after login with Socialite in Laravel

Deadly 提交于 2019-12-13 19:06:05

问题


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.

What I need is to redirect to

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

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

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

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

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

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

Problem is quite similar to this post


回答1:


The key functions are guest() and 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.

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

Demo

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

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



回答2:


Use it like this: return redirect()->guest('auth/login');.



来源:https://stackoverflow.com/questions/35022774/redirect-to-url-after-login-with-socialite-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!