简体   繁体   English

当用户未通过身份验证时,如何让Laravel发送登录表单,而不是重定向到登录页面?

[英]How do I have Laravel send a login form when a user isn't authenticated, rather than redirecting to a login page?

Currently, Laravel redirects the user to a different URL if he isn't logged in (eg /auth/login ). 当前,Laravel如果用户未登录,则将其重定向到其他URL(例如/auth/login )。 /auth/login contains logic to check if a user has supplied login credentials, in which case they are redirected to another page (eg /home ). /auth/login包含用于检查用户是否提供了登录凭据的逻辑,在这种情况下,它们将被重定向到另一个页面(例如/home )。

(Is this correct? I'm still learning.) (这样对吗?我还在学习。)

I'd like to change this functionality. 我想更改此功能。 Ideally, when a user visits / or /index.php , Laravel would detect that he's not logged in and would return a different view (the login form) and not redirect. 理想情况下,当用户访问//index.php ,Laravel会检测到他尚未登录,并且将返回不同的视图(登录表单)并且不会重定向。 The user would see /#/users in the navigation bar, not /auth/login . 用户将在导航栏中看到/#/users ,而不是/auth/login The login form would submit to the same page. 登录表单将提交到同一页面。

Now, when Laravel detects that the user isn't logged in but Auth::check() works, it would log the user in and continue with the original view. 现在,当Laravel检测到用户尚未登录但Auth::check()有效时,它将登录用户并继续原始视图。

The reason I'd like to do this is so that Angular view navigation still works. 我想这样做的原因是为了使Angular视图导航仍然有效。 Because of the redirect, the #param ( /#/users ) is lost and we can only redirect the user to the dashboard. 由于重定向, /#/users param( /#/users )丢失了,我们只能将用户重定向到仪表板。

Is this possible is Laravel? Laravel有可能吗? I'd be satisfied with learning where all the current functionality is hidden so I can start there. 我对学习隐藏所有当前功能的地方感到满意,因此我可以从那里开始。

Laravel will redirect the user to auth/login, then after authentication, redirect it back to it's original view. Laravel会将用户重定向到auth / login,然后在身份验证之后,将其重定向回其原始视图。

One way to prevent this from happening, is to use Laravel as an API resource, and have your Angular app control the user interface and actions. 一种防止这种情况发生的方法是,将Laravel用作API资源,并让Angular应用程序控制用户界面和操作。

For example, when a user land in '/', your Angular app will gather all the necessary data from your Laravel API via ajax calls. 例如,当用户进入“ /”时,您的Angular应用将通过ajax调用从Laravel API收集所有必要的数据。 The first ajax call would be to check if user is authenticated, and if not, use Angular to display a modal with a login form. 第一个ajax调用将检查用户是否已通过身份验证,如果未通过身份验证,则使用Angular显示带有登录表单的模式。 Then use the form to authenticate the user via Laravel API. 然后使用该表单通过Laravel API验证用户身份。

This way your url and current view won't change, and you have full control with your Angular app. 这样,您的网址和当前视图就不会改变,并且您可以完全控制Angular应用。

Just an idea...hope it helps. 只是一个主意...希望有帮助。

暂无
暂无

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

相关问题 如果在Laravel中通过身份验证,则将用户从登录页面重定向到主页 - Redirecting User from Login Page to Home Page if Authenticated in Laravel 我可以通过帖子请求登录用户,而不是默认情况下通过Laravel登录页面登录 - Can I log a user in with post request rather than through Laravel login page by default Laravel,如果用户未通过身份验证,则重定向到特定的登录页面 - Laravel, redirect to specific login page if user is not authenticated 用户登录后(通过我创建的登录表单)如何将他们发送到其他页面? - Once a user logs in (via a login form I've created) how do I send them to a different page? 用户登录后在Laravel上使用模式形式进行重定向 - Redirecting after user login with modal form on Laravel PHP登录后重定向到BASE URL,而不是上一页 - PHP Redirecting to BASE URL after login rather than previous page Laravel - 如果用户未经过身份验证,如何重定向到登录 - Laravel - How to redirect to login if user is not authenticated 当用户成功在我的网站上注册时,我想看到“将您重定向到登录页面”。 我怎样才能做到这一点? - I want to see “redirecting you to the login page” when a user successfully registers on my website. How can i do this? 如果未通过身份验证,laravel 5.2中的路由不会自动重定向到登录 - Routes in laravel 5.2 are not automatically redirecting to login if not authenticated 登录Symfony 2.3后未对用户进行身份验证 - User isn't Authenticated after Login Symfony 2.3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM