Jul*_*oro 2 php redirect laravel laravel-socialite
我需要使用以下网址注册锦标赛:
http://laravel.dev/tournaments/1/register/
Run Code Online (Sandbox Code Playgroud)
这个 URL 在中间件 'auth' 中,所以如果用户没有登录,他会被重定向到登录页面。
我需要的是重定向到
http://laravel.dev/tournaments/1/register/
Run Code Online (Sandbox Code Playgroud)
社交登录后(我有 2 个提供商,fb 和 google)
在这种情况下,我不希望用户被重定向到 .env 中定义的重定向 url
这是我用来登录 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);
}
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能让系统将我重定向到初始操作而不是默认的 redirect_url 参数。
我通过guest()函数正常登录解决了这个问题,但我不知道在这种情况下如何实现它。
问题与这篇文章非常相似
主要功能是guest()和intended():
guest()创建对您选择的 URL 的重定向响应,最好是您的登录页面,同时存储当前的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);
}
}
Run Code Online (Sandbox Code Playgroud)
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();
}
}
Run Code Online (Sandbox Code Playgroud)
已经在框架中的函数声明
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3781 次 |
| 最近记录: |