无法获得快递服务器发送的Cookie

时间:2018-09-26 11:45:38

标签: javascript express cookies passport.js session-cookies

我有一个奇怪的问题。我无法在客户端javascript中获取cookie,我将使用它来检查客户端登录的天气。我在服务器和护照中使用cookie-session。这可能是一个简单的问题,但这是我第一次尝试。我也在互联网上进行搜索,是的,有很多获取Cookie的功能,但似乎没有任何作用。

这是我的浏览器窗口的屏幕截图: enter image description here

这是我的expressjs代码,也许我对它有误解。

const express = require('express');
const passport = require('passport');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const cookieSession = require('cookie-session');
// const path = require('path');

const keys = require('./config/keys');
require('./models/User');
require('./services/passport');

mongoose.connect('mongodb://127.0.0.1:27017/eventbrite');

const app = express();

app.use(cookieSession({
  maxAge: 30 * 24 * 60 * 60 * 1000,
  keys: [keys.cookieKey]
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

require('./routes/authRoutes')(app);
const port = process.env.PORT || 5000;

app.listen(port, () => {
  console.log(`Server is up on port ${port}`);
});

这是我的passwordsjs配置:

const passport = require('passport');
const EventbriteStrategy = require('passport-eventbrite-oauth').OAuth2Strategy;
const mongoose = require('mongoose');
const keys = require('../config/keys');

const User = mongoose.model('users');

passport.serializeUser((user, done) => {
  done(null, user.accessToken);
});

passport.deserializeUser((token, done) => {
  User.findOne({ accessToken: token })
    .then((user) => {
      done(null, user);
    });
});

passport.use(new EventbriteStrategy({
  clientID: keys.eventbriteClientID,
  clientSecret: keys.eventbriteClientSecret,
  callbackURL: 'http://localhost:3000/auth/eventbrite/callback'
},
((accessToken, refreshToken, extraParams, profile, done) => {
  User.findOne({ eventbriteId: profile.id })
    .then((existingUser) => {
      if (existingUser) {
        done(null, existingUser);
      } else {
        new User({ eventbriteId: profile.id, accessToken, name: profile.displayName })
          .save()
          .then(user => done(null, user));
      }
    });
  console.log(profile, 'profile');
}
)));

0 个答案:

没有答案