【发布时间】:2017-04-05 10:41:59
【问题描述】:
我正在学习 NativeScript/Angular 2,需要就这个问题寻求帮助。
为了在 Firebase 平台内实现多角色登录系统,我想到了这个解决方案
- 通过 Firebase 身份验证登录用户
- 在
/stores/路径中查询store,其中merchantEmail字段与刚刚登录的电子邮件相同 - 如果我找到它,我在
BackendService服务中设置storeID,该服务使用getString/setString来存储令牌,然后路由到MerchantDashboardComponent - 如果我没有找到它,只需路由到
BuyerDashboardComponent
这是我在login.service中的代码的一部分:
login (email: string, password: string) {
return firebase.login({
type: firebase.LoginType.PASSWORD,
email: email,
password: password
}).then(
(result: any) => {
firebase.query(
(_result) => { // Here I set BackendService.storeID
在.query() 回调中,我正在分配我在应用程序中需要的令牌。
这是我在login.component 中使用的方法:
doLogin () {
this.isAuthenticating = true;
if (!this.validateEmail()) {
alert("Please insert a valid email");
return false;
}
this.loginService.login(this.email, this.password).then(
() => {
this.isAuthenticating = false;
if (BackendService.loginError)
alert(BackendService.loginError)
else if (BackendService.storeID != '') {
this.router.navigate(['/merchant-dashboard'], {clearHistory: true});
}
else {
this.router.navigate(['/home/categories'], {clearHistory: true});
}
}
);
}
一切正常,除了商家被路由到买家仪表板这一事实。我已经设法发现执行顺序实际上并不是我所期望的:
-
firebase.login()被执行并返回一个 Promise -
.then()处理程序在doLogin()方法内执行 - 只有在这之后,
firebase.query()方法才完成回调并且我的令牌可用,但doLogin()已经导航用户,因为当我需要它时storeID仍然是空的
我希望我尽可能清楚。 感谢您的关注。
您好, 大卫
【问题讨论】:
标签: angularjs angular firebase nativescript