0

我有 laravel 5.2 应用程序,如果用户已经登录,用户将被限制访问/login。现在我们已经实现了“从其他应用程序(不是 laravel)登录我们的应用程序”的功能。现在的问题是,当请求应用程序进行 ajax 调用以获取登录时,如果用户在请求之前未登录,laravel 5.2 会返回完美响应。但是登录后,如果再次请求应用程序请求登录,laravel 应用程序返回主页视图作为响应。我想在处理登录请求之前对其进行自定义并让用户注销。我在互联网和调试上花费了太多时间。但是到处都有人要求限制登录页面。但我的要求是允许/login使用 post 方法请求并自定义响应. 如果有人知道答案,将不胜感激。

这是请求应用程序的代码

$.ajax({
    type: 'POST',
    url: 'http://192.168.1.78/my-app/login',
    data: {
        st_email: 'xyz@xyz.com',
        st_password: 'myPass'
    }
}).success(function (response) {
    // getting view in response if user is already restrict
}).error(function () {
    console.log('error');
});
4

1 回答 1

1

在你的 app/Http/Middleware/RedirectIfAuthenticated 你有这个功能:

    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            return redirect('/');
        }

        return $next($request);
    }

如果您的应用停止从 /login 重定向您,则删除此选项。你也可以像这样编辑这个函数:

    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check() and ! $request->isMethod('post')) {
            return redirect('/');
        }

        return $next($request);
    }

仅当您请求不是 post 方法时,此功能才会重定向您

于 2016-10-11T10:52:03.457 回答