2

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

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

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

我需要的是重定向到

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

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

在这种情况下,我不希望用户被重定向到 .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);
}

如何使系统将我重定向到初始操作而不是默认的 redirect_url 参数。

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

问题与这篇文章非常相似

4

2 回答 2

5

主要功能是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);
}
于 2016-01-27T11:01:27.743 回答
1

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

于 2016-01-27T01:37:00.850 回答