簡體   English   中英

當模板請求返回401重定向時,如何使用Laravel將用戶重定向到登錄頁面

[英]How to redirect user to login page with Laravel when template request returns 401 redirect

我有一個angularjs / laravel Web應用程序,其中模板包含動態加載。

問題是,當用戶的會話超時並且他們導航到頁面上的新部分時,它將在服務器端進行重定向,並實際上回送模板應該存在的登錄頁面。

如何攔截此模板請求,而是將用戶重定向到登錄頁面,而不是簡單地將登錄頁面作為模板返回?

在一個項目中,我們使用這種攔截http錯誤的方式。 在那里你可以重定向到登錄頁面(或任何你喜歡的)。

function httpInterceptor($provide, $httpProvider) {
    function interceptorFunction($q) {
        return {
            responseError: function (rejection) {
                if (typeof rejection.status !== 'undefined') {
                    if (rejection.status === 401) {
                        //Your app will handle 401 errors here... 
                    } 
                }
                return $q.reject(rejection);
            }
        };
    }

    $provide.factory('httpInterceptor', interceptorFunction);
    interceptorFunction.$injector = ['$q'];
    $httpProvider.interceptors.push('httpInterceptor');

 angular.module('app', dependencies)
    .config(httpInterceptor);

'app'模塊是我們的'主要'模塊。 401錯誤應該發生,因此服務器應該至少一次響應該狀態代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM