简体   繁体   English

Laravel登录后将用户重定向到原始目的地

[英]Laravel Redirect user to original destination after login

In my Laravel app I have a private page which can be only accessed by Auth users. 在我的Laravel应用中,我有一个私人页面,只能由Auth用户访问。 I am also using social logins, so when a user goes to private page without logging in, Laravel sends user to login page and after successful login, the user is sent back to original destination (private page). 我也在使用社交登录,因此,当用户不登录而进入私有页面时,Laravel会将用户发送到登录页面,成功登录后,该用户将被发送回原始目的地(私有页面)。 Only when a user logs in via Laravel inbuilt Auth system, but when a user logs in via the Social Login method, the user will be directed to the homepage. 仅当用户通过Laravel内置Auth系统登录时,而当用户通过“社交登录”方法登录时,该用户将被定向到主页。

This is my Social Login handler after login: 登录后,这是我的社交登录处理程序:

public function handleProviderCallback($provider)
{

    $user = Socialite::driver($provider)->user();

    $authUser = $this->findOrCreateUser($user, $provider);
    Auth::login($authUser, true);
    return redirect('/home');
}

How can I send the user to the original destination if it's present in session, otherwise send them back to home. 如果会话中存在该用户,我该如何将其发送到原始目的地,否则将其发送回首页。

The documentation mentions it very briefly, but Laravel has a built-in method: 文档非常简短地提到了它,但是Laravel有一个内置方法:

return redirect()->intended('/home')

Whenever the Auth middleware redirects a user because he is not logged in, his original url will be stored. 每当Auth中间件由于用户未登录而重定向用户时,其原始URL将被存储。 intended checks whether or not that url is stored and uses that, or the fallback you supply as argument. intended检查该URL是否被存储和使用,或作为参数提供的后备。

Use, 采用,

return redirect()->intended('/home')

The intended method on the redirector will redirect the user to the URL they were attempting to access before being intercepted by the authentication middleware. 重定向器上的预期方法会将用户重定向到他们尝试访问的URL,然后再被身份验证中间件拦截。 A fallback URI may be given to this method in case the intended destination is not available. 在预期的目标不可用的情况下,可以为此方法提供后备URI。

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

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