【发布时间】:2017-12-17 19:01:19
【问题描述】:
我正在使用 signInWithGoogle 登录,同时检查用户是否已存在于数据库中。如果没有,则将用户信息添加到数据库中。
现在我想导航到新用户的 survey 页面和现有用户的 profile 页面。对于新用户,用户信息会添加到数据库中,但也会导航到 profile 页面。我希望新用户导航到survey 页面。
import { Component } from '@angular/core';
import { AngularFireAuth } from 'angularfire2/auth';
import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database';
import { Router } from '@angular/router';
export class LoginComponent {
users: FirebaseListObservable<User[]>;
constructor(
private db: AngularFireDatabase,
public afAuth: AngularFireAuth,
private router: Router
) {
this.users = this.db.list('/users') as FirebaseListObservable<User[]>;
}
signInWithGoogle() {
this.afAuth.auth.signInWithPopup(new firebase.auth.GoogleAuthProvider())
.then((data) => {
// Checking if the email exists in the database
this.users.subscribe(users => {
let exists = users.some(function(el) {
return el.email === data.user.email;
});
// If email does not exists in the database i.e. New user
if(!exists) {
this.router.navigate(['/survey']);
this.users.push({
email: data.user.email
})
return false;
}
else {
// If user already exists
this.router.navigate(['/profile']);
return false;
}
})
})
}
}
感谢任何帮助。谢谢
【问题讨论】:
-
您可以使用路由器解析并检查用户是否存在,如果不导航到调查,则转到配置文件
-
@RahulSingh 感谢您的评论。你能告诉我在这种情况下如何实现路由器解析吗?
-
我发现这个问题即使使用路由器解决它也不起作用,因为当您推送到可观察的火力基地时,会再次调用整个订阅,从而呈现整个 if case for the route useless 。我正在研究它会更新你的相同
标签: angular firebase firebase-realtime-database firebase-authentication angularfire2