使用parse-server,node.js和express的会话令牌无效

时间:2016-11-12 18:01:22

标签: javascript node.js express session-cookies parse-server

我正在尝试从网站登录用户。我正在使用Microsoft Azure托管的parse-server。我一直收到以下错误,只是尝试访问主页:

Error handling request: ParseError { code: 209, message: 'invalid session token' } code=209, message=invalid session token

浏览器会抛出“......重定向你太多次了。”错误。我不确定我做错了什么,我已经尝试过从这里开始研究和拼凑:https://github.com/ParsePlatform/parse-server/issues/497没有运气。

index.js

var express...
etc...

var jsonParser = bodyParser.json();
var urlencodedParser = bodyParser.urlencoded({extended:false});

//Server configuration
...

//Express configuration
var app = express();

app.use(cookieParser()); // read cookies (needed for auth)

// get information from html forms
app.use(bodyParser.urlencoded({
  extended: true
}));

app.use(bodyParser.json());

// app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use('/public', express.static(__dirname + '/public'));
app.use('/parse', new ParseServer(config.server));
app.use('/parse-dashboard', ParseDashboard(config.dashboard, true));

app.use(cookieSession({
  name: "COOKIE_NAME",
  secret: "COOKIE_SECRET",
  maxAge: 15724800000
}));

app.use(function (req, res, next) {
    Parse.Cloud.httpRequest({
      url: 'https://localhost:1337/parse/users/me',
      headers: {
        'X-Parse-Application-Id': process.env.APP_ID,
        'X-Parse-REST-API-Key': process.env.API_KEY,
        'X-Parse-Session-Token': req.session.token
      }
    }).then(function (userData) {
        req.user = Parse.Object.fromJSON(userData.data);
        next();
    }).then(null, function () {
        return res.redirect('/login');
    });
});

app.use(flash()); // use connect-flash for flash messages stored in session

//routes
require('./routes/routes.js')(app);


app.listen(process.env.PORT || url.parse(config.server.serverURL).port, function () {
  console.log(`Parse Server running at ${config.server.serverURL}`);
});

routes.js

// app/routes.js
var bodyParser = require('body-parser');
var jsonParser = bodyParser.json();
var urlencodedParser = bodyParser.urlencoded({extended:false});


module.exports = function(app) {


    // HOME PAGE ========

    app.get('/', function(req, res) {
        res.render('index.ejs', { title: 'Audiomesh' }); // load the index.ejs file
    });


    // LOGIN ===============================

    // show the login form
    app.get('/login', function(req, res) {

        res.render('login.ejs', { message: req.flash('loginMessage') }); 
    });


    app.post('/login', function(req, res) {
        Parse.User.logIn(req.body.username, req.body.password).then(function(user) {
          req.session.user = user;
          req.session.token = user.getSessionToken();
          res.redirect('/dashboard');
        }, function(error) {
          req.session = null;
          res.render('login', { flash: error.message });
        });
  });


    // DASHBOARD =====================

    app.get('/dashboard', function(req, res) {
        res.render('dashboard.ejs', {
            user : req.user // get the user out of session and pass to template
        });
    });


    // LOGOUT ==============================

    app.get('/logout', function(req, res) {
        req.logout();
        res.redirect('/');
    });

};

login.ejs

    <body>

      <p><%= message %></p>
      <form name="loginForm" action="/login" method="post">
        <label>Email</label>
        <input type="email" name="email"></input>
        <label>Password</label>
        <input name="password" type="password"></input>
        <input class="button" type="submit" value="Log In">
      </form>

        <p>Coming soon azure</p>
        <p>Back to <a href="/">home page</a></p>
    </body>

我的网站的目标是让主页成为移动应用的广告/目标网页。因此,如果您已登录,则此处没有任何证据。单击“登录”后,它将检查用户是否已登录并加载其仪表板(如果为true)或登录页面(如果为false)。

现在的问题是我甚至无法加载主页。我得到了太多的重定向。

0 个答案:

没有答案