2016-03-21 17 views
2

を望んでいない新しいセッションを作成します私のapiのルートを定義しますが、たとえば "/ api/myroute"を呼び出すと、エクスプレスのルーターは他のセッションを作成してユーザーを失いますので、私の関数isLoggedInはこれにユーザーがいないのでコントローラの呼び出しをブロックします新しいセッション。だから私の質問です:なぜルータは、セッションを再作成しますか?私の設定で何が間違っていますか?ここに私のコードは次のとおりです。Expressルータは、私がこのチュートリアルに基づいて認証部に急行し、パスポート、現地を使用して反応させるプロジェクトをやっている

//server.js  
var path = require('path'); 
    var webpack = require('webpack'); 
    var webpackDevMiddleware = require('webpack-dev-middleware'); 
    var webpackHotMiddleware = require('webpack-hot-middleware'); 
    var config = require('./webpack.config'); 

    var express = require('express'); 
    var app  = express(); 
    var port  = process.env.PORT || 8080; 
    var mongoose = require('mongoose'); 
    var passport = require('passport'); 
    var flash = require('connect-flash'); 

    var morgan  = require('morgan'); 
    var cookieParser = require('cookie-parser'); 
    var bodyParser = require('body-parser'); 
    var session  = require('express-session'); 

    var configDB = require('./config/database.js'); 

    var router = express.Router(); 

    // configuration =============================================================== 
    mongoose.connect(configDB.url); // connect to our database 

    require('./config/passport')(passport); // pass passport for configuration 

    // set up our express application 
    app.use(morgan('dev')); // log every request to the console 
    app.use(cookieParser()); // read cookies (needed for auth) 
    app.use(bodyParser()); // get information from html forms 

    app.set('view engine', 'ejs'); // set up ejs for templating 

    // required for passport 
    app.use(session({ secret: 'secret' })); // session secret 
    app.use(passport.initialize()); 
    app.use(passport.session()); // persistent login sessions 
    app.use(flash()); // use connect-flash for flash messages stored in session 


    var compiler = webpack(config); 
    app.use(webpackDevMiddleware(compiler, { noInfo: true, publicPath: config.output.publicPath })); 
    app.use(webpackHotMiddleware(compiler)); 
    app.use('/plugins/bootstrap', express.static(path.join(__dirname, './plugins/bootstrap'))); 
    app.use('/plugins/jquery', express.static(path.join(__dirname, './plugins/jquery'))); 
    app.use('/plugins/font-awesome', express.static(path.join(__dirname, './plugins/font-awesome'))); 


    // all of our routes will be prefixed with /api 
    app.use('/api', router); 

    // routes ====================================================================== 
    require('./api/routes.js')(app, passport,router); // load our routes and pass in our app and fully configured passport 


    // launch ====================================================================== 
    app.listen(port, function(error) { 
     if (error) { 
     console.error(error) 
     } else { 
     console.info("==> Listening on port %s. Open up http://localhost:%s/ in your browser.", port, port) 
     } 
    }); 
    console.log('The magic happens on port ' + port); 

そして、ここでは私のroutes.jsです:ログイン、ログアウトのための

module.exports = function(app, passport,router) { 
    var MyController = require("../api/controllers/mycontroller"); 


    // ===================================== 
    // HOME PAGE (with login links) ======== 
    // ===================================== 
    app.get('/', function(req, res) { 
     console.log("welcome") 
     res.render('index.ejs', { message: ""}); // load the index.ejs file 
    }); 

    // ===================================== 
    // LOGIN =============================== 
    // ===================================== 
    // show the login form 
    app.get('/login', function(req, res) { 
     console.log("login") 
     // render the page and pass in any flash data if it exists 
     res.render('index.ejs', { message: req.flash('loginMessage') }); 
    }); 

    app.post('/login', passport.authenticate('local-login', { 
     successRedirect : '/welcome', // redirect to the secure profile section 
     failureRedirect : '/login', // redirect back to the signup page if there is an error 
     failureFlash : true // allow flash messages 
    })); 

    // ===================================== 
    // SIGNUP ============================== 
    // ===================================== 
    // show the signup form 
    app.get('/signup', function(req, res) { 
     console.log("signup") 
     // render the page and pass in any flash data if it exists 
     res.render('signup.ejs', { message: req.flash('signupMessage') }); 
    }); 

    // process the signup form 
    app.post('/signup', passport.authenticate('local-signup', { 
     successRedirect : '/welcome', // redirect to the secure profile section 
     failureRedirect : '/signup', // redirect back to the signup page if there is an error 
     failureFlash : true // allow flash messages 
    })); 

    // process the signup form 
    // app.post('/signup', do all our passport stuff here); 

    // ===================================== 
    // LOGOUT ============================== 
    // ===================================== 
    app.get('/logout', function(req, res) { 
     console.log("logout") 
     req.logout(); 
     res.redirect('/'); 
    }); 

    router.get('/myroute', isLoggedIn, function(req, res) { 
     //HERE the router recreate a session and req.user is now undefined why ? 
     MyController.getAllObjects(req,res); 
    }); 


    // ===================================== 
    // HOME SECTION ===================== 
    // ===================================== 
    // we will want this protected so you have to be logged in to visit 
    // we will use route middleware to verify this (the isLoggedIn function) 
    app.get('*', isLoggedIn, function(req, res) { 
     console.log("HOME") 
     res.render('home.ejs', { 
      user : req.user // get the user out of session and pass to template 
     }); 
    }); 
}; 

// route middleware to make sure a user is logged in 
function isLoggedIn(req, res, next) { 

    // if user is authenticated in the session, carry on 
    if (req.isAuthenticated()) 
     return next(); 

    console.log("redirect because not log in") 
    // if they aren't redirect them to the home page 
    res.redirect('/'); 

} 

機能、サインイン...チュートリアルのコピーペーストです。

+0

サーバーの設定が正常になっています。クライアントからの呼び出しを表示できますか? – Cithel

+1

はいそれこのようなものだ: リターン( '/ API/myroute')をフェッチ .then(応答=> response.json()) .then(JSON => { ディスパッチ({ タイプ:types.MY_ACTION、 フォーム:json、 receivedAt:Date.now() }) } – fandro

答えて

2

あなたはフェッチライブラリを使用しているようです。このライブラリの順序であなたの要求を動作するので、デフォルトでは、ドメインのクッキーが含まれていませんが、このようになります。

fetch('/api/myroute', { 
    credentials: 'same-origin' // This is the line you are missing 
}).then(response => response.json()) 
.then(json => { 
    dispatch({ 
     type: types.MY_ACTION, forms: json, receivedAt: Date.now() 
    }) 
}) 

は、より多くの情報のためthis linkを参照してください。

+1

はい、動作します。どうもありがとう ! – fandro

関連する問題