如何使用 node js 在浏览器中创建 cookie
How create cookies in browser with node js
我在 node js 中有一个用户身份验证功能,我没有使用身份验证、护照或其他库,我不太理解,所以我做了自己的身份验证功能。
但是,我的功能仅对用户有效,它不会创建 cookie,我需要在“过期日期”节点上创建 cookie,并且一旦等待,它就会将我重定向回登录路径。我想在这个函数中实现:
function verify(req,res,username,password){
db.serialize(function (){
const query = 'SELECT * from Users WHERE User = (?) AND Password = (?)'
db.all(query,[username, password],function(err,rows){
if(err) {
console.log(err);
}
if(rows.length == 1){
console.log(rows);
console.log("Correct user");
res.render(__dirname + "/View/Home/index.handlebars");
}
else{
console.log("Incorrecto user!")
res.redirect('/login')
}
});
});
}
这是我在节点js中的路由
app.get("/login", (req,res) => {
res.render(__dirname + "/View/Home/login.handlebars");
});
app.post("/",(req,res) => {
const username = req.body.username
const password = req.body.password
verify(req,res,username,password)
});
如果用户有效,我需要在 verify () 函数中创建一个 cookie,当 cookie 过期时,用户将再次重定向到“/ login”路由
假设您使用的是 Express 服务器,您可以使用 res.cookie(name, value, options)
设置 cookie。 options
是一个对象,而 maxAge
属性 可能是你想要的,这是一个数字,它是过期前的毫秒数。例如,如果用户有效,您可以执行 res.cookie('username', username, {maxAge: 172800000 /* two days */})
.
您可以使用 req.cookies
, which is an object of cookie name keys and string values. You can check if the 'username'
key exists and is valid, and if not, redirect using res.redirect
访问请求的 cookie。
function verify(req,res,username,password){
// Place this block wherever you want it to check for a valid cookie
// Depending on the context when this function is called, that might not be at the start
if(typeof(req.cookies.username) != 'string')
res.redirect('/login');
db.serialize(function (){
const query = 'SELECT * from Users WHERE User = (?) AND Password = (?)'
db.all(query,[username, password],function(err,rows){
if(err) {
console.log(err);
}
if(rows.length == 1){
console.log(rows);
console.log("Correct user");
// Set Cookie
res.cookie('username', username, {maxAge: 172800000 /* two days */});
res.render(__dirname + "/View/Home/index.handlebars");
}
else{
console.log("Incorrecto user!")
res.redirect('/login')
}
});
});
}
使用 Keycloak
下载
Offical Keycloak Download Page
安装 Linux/Unix
./bin/standalone.sh
** 安装 Windows**
\bin\standalone.bat
像这样使用 arg -b 绑定网络地址(默认为 127.0.0.1)
./bin/standalone.sh -b 192.168.1.150
安装Keycloak-nodejs-connect
npm install keycloak-connect
我在 node js 中有一个用户身份验证功能,我没有使用身份验证、护照或其他库,我不太理解,所以我做了自己的身份验证功能。 但是,我的功能仅对用户有效,它不会创建 cookie,我需要在“过期日期”节点上创建 cookie,并且一旦等待,它就会将我重定向回登录路径。我想在这个函数中实现:
function verify(req,res,username,password){
db.serialize(function (){
const query = 'SELECT * from Users WHERE User = (?) AND Password = (?)'
db.all(query,[username, password],function(err,rows){
if(err) {
console.log(err);
}
if(rows.length == 1){
console.log(rows);
console.log("Correct user");
res.render(__dirname + "/View/Home/index.handlebars");
}
else{
console.log("Incorrecto user!")
res.redirect('/login')
}
});
});
}
这是我在节点js中的路由
app.get("/login", (req,res) => {
res.render(__dirname + "/View/Home/login.handlebars");
});
app.post("/",(req,res) => {
const username = req.body.username
const password = req.body.password
verify(req,res,username,password)
});
如果用户有效,我需要在 verify () 函数中创建一个 cookie,当 cookie 过期时,用户将再次重定向到“/ login”路由
假设您使用的是 Express 服务器,您可以使用 res.cookie(name, value, options)
设置 cookie。 options
是一个对象,而 maxAge
属性 可能是你想要的,这是一个数字,它是过期前的毫秒数。例如,如果用户有效,您可以执行 res.cookie('username', username, {maxAge: 172800000 /* two days */})
.
您可以使用 req.cookies
, which is an object of cookie name keys and string values. You can check if the 'username'
key exists and is valid, and if not, redirect using res.redirect
访问请求的 cookie。
function verify(req,res,username,password){
// Place this block wherever you want it to check for a valid cookie
// Depending on the context when this function is called, that might not be at the start
if(typeof(req.cookies.username) != 'string')
res.redirect('/login');
db.serialize(function (){
const query = 'SELECT * from Users WHERE User = (?) AND Password = (?)'
db.all(query,[username, password],function(err,rows){
if(err) {
console.log(err);
}
if(rows.length == 1){
console.log(rows);
console.log("Correct user");
// Set Cookie
res.cookie('username', username, {maxAge: 172800000 /* two days */});
res.render(__dirname + "/View/Home/index.handlebars");
}
else{
console.log("Incorrecto user!")
res.redirect('/login')
}
});
});
}
使用 Keycloak
下载 Offical Keycloak Download Page
安装 Linux/Unix
./bin/standalone.sh
** 安装 Windows**
\bin\standalone.bat
像这样使用 arg -b 绑定网络地址(默认为 127.0.0.1)
./bin/standalone.sh -b 192.168.1.150
安装Keycloak-nodejs-connect
npm install keycloak-connect