如果重新回到我的应用程序,我将重定向已登录的用户,它将重定向到首页而不登录页面。
在我的登录功能中
login(){
this.googlePlus.login({
'webClientId' : '927898787134-spvfdmvm9apq0e1fo2efvvura8vqpid8.apps.googleusercontent.com',
'offile' : true
}).then(res=>{
firebase.auth().signInWithCredential(firebase.auth.GoogleAuthProvider.credential(res.idToken))
.then(suc=>{
// console.log('users', JSON.stringify(suc));
localStorage.setItem('user', JSON.stringify(suc));
this.router.navigate(["/tabs/home"]);
}).catch(ns=>{
alert('Unsucessful');
})
})
}
我的localStorage中有用户数据,我想我可以检查localToken上是否存在用户令牌。但是当我在登录页面上尝试
ionViewWillEnter() {
firebase.auth().onAuthStateChanged(function(user) {
if (!user) {
console.log("user is not logged in");
} else {
console.log("user is logged in");
this.router.navigateByUrl('/tabs/home');
return;
}
});
}
它没有重定向到家庭,有人知道该怎么做吗?
在尝试将我的代码插入到app.component.ts之后
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.overlaysWebView(true);
this.statusBar.show();
firebase.auth().onAuthStateChanged(function(user) {
if (!user) {
console.log("user is not logged in");
} else {
console.log("user is logged in");
this.router.navigateByUrl('/home');
return;
}
});
});
}
上面的代码给我这个错误
这是我的app-routing.module
const routes: Routes = [
{
path: '',
loadChildren: () => import('./tabs/tabs.module').then(m => m.TabsPageModule)
},
{ path: 'login', loadChildren: './login/login.module#LoginPageModule' },
{ path: 'welcome', loadChildren: './welcome/welcome.module#WelcomePageModule' },
{ path: 'settings', loadChildren: './settings/settings.module#SettingsPageModule' }
];
@NgModule({
imports: [
RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
],
exports: [RouterModule]
})
export class AppRoutingModule {}
答案 0 :(得分:0)
尝试将其放在app.component.ts
之后的constractor() { here your code }