如何从 Angularfire2 facebook 身份验证返回 true 或 false [英] how to return true or false from Angularfire2 facebook Authentication
问题描述
好的,我对 AngularFire2
和 Angular2
还很陌生,我正在学习 AngularFire2
提供的身份验证方法.
Ok so I'm pretty new to AngularFire2
and Angular2
in general, I'm learning the authentication method that AngularFire2
provides.
到目前为止,我已经成功地实现了使用 Facebook
的登录,我的代码目前驻留在一个 service
中.
So far I have successfully implemented logging in with Facebook
, my code currently resides in a service
.
所以代码很简单(你可能看起来很恶心),我通过 Facebook
登录,检索用户访问令牌和他们的 facebookId,调用 Facebook 图并检索名字、姓氏、性别,电子邮件等然后将这些数据保存在 Firebase
这是我执行上述操作的代码:
So the code is simple (May look disgusting to you), I login via Facebook
retrieve the users access token, and their facebookId, make a call to Facebook graph and retrieve Firstname, Lastname, Gender, Email etc then save this data within Firebase
this is my code for doing the above:
loginWithFacebook(): FirebaseListObservable<string> {
return FirebaseListObservable.create(obs => {
this.af.auth.login({
provider: AuthProviders.Facebook,
method: AuthMethods.Popup,
scope: ['public_profile']
}).then((authState: any) => {
let userRef = this.af.database.object('/users/' + authState.uid); // get user profile
userRef.subscribe(user => {
let url = `https://graph.facebook.com/v2.8/${user.facebookId}?fields=first_name,last_name,gender,email&access_token=${user.accessToken}`;
this.http.get(url).subscribe(response => {
let result = response.json()
userRef.update({
first_name: result.first_name,
last_name: result.last_name,
gender: result.gender,
email_address: result.email,
facebookId: user.facebookId
})
})
})
obs.next(userRef);
}).catch(err => {
obs.throw(err)
});
});
我从我的 login.component
调用它,如下所示:
I call this from my login.component
as shown:
loginFacebook() {
this.loginSubscription = this.service.loginWithFacebook().subscribe(data => {
console.log('after finish', data);
});
}
我的问题是我想捕获如果使用 facebook 登录失败或通过我已经尝试在 Service 方法中填充 Observable
,但是当我在 console.log '完成后',数据我看到以下内容:
Issue I have is I want to capture if the login with facebook fails or passes I've tried populating the Observable
within the Service method, however when I console.log 'after finish', data I see the following:
有人能解释一下我如何从这种方法返回真/假吗?如果有人能看到一种更简洁的方法,我将不胜感激.
Can someone shed any light into how I go about returning true / false from this method? If anyone can see a cleaner way of doing this I would be highly appreciative.
推荐答案
现在无法测试,你可以这样做:
Cannot test right now, you could do it like this:
loginWithFacebook(): Observable<boolean> {
return this.af.auth.login({
provider: AuthProviders.Facebook,
method: AuthMethods.Popup,
scope: ['public_profile']
})
.map((authState: any) => {
if (!authState) return false;
let userRef = this.af.database.object('/users/' + authState.uid); // get user profile
userRef.subscribe(user => {
let url = `https://graph.facebook.com/v2.8/${user.facebookId}?fields=first_name,last_name,gender,email&access_token=${user.accessToken}`;
this.http.get(url).subscribe(response => {
let result = response.json();
userRef.update({
first_name: result.first_name,
last_name: result.last_name,
gender: result.gender,
email_address: result.email,
facebookId: user.facebookId
})
})
})
return true;
}).catch(err => {
return Observable.of(false);
});
}
loginFacebook() {
this.loginSubscription = this.service.loginWithFacebook().subscribe(data => {
console.log('after finish', data);
});
}
这篇关于如何从 Angularfire2 facebook 身份验证返回 true 或 false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!