2017-09-08 8 views
0

ionViewWillEnterを使用して、ユーザーがログインしているかどうかを確認しようとしています。ページ。次に、intializeapp関数を呼び出す必要があります。私はAngularとIonicにかなり新しいので、どんなヒントも役に立ちます。 `ユーザーがログインしているかどうかを確認しようとしていますが、ログインページにプッシュしないと表示されます

ionViewCanEnter() { 
    console.log("1"); 
    console.log(this.userSevice.isUserLoggedIn()); 
    if (this.userSevice.isUserLoggedIn() === false){ 
     this.nav.push(LoginPage); 
     alert("user is logging in"); 
    } 
    else{ 
     this.initializeApp(); 
    } 
    }` 

答えて

1

ありGuardと呼ばれる角度の美しい概念であり、あなたが角度のアプリケーションであなたの全体のログインフローを処理するためにそれを使用することができます。

authガードを作成してルータやどこにでも置くことができ、ログインしていない場合やサイトのプライベートエリアにアクセスしようとすると、ユーザを強制的にログインページに誘導できます。

これは単純な認証ガード

@Injectable() 
export class AuthGuard implements CanActivate { 

    constructor(private router: Router, private authService: AuthService) { } 

    canActivate(): Observable<boolean> | boolean { 
    // here check if this is first time call. If not return 
    // simple boolean based on user object from authService 
    // otherwise: 

    return this.authService.getAuthenticated.map(user => { 
      this.authService.setUser(user); 
      return user ? true : false; 
    }) 

    } 
} 

とルーターで、あなたはこの

const routes: Routes = [ 
    { 
    path: '', 
    canActivate: [AuthGuardService], 
    runGuardsAndResolvers: 'always', 
    } 
    ... 
] 
のように使用することができますです
関連する問題