如何将jwt令牌从后端NodeJ传递到前端angularjs
我创建了一个jwt令牌并存储在数据库中。登录成功后,我想用令牌重定向到主页。我怎么能那样做呢 我的Nodejs文件如何将jwt令牌从后端NodeJ传递到前端angularjs,angularjs,node.js,express,Angularjs,Node.js,Express,我创建了一个jwt令牌并存储在数据库中。登录成功后,我想用令牌重定向到主页。我怎么能那样做呢 我的Nodejs文件 app.post('/authenticate', function(req, res, next) { User.findOne({name: req.body.name}, function(err, user) { if (err) {throw err;} if (!user) { res.json({ suc
app.post('/authenticate', function(req, res, next) {
User.findOne({name: req.body.name}, function(err, user) {
if (err) {throw err;}
if (!user) {
res.json({ success: false, message: 'Authentication failed. User not found.' });
} else if (user) {
// check if password matches
if (user.password !== req.body.password) {
res.json({ success: false, message: 'Authentication failed. Wrong password.' });
} else {
// if user is found and password is right
// create a token
var token = jwt.sign(user, app.get('superSecret'), {
expiresIn: 1440 // expires in 24 hours
});
// return the information including token as JSON
res.json({
success: true,
message: 'Enjoy your token!',
token: token
});
}
}
});
});
我的angularjs文件
var app = angular.module('loginApp', []);
// Controller function and passing $http service and $scope var.
app.controller('loginCtrl', function($scope, $http) {
// create a blank object to handle form data.
$scope.user = {};
// calling our submit function.
$scope.submitForm = function() {
// Posting data to file
$http.post('/tokken/login/', $scope.user).then(function (response) {
//$http.defaults.headers.common['Token'] = token
if (response.errors) {
// Showing errors.
$scope.errorName = response.errors.name;
$scope.erroPassword = response.errors.password;
} else {
$scope.message = response.data;
}
});
};
});
我在同一个登录页面中打印令牌值。我想用令牌重定向到另一个页面您是否尝试过:res.redirect('/homepage?token='+token);如何从angular、form post或ajaxadded my AngularJS调用authenticate函数您是否尝试过:res.redirect('/homepage?token='+token);如何从angular、form post或ajaxadded my angularjs调用authenticate函数