Angularfire2:AuthState更改登录的用户凭据(无需注销)。

时间:2018-03-25 12:15:58

标签: angular firebase angularfire2

Test App Web App Interface

应用程序的前端显示登录用户电子邮件:admin@admin.com,位于导航栏的右上角。这来自以下代码:

导航组件

loggedInUser;

  constructor(
    private _authService: AuthService
  ) {}

ngOnInit() {
    this._authService.getAuth().subscribe(auth => {
      if (auth) {
        this.loggedInUser = auth.email;
      }
    });
}

AuthService

constructor(
    private afAuth: AngularFireAuth,
    private _afs: AngularFirestore
  ) {}

getAuth() {
    return this.afAuth.authState.map(auth => auth);
  }

createAccount(email: string, password: string) {
     return new Promise((resolve, reject) => {
      this.afAuth.auth.createUserWithEmailAndPassword(email, password)
        .then(userData => resolve(userData), err => reject(err));
     });
   }

导航组件HTML(我已经删除了引导代码以帮助关注代码)

<nav>
    <ul class="navbar-nav ml-auto">
      <li class="nav-item dropdown">
        <a class="..">{{ loggedInUser }}</a>
        <div class="..">
          <a class=".." (click)="onLogoutClick()">Logout</a>
        </div>
      </li>
    </ul>
</nav>

当我点击创建驱动程序移动应用程序帐户时,将在上面的AuthService中在firebase中创建一个新用户帐户。 createAccount(email, password){}

问题在于,当我创建此帐户时,loggedInUser会从admin@admin.com更改为fcw@test.com,这是新创建的用户的帐户。

如果我为其他驱动程序创建另一个帐户,例如John Doe - &gt; johndoe@test.com,loggedInUser从fcw@test.com更改为johndoe@test.com

如何阻止AuthState更改loggedInUser凭据?

使用的Stack是Angular 5,Angularfire2,Firebase,如下面的package.json依赖项所示:

"dependencies": {
    "@angular-devkit/core": "0.0.29",
    "@angular/animations": "^5.1.0",
    "@angular/common": "^5.1.0",
    "@angular/compiler": "^5.1.0",
    "@angular/core": "^5.1.0",
    "@angular/forms": "^5.1.0",
    "@angular/http": "^5.1.0",
    "@angular/platform-browser": "^5.1.0",
    "@angular/platform-browser-dynamic": "^5.1.0",
    "@angular/router": "^5.1.0",
    "@types/lodash": "^4.14.96",
    "angular2-flash-messages": "^2.0.5",
    "angularfire2": "^5.0.0-rc.4",
    "bootstrap": "4.0.0-beta.2",
    "core-js": "^2.4.1",
    "firebase": "^4.9.0",
    "font-awesome": "^4.7.0",
    "jquery": "^3.3.1",
    "lodash": "^4.17.4",
    "popper.js": "^1.12.9",
    "rxjs": "^5.5.6",
    "zone.js": "^0.8.19"
  },

1 个答案:

答案 0 :(得分:0)

重复,link to SO thread已经解决,但仍然是一种非常黑客的方式,

我要做的是编写一个Cloud函数来创建这些用户,因此您实际上不会在客户端设备上创建用户。我认为这会更清洁

然后使用firbase-admin for nodejs

  admin.auth().createUser({
   email: email,
   password: password,
   displayName: `${firstName} ${lastName}`,
  })
相关问题