简体   繁体   English

laravel ajax登录后重定向

[英]laravel ajax redirect after login

I have a global like button that allows only authenticated users to like a post. 我有一个全局的“喜欢”按钮,仅允许经过身份验证的用户喜欢帖子。 This is done via ajax so when a user is not authenticated gets redirected to /login. 这是通过ajax完成的,因此当未通过身份验证的用户被重定向到/ login。

Problem is that I don't have the after login redirect location. 问题是我没有登录后重定向的位置。 The laravel built-in Redirect::intended is not working because the filter is applied via ajax. laravel内置的Redirect :: intended无法使用,因为该过滤器是通过Ajax应用的。 So when user gets authenticated how can I redirect him back to where he pressed the like button. 因此,当用户通过身份验证后,我如何将其重定向回他按下“赞”按钮的位置。 ?

You probably are using Json response data. 您可能正在使用Json响应数据。 return status code 401. with the following structure 具有以下结构的状态码401。

{
    success: false,
    message: "You need to log in to like a post"
}

I solved this by saving the location to the session. 我通过将位置保存到会话来解决此问题。 I do this in the controller's constructor. 我在控制器的构造函数中执行此操作。 I just thought maybe there would be a more elegant way. 我只是想也许会有一种更优雅的方式。

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

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