Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angular 2 Facebook身份验证_Javascript_Node.js_Facebook_Angular_Passport.js - Fatal编程技术网

Javascript Angular 2 Facebook身份验证

Javascript Angular 2 Facebook身份验证,javascript,node.js,facebook,angular,passport.js,Javascript,Node.js,Facebook,Angular,Passport.js,我正在尝试从angular 2应用程序中实现facebook AuthenticationThion,但我认为我的实现方式是错误的,我找不到有关这方面的教程,我也想问一下,Auth0是否是实现这一点的唯一方法?。 我有这个: 节点JS服务器: // FB authentication authRoutes.get('/facebook', passport.authenticate('facebook', { session : false, scope: ['email'] })) auth

我正在尝试从angular 2应用程序中实现facebook AuthenticationThion,但我认为我的实现方式是错误的,我找不到有关这方面的教程,我也想问一下,Auth0是否是实现这一点的唯一方法?。 我有这个:

节点JS服务器:

//  FB authentication
authRoutes.get('/facebook', passport.authenticate('facebook', { session : false, scope: ['email'] }))
authRoutes.get('/facebook/callback', passport.authenticate('facebook', { session : false, failureRedirect: '/'}), ctrlAuthentication.facebookResponse)
这些是api中的路由,我使用的是passportjs,当我在浏览器中直接调用api(如:localhost:3000/api/auth/facebook)时,所有这些都可以工作,并且我从api中以JSON格式返回用户

在前面我有角2,这是 authentication.service.ts:

import {User} from '../_models/user'

@Injectable()
export class AuthenticationService {
public token : String;
public user : User;

constructor(private http: Http){
}

fbLogin() : Observable<User> {
    let user = this.http.get('/api/auth/facebook')
        .map(this.mapUser);
    return user;
 }

mapUser(response : Response) : User {
    console.log(response)
    let data = response.json();
    this.token = data.token;
    let user = <User>(data.user);
    this.user = user;

    localStorage.setItem('currentUser', JSON.stringify({ user: user, token: data.token }));
    return user;
}
}
从应用程序调用fbLogin()时,出现以下错误:

SyntaxError: Unexpected token < in JSON at position 0
MapSubscriber.AuthenticationService.mapUser [as project] (authentication.service.ts:46)
SyntaxError:JSON中位置0处的意外标记<
MapSubscriber.AuthenticationService.mapUser[作为项目](authentication.service.ts:46)
我认为这是因为我没有从我的api中获取用户,我从facebook中获取响应,而这并不是在执行什么


谢谢你的回答,对不起我的英语。

控制台日志(响应)打印什么?还有,什么是用户?一个界面还是一个类?它打印:_body:whit html代码,ok:true,status:200,statusText:ok和url:带有facebook url,我认为这是facebook页面中授权应用程序应该呈现的页面,但它不呈现,而User是一个包含_id、电子邮件、角色和个人资料的类,我在api中构建这个结构并发送类似的响应。响应对象中有用户属性吗?因为这就是相关内容应该发生的地方:)我添加了一个关于响应的图像,它没有关于用户的任何属性。谢谢你的回答!我认为你应该使用facebook认证api,我不确定你期望的是什么,但你可能没有得到想要的答案。检查:console.log(响应)打印什么?还有,什么是用户?一个界面还是一个类?它打印:_body:whit html代码,ok:true,status:200,statusText:ok和url:带有facebook url,我认为这是facebook页面中授权应用程序应该呈现的页面,但它不呈现,而User是一个包含_id、电子邮件、角色和个人资料的类,我在api中构建这个结构并发送类似的响应。响应对象中有用户属性吗?因为这就是相关内容应该发生的地方:)我添加了一个关于响应的图像,它没有关于用户的任何属性。谢谢你的回答!我认为你应该使用facebook认证api,我不确定你期望的是什么,但你可能没有得到想要的答案。选中此项:
SyntaxError: Unexpected token < in JSON at position 0
MapSubscriber.AuthenticationService.mapUser [as project] (authentication.service.ts:46)