如何从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:
有人可以阐明我如何从该方法返回true/false吗?如果有人能看到更干净的方法,我将非常感激.
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屋!