Angularfire2在登录后重定向 [英] Angularfire2 redirect after login
问题描述
我已经配置并测试了所有的功能(我只是看到我的用户登录了Firebase控制台),但是我想在登录后重定向到其他页面。
$ b
我的登录代码:
registerUserWithGoogle(){
console.log '谷歌');
this.af.auth.login();
$ / code>
我的firebase配置为:
export const myFirebaseAuthConfig = {
provider:AuthProviders.Google,
method:AuthMethods.Redirect
};
所以在login()方法之后有一个重定向的方法? AngularFire2具有 FirebaseAuth
,您可以订阅auth状态更改
这是处理用户身份验证的推荐方式,因为您将订阅auth状态中的更改,其他方法可能无法按预期方式工作,例如用户刷新页面用户对象时可能不存在。 / p>
从'angularfire2'导入{FirebaseAuth};
$ b @Component({
templateUrl:'app.html'
})
导出类AppComponent {
构造函数(public auth:FirebaseAuth){
}
ngOnInit(){
this.auth.subscribe(user => {
if(user){
//转到主页
}其他{
//转到登录页面
}
});
$ b 注意: strong>您无需在每个页面上订阅。只订阅 AppComponent
或您的应用特定的根组件。
i'm using ionic 2 rc0 and angular 2 and i just added angularfire2 to use firebse auth.
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.
My code for login:
registerUserWithGoogle() {
console.log('Google');
this.af.auth.login();
}
My firebase config is:
export const myFirebaseAuthConfig = {
provider: AuthProviders.Google,
method: AuthMethods.Redirect
};
So there is a way to redirect after the method login()?
解决方案 AngularFire2 has FirebaseAuth
, to which you can subscribe to 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 { FirebaseAuth } from 'angularfire2';
@Component({
templateUrl: 'app.html'
})
export class AppComponent {
constructor(public auth: FirebaseAuth) {
}
ngOnInit() {
this.auth.subscribe(user => {
if (user) {
// go to home page
} else {
// go to login page
}
});
}
}
Note: You don't need to subscribe on every page. Only subscribe in AppComponent
or your app specific root component.
这篇关于Angularfire2在登录后重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!