简体   繁体   English

如果已经登录 ionic,则重定向用户

[英]redirect user if the already logged in ionic

I want to redirect logged in user if the comeback to my app the will redirected to home not to log in page.如果返回到我的应用程序,我想重定向登录的用户,将重定向到主页而不是登录页面。

in my login function like this在我这样的登录功能中

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');
      })
    })
  }

i have user data in my localStorage, i think i can check whether the user token is exist on localStorage or not.我的localStorage中有用户数据,我想我可以检查localStorage上是否存在用户令牌。 but when i try this in my login page但是当我在登录页面尝试这个时

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;
      }    
    });
  }

it doesn't redirected to home, did anyone know exactly how to do that?它没有重定向到家,有没有人知道如何做到这一点?

after trying to insert to app.component.ts my code like this尝试插入 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;
        }
      });
    });
  }

above code give me this error上面的代码给了我这个错误

错误

heres my app-routing.module这是我的 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 {}

尝试将它放在app.component.ts之后app.component.ts constractor() { here your code }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM