登录后Angularfire2重定向 [英] Angularfire2 redirect after login
问题描述
我正在使用 ionic 2 rc0 和 angular 2,我刚刚添加了 angularfire2 以使用 firebse 身份验证.
i'm using ionic 2 rc0 and angular 2 and i just added angularfire2 to use firebse auth.
我已经完成所有配置和测试(我只看到我的用户登录了 firebase 控制台)但我想在登录后重定向到其他页面.
I have all configured and tested (I just see my user logged in the firebase console) but i want to redirect to other page after login.
我的登录代码:
registerUserWithGoogle() {
console.log('Google');
this.af.auth.login();
}
我的 Firebase 配置是:
My firebase config is:
export const myFirebaseAuthConfig = {
provider: AuthProviders.Google,
method: AuthMethods.Redirect
};
那么有没有办法在方法login()之后重定向?
So there is a way to redirect after the method login()?
推荐答案
AngularFire2 在 AngularFireAuth
中有一个名为 authState
的属性,您可以订阅该属性以获取 auth 状态变化
AngularFire2 has a property named authState
in AngularFireAuth
, to which you can subscribe to get the auth state changes
这是处理用户身份验证的推荐方法,因为您将订阅身份验证状态的更改,其他方法可能无法按预期工作,例如用户刷新页面时用户对象可能不存在.
This is recommended way to handle user auth, because you will subscribe to changes in the auth state, other methods might not work as expected, like when user refreshes the page user object might not exist.
import { AngularFireAuth } from 'angularfire2/auth';
@Component({
templateUrl: 'app.html'
})
export class AppComponent {
constructor(public afAuth: AngularFireAuth) {}
ngOnInit() {
this.afAuth.authState.subscribe(user => {
if (user) {
// go to home page
} else {
// go to login page
}
});
}
}
注意:您不需要在每个页面上都订阅.仅订阅 AppComponent
或您的应用特定根组件.
Note: You don't need to subscribe on every page. Only subscribe in AppComponent
or your app specific root component.
这篇关于登录后Angularfire2重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!