Angular 6 Web应用程序在使用不良Internet启动应用程序时注销用户

发布于 2025-02-04 13:17:57 字数 2169 浏览 4 评论 0原文

有时,当我的应用程序启动不良的互联网连接时,用户已登录。

我不希望这种情况发生..我希望用户继续登录,即使该应用程序由于不良的互联网连接而无法正确启动。.

"@angular/fire": "^5.1.0",

我附上了我的验证代码,也附加了我的依赖关系。.我在做什么错误的?

import { Router } from '@angular/router';
import { Injectable } from '@angular/core';
import { AngularFireAuth } from '@angular/fire/auth';
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';

@Injectable()
export class AuthService {
  private user: Observable<firebase.User>;
  public userDetails: firebase.User = null;
  userAuthDoneSubject = new Subject();

  constructor(
    private firebaseAuth: AngularFireAuth) {
    this.user = firebaseAuth.authState;

    this.user.subscribe(
      (userDetails) => {
        if (userDetails) {
          this.userDetails = userDetails;
        }
        else {
          this.userDetails = null;
        }
        this.userAuthDoneSubject.next();
      },(error) => {this.userAuthDoneSubject.error("AuthService:error authenticate"); }
    );
  }
  isLoggedIn() {
    if (this.userDetails == null ) {
      return false;
    } else {
        return true;
      }
    }
 
    logout() {
       return this.firebaseAuth.auth.signOut();
    }

  signinUser(customToken:any){
    return this.firebaseAuth.auth.signInWithCustomToken(customToken)
  }

  getJWT() {
    return this.firebaseAuth.auth.currentUser.getIdToken(true);
  }

}
"dependencies": {
  "@agm/core": "^1.0.0",
  "@angular/animations": "^6.1.0",
  "@angular/common": "^6.1.0",
  "@angular/compiler": "^6.1.0",
  "@angular/core": "^6.1.0",
  "@angular/fire": "^5.1.0",
  "@angular/forms": "^6.1.0",
  "@angular/http": "^6.1.0",
  "@angular/platform-browser": "^6.1.0",
  "@angular/platform-browser-dynamic": "^6.1.0",
  "@angular/router": "^6.1.0",
  "@types/googlemaps": "^3.30.19",
  "angular2-multiselect-dropdown": "^4.6.2",
  "core-js": "^2.5.4",
  "css-toggle-switch": "^4.1.0",
  "firebase": "^5.5.9",
  "ng-lazyload-image": "^6.1.0",
  "ngx-owl-carousel-o": "^0.1.2",
  "ngx-page-scroll-core": "^6.0.2",
  "rxjs": "^6.0.0",
  "rxjs-compat": "^6.3.3",
  "zone.js": "~0.8.26"
},

Sometimes when my app launch with a bad internet connection the user got logged out.

I don't want this to happen.. I want the user to continue being logged in even if the app didn't launch properly because of bad internet connection..

"@angular/fire": "^5.1.0",

I attached my AuthService code, also my dependencies.. what I am doing wrong?

import { Router } from '@angular/router';
import { Injectable } from '@angular/core';
import { AngularFireAuth } from '@angular/fire/auth';
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';

@Injectable()
export class AuthService {
  private user: Observable<firebase.User>;
  public userDetails: firebase.User = null;
  userAuthDoneSubject = new Subject();

  constructor(
    private firebaseAuth: AngularFireAuth) {
    this.user = firebaseAuth.authState;

    this.user.subscribe(
      (userDetails) => {
        if (userDetails) {
          this.userDetails = userDetails;
        }
        else {
          this.userDetails = null;
        }
        this.userAuthDoneSubject.next();
      },(error) => {this.userAuthDoneSubject.error("AuthService:error authenticate"); }
    );
  }
  isLoggedIn() {
    if (this.userDetails == null ) {
      return false;
    } else {
        return true;
      }
    }
 
    logout() {
       return this.firebaseAuth.auth.signOut();
    }

  signinUser(customToken:any){
    return this.firebaseAuth.auth.signInWithCustomToken(customToken)
  }

  getJWT() {
    return this.firebaseAuth.auth.currentUser.getIdToken(true);
  }

}
"dependencies": {
  "@agm/core": "^1.0.0",
  "@angular/animations": "^6.1.0",
  "@angular/common": "^6.1.0",
  "@angular/compiler": "^6.1.0",
  "@angular/core": "^6.1.0",
  "@angular/fire": "^5.1.0",
  "@angular/forms": "^6.1.0",
  "@angular/http": "^6.1.0",
  "@angular/platform-browser": "^6.1.0",
  "@angular/platform-browser-dynamic": "^6.1.0",
  "@angular/router": "^6.1.0",
  "@types/googlemaps": "^3.30.19",
  "angular2-multiselect-dropdown": "^4.6.2",
  "core-js": "^2.5.4",
  "css-toggle-switch": "^4.1.0",
  "firebase": "^5.5.9",
  "ng-lazyload-image": "^6.1.0",
  "ngx-owl-carousel-o": "^0.1.2",
  "ngx-page-scroll-core": "^6.0.2",
  "rxjs": "^6.0.0",
  "rxjs-compat": "^6.3.3",
  "zone.js": "~0.8.26"
},

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千柳 2025-02-11 13:17:57

当您将firebase用于客户端应用程序时,将加载firebase脚本,然后检查上一个会话是否存在,然后将其针对Auth Server进行验证。如果出于某种原因互联网连接失败,则此过程将无法成功,因此将不会触发该事件,这将使您的应用程序状态处于未登录状态。

您的代码基于包装Firebase功能的第三方库(我不知道其内部设备),并且可能会收听Firebase事件。

我建议您在用户登录时将firebase令牌保存在LocalStorage中,如果Internet连接失败,则可以尝试使用保存在LocalStorage中的令牌进行登录。或者,您可以收听净连接事件。请检查此 docs

When you use firebase into a client app, the firebase script will be loaded and then it will check if a previous session exists and then will validate it against auth server. If for some reason internet connection fails, this process will not succeed, so the event onAuthStateChanged will not be fired, which will cause you app state to be in a not logged in state.

Your code is based on a third party library (which I don't know its internals) that wraps firebase functionalities and probably will listen to firebase events.

I suggest you save the firebase token in localStorage when the user logs in, and in case internet connection fails, you can try to log in using the token that was saved in localStorage. Or you can listen to net connection events. Please check this docs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文