如何从angularfire2获取当前登录的auth state/user
How to get currently logged in auth state/user from angularfire2
我有一个 ionic2 应用程序,并且正在使用 Firebase 和 angularFire2。我想使用 angularFire2 从 firebase 获取当前身份验证状态和当前身份验证 object/user。
这是到目前为止的工作 - 我可以对用户进行身份验证并订阅 FirebaseAuthState 以获取 facebook 用户对象。
constructor(platform: Platform, private auth: FirebaseAuth) {
auth.subscribe((user: FirebaseAuthState) => {
if (user) {
// I could store user in localstorage, but I'd like to see an All Firebase solution
this.rootPage = TabsPage;
} else {
this.rootPage = LoginPage;
}
});
现在我可以在这里设置 localstorage 并缓存我的用户对象以记住身份验证状态。但是,我很想知道如何在不实现自己的自定义本地存储密钥的情况下仅使用 Firebase。我看到 Firebase 存储了它自己的 localStorage 密钥,所以知道它已登录。
如何从代码中获取授权对象?此外,我尝试了 AngularFire2 文档中列出的示例以在模板中呈现身份验证状态 - 但这给了我一个错误。
import {FirebaseAuth} from 'angularfire2';
@Component({
selector: 'auth-status',
template: `
<div *ng-if="auth | async">You are logged in</div>
<div *ng-if="!(auth | async)">Please log in</div>
`
})
class App {
constructor (@Inject(FirebaseAuth) public auth: FirebaseAuth) {}
}
当前身份验证状态可从注入的 FirebaseAuth
获得。您可以获得实际的身份验证数据 auth.getAuth()
参见:https://github.com/angular/angularfire2/blob/master/src/providers/auth.ts#L99
现在为了获取用户信息,您必须订阅获取授权信息。例如
constructor(public af: AngularFire) {
this.af.auth.subscribe(auth => console.log(auth));// user info is inside auth object
}
如果 auth 状态不存在,auth 对象将为 null
- 导入:
import { AngularFireAuth } from 'angularfire2/auth';
- 注入:
constructor(public afAuth: AngularFireAuth) { }
检查:
this.afAuth.authState.subscribe(res => {
if (res && res.uid) {
console.log('user is logged in');
} else {
console.log('user not logged in');
}
});
您可以简单地导入 AngularFireAuth 并执行此操作:
this.angularFireAuth.authState.subscribe(userResponse => {
if (userResponse) {
console.log('here is your user data');
localStorage.setItem('user', JSON.stringify(userResponse));
console.log(userResponse);
} else {
localStorage.setItem('user', null);
}
});
在这里,我还使用 localStorage 来保存我当前在我的应用程序上活跃的用户的所有数据。
我有一个 ionic2 应用程序,并且正在使用 Firebase 和 angularFire2。我想使用 angularFire2 从 firebase 获取当前身份验证状态和当前身份验证 object/user。
这是到目前为止的工作 - 我可以对用户进行身份验证并订阅 FirebaseAuthState 以获取 facebook 用户对象。
constructor(platform: Platform, private auth: FirebaseAuth) {
auth.subscribe((user: FirebaseAuthState) => {
if (user) {
// I could store user in localstorage, but I'd like to see an All Firebase solution
this.rootPage = TabsPage;
} else {
this.rootPage = LoginPage;
}
});
现在我可以在这里设置 localstorage 并缓存我的用户对象以记住身份验证状态。但是,我很想知道如何在不实现自己的自定义本地存储密钥的情况下仅使用 Firebase。我看到 Firebase 存储了它自己的 localStorage 密钥,所以知道它已登录。
如何从代码中获取授权对象?此外,我尝试了 AngularFire2 文档中列出的示例以在模板中呈现身份验证状态 - 但这给了我一个错误。
import {FirebaseAuth} from 'angularfire2';
@Component({
selector: 'auth-status',
template: `
<div *ng-if="auth | async">You are logged in</div>
<div *ng-if="!(auth | async)">Please log in</div>
`
})
class App {
constructor (@Inject(FirebaseAuth) public auth: FirebaseAuth) {}
}
当前身份验证状态可从注入的 FirebaseAuth
获得。您可以获得实际的身份验证数据 auth.getAuth()
参见:https://github.com/angular/angularfire2/blob/master/src/providers/auth.ts#L99
现在为了获取用户信息,您必须订阅获取授权信息。例如
constructor(public af: AngularFire) {
this.af.auth.subscribe(auth => console.log(auth));// user info is inside auth object
}
如果 auth 状态不存在,auth 对象将为 null
- 导入:
import { AngularFireAuth } from 'angularfire2/auth';
- 注入:
constructor(public afAuth: AngularFireAuth) { }
检查:
this.afAuth.authState.subscribe(res => { if (res && res.uid) { console.log('user is logged in'); } else { console.log('user not logged in'); } });
您可以简单地导入 AngularFireAuth 并执行此操作:
this.angularFireAuth.authState.subscribe(userResponse => {
if (userResponse) {
console.log('here is your user data');
localStorage.setItem('user', JSON.stringify(userResponse));
console.log(userResponse);
} else {
localStorage.setItem('user', null);
}
});
在这里,我还使用 localStorage 来保存我当前在我的应用程序上活跃的用户的所有数据。