Angular2和ExpressJS API - 登录的会话不起作用

时间:2016-11-08 22:36:18

标签: facebook api express angular passport.js

基本上我已经编写了Angular2前端客户端,它包含一个按钮,单击此按钮时,它使用以下函数向expressJS和passportJS api发出非常简单的请求:

socialLogin(){
    window.location.href='http://api.example.io/auth/facebook'; 
    console.log('social');
   this.authenticationService.getProjectsMain();
}

一旦完成,一切都很好,我可以访问api url:

http://api.example.io/v1/api/me

在我的浏览器中查看我的facebook数据,我的问题是,当我尝试从angular2前端服务获取此json数据时,就好像登录从未发生过,即使我在另一个选项卡中打开它并且可以看到有关API子域的数据。我正在向api url做一个get请求,它什么都没有,因为什么都没有。我猜这是一种持久性cookie,但我不确定如何将所有这些放在一起。

我的app.js服务器文件

const express          = require('express');
const passport         = require('passport');
const Strategy         = require('passport-facebook').Strategy;
const path             = require('path');
const favicon          = require('serve-favicon');
const logger           = require('morgan');
const cookieParser     = require('cookie-parser');
const bodyParser       = require('body-parser');
const routes           = require('./routes/index');
const loginfacebook    = require('./routes/login-facebook');
const callbackfacebook = require('./routes/callback-facebook');
const standardLogin    = require('./routes/standardlogin');
const me               = require('./routes/me');
const app              = express();




app.all('/*', function(req, res, next) {
    // res.header("Access-Control-Allow-Origin", "*");
    console.log('origin ' + req.headers.origin);
    res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.header("Access-Control-Allow-Origin", req.headers.origin );
    res.header('Access-Control-Allow-Credentials', true);
    res.header("Access-Control-Allow-Headers", "X-Requested-With, Content-type");
    next();
});

app.set('views', path.join(__dirname, 'views'));

app.set('view engine', 'jade');

app.use(logger('dev'));

app.use(bodyParser.json());

app.use(cookieParser());

app.use(express.static(path.join(__dirname, 'public')));

/*app.use(require('express-session')({ 
    secret: 'keyboard cat',
    resave: true,
    saveUninitialized: true
}));*/

app.use(require('express-session')({ 
    secret: 'keyboard cat',
    cookie: {
        path: '/',
        domain: '.example.io',
        maxAge: 1000 * 60 * 24, // 24 hours
    },
    resave: true, 
    saveUninitialized: true  
}));

app.use(passport.initialize());

app.use(passport.session());

app.use('/', routes);

app.use('/auth/facebook', loginfacebook);

app.use('/callback/facebook', callbackfacebook);

app.use('/v1/api/login', standardLogin);

app.use('/v1/api/me', me);

app.listen(80);

module.exports = app;

0 个答案:

没有答案