【发布时间】:2016-10-28 05:19:44
【问题描述】:
我想知道如何将变量从 Node / Express 传递到我的 AngularJS 前端。特别是,如果我使用 Express (Passport) 进行身份验证,如何传递用户名和信息以显示在我的应用角落的“注销”按钮旁边?
这是我的身份验证路由代码。我应该为此使用 URL 变量吗?还是我应该尝试以某种方式实现一个单独的端点来传递这一点信息?
澄清一下,一旦用户登录并被定向到“/”,AngularJS 前端就会接管并在其内部进行路由。
var express = require('express');
var router = express.Router();
module.exports = function(passport){
var isAuthenticated = function (req, res, next) {
// if user is authenticated in the session, call the next() to call the next request handler
// Passport adds this method to request object. A middleware is allowed to add properties to
// request and response objects
if (req.isAuthenticated()){
//console.log(next());
return next();
}
// if the user is not authenticated then redirect him to the login page
res.redirect('/login');
}
/* GET login page. */
router.get('/login', function(req, res) {
// Display the Login page with any flash message, if any
res.render('login', { message: req.flash('message') });
});
/* Handle Login POST */
router.post('/login', passport.authenticate('login', {
successRedirect: '/',
failureRedirect: '/login',
failureFlash : true
}));
/* GET Registration Page */
router.get('/signup', function(req, res){
res.render('register',{message: req.flash('message')});
});
/* Handle Registration POST */
router.post('/signup', passport.authenticate('signup', {
successRedirect: '/',
failureRedirect: '/signup',
failureFlash : true
}));
/* GET Home Page when logged in */
router.get('/', isAuthenticated, function(req, res){
res.render('index', { user: req.user });
});
/* Handle Logout */
router.get('/signout', function(req, res) {
req.logout();
res.redirect('/login');
});
return router;
}
【问题讨论】:
标签: javascript angularjs node.js passport.js