mysql - 如何添加 session 并添加访问 token 以登录 Node

标签 mysql database node.js session authentication

我有一个登录 API,如下所示:

app.post('/login', function (req, res){

email = req.body.email;
password = req.body.password;
if(email && password )
{
console.log(email); //displays the email in the terminal
console.log(password); //displays the password in the terminal
var status = []; //array to output json

//connection.connect(); //mysql connection

connection.query('SELECT username FROM user WHERE email =? and password = ?',[email,password], function (error, rows, fields) {

if(rows.length == 1)
{
  passport.serializeUser(function(res, done) {
                    done(null,res);
                });

                passport.deserializeUser(function(id, done) {
                    done(null,res);

                });

status.push({username: rows[0].username});
username = rows[0].username;

console.log('sucessful login');
res.end('"status:"\n'+JSON.stringify(status)); //output as JSON
}

if(rows.length == 0)
{

connection.query('select * from temp_users where email = ? and password = ?',[email,password],function(error,rows,fields)
{
status = '';
if(rows.length == 1)
{
status = 'pending';
//res.end('Your Account needs to be verified');
res.end('"status:"'+JSON.stringify(status));
console.log('Your Account needs to be verified.');
}
else
{
status = 'error';
res.end('"status:"'+JSON.stringify(status));
//res.end('no such user.Please create an account');
console.log('no such user.Please create an account');
}
}
);
}


});
}
});

现在我想做的是为每个登录的用户添加一个 session ,并使该 session 在一段时间后过期。我还希望在用户登录后将访问 token 插入到我的数据库中。 有什么帮助吗?

最佳答案

这个示例正是您所寻找的。

node.js express mysql passport

关于mysql - 如何添加 session 并添加访问 token 以登录 Node ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26526977/

相关文章:

php - 写数据库时如何整合继承技术

mysql - 检索每个 ID 的最新记录。如何,在 SQL 中?

node.js - 我可以在不使用 res.render/return 信息给用户的情况下以 Express 方式渲染 html 吗?

javascript - 如何监视nodejs进程

php - 使用用户名和/或电子邮件登录

php - 在 php 中以编程方式声明和初始化变量

MySQL 太长 varchar 截断/错误设置

php - 假设数据库已建立索引,如何使网页上的大型数据表加载速度更快?

javascript - Node.js 中是否需要清除 Buffer?如果是,如何?如果否,这里的问题是什么?

Mysql 查询卡在发送数据中