簡體   English   中英

為什么我的頁面在重新加載時當前沒有登錄?

[英]Why is my page is not getting currently logged in when reload?

我在我的網站上使用 Angular。 最初,一旦頁面啟動,就沒有問題,並且可以完美地進行身份驗證。 但是,刷新頁面時,它無法再拉取當前登錄的用戶。 看起來它沒有調用 Windows 身份驗證。 我正在添加我的代碼。 任何幫助表示贊賞。

intercept(req: HttpRequest<any>, next: HttpHandler):
    Observable<HttpEvent<any>> {

    console.log('AppInterceptorService1111');
    req = req.clone({
        withCredentials: true
      });

    return next.handle(req)
        .pipe(catchError(this.handleError));
}

本地存儲已經實現。

this.authenticateCurrentNTLogin().subscribe((data: any) => {
            this.currentUserInfoService.tokenInfo = data.Response;            
            localStorage.setItem('userToken', this.currentUserInfoService.tokenInfo.access_token);
            localStorage.setItem('userRoles', this.currentUserInfoService.tokenInfo.role);        
            this.currentUserInfoService.currentUserTokenExpiration = new Date();
            this.currentUserInfoService.currentUserTokenExpiration.setSeconds(new Date().getSeconds() + parseInt(this.currentUserInfoService.tokenInfo.expires_in));
            localStorage.setItem('myTokenExpiration', this.currentUserInfoService.currentUserTokenExpiration.toLocaleDateString() +
            ' ' + this.currentUserInfoService.currentUserTokenExpiration.toLocaleTimeString());                     
        }, 

使用身份驗證登錄后,將用戶 ID 或登錄 ID 存儲在變量中。 在整個應用程序中,您可以檢索此局部變量以確認用戶 ID 是否已登錄,即使在刷新后也是如此。 當他注銷時,清除該變量。 換句話說,您可以在客戶端使用本地存儲作為會話。 當用戶登錄和身份驗證為 true 時,使用下面的一個。

localStorage.setItem("user", user id)

var user = local storage.getItem("user")

在您重新加載的頁面中,使用 .get 方法,如果用戶不為空,則允許他訪問當前頁面。 如果用戶變量為空,則用戶未登錄並將其重定向到登錄頁面。 當用戶退出時:

local storage.removeItem("user")

希望這就是你要問的。

在我的應用程序中,一旦用戶通過身份驗證,我會將用戶名和 user_id 存儲在 localStorage 中,因此它可以在應用程序的任何位置即時訪問。

暫無
暫無

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

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