[英]change login to logout
I want to change login button to logout when ctx.session.userID have value, I try to use ejs engine, but it didn't work, is there any problem, or other better way.我想在 ctx.session.userID 有值时将登录按钮更改为注销,我尝试使用 ejs 引擎,但它不起作用,是否有任何问题,或其他更好的方法。 This is server.js:这是 server.js:
const M = require('./model')
const Koa = require('koa')
var serve = require('koa-static')
const session = require('koa-session')
var KoaRouter = require('koa-router')
var koaLogger = require('koa-logger')
const koaBody = require('koa-body')
const views = require('koa-views')
var app = new Koa()
const router = new KoaRouter()
app.use(views('view', {map:{html:'ejs'}}))
app.use(koaLogger())
app.use(koaBody())
app.use(router.routes())
app.use(serve(__dirname + '/public'));
app.keys = ['*@&))9kdjafda;983']
const CONFIG = {
key: 'd**@&(_034k3q3&@^(!$!',
maxAge: 86400000
}
app.use(session(CONFIG, app))
router
.get('/', async (ctx) => {
let title = 'ejs test'
let userid = ctx.session.userID
await ctx.render('index',{
title, userid
})
})
.post('/login', login)
.get('/error', async (ctx)=> {
await ctx.render('error',{})
})
async function login(ctx){
let {id, password} = ctx.request.body
console.log("test:",id,password)
if ( await M.get(id,password) != null){
ctx.session.userID = id
ctx.redirect('/')
console.log('login success')
}
else {
ctx.redirect('/error')
}
}
And this is a part of index.html:这是 index.html 的一部分:
<% if (userid == null){ %>
<a href="/login">Login</a>
<% } else { %>
<a href="/logout">Logout</a>
<% } %>
In your get('/')
route function, you are not using the same session variable you set in your login()
function.在您的get('/')
路由函数中,您没有使用您在login()
函数中设置的相同会话变量。 Change the get('/')
function to let userid = ctx.session.userID
with the capital ID
and log it to the console before sending to the template to make sure the variable is indeed set correctly.更改get('/')
函数以let userid = ctx.session.userID
带有大写ID
并在发送到模板之前将其记录到控制台以确保变量确实设置正确。
I am assuming that you are using session in this application.我假设您在此应用程序中使用会话。 In the index.html you can check whether the status is logged in or not by using following Ejs code:在 index.html 中,您可以使用以下 Ejs 代码检查状态是否已登录:
<li id="login"> <a href="<%= logStatus==='out'?'login':'logout'%>"> <%= logStatus==='out'?'LOGIN':'LOGOUT'%> </a> </li>
After this you can pass the status of login and logout through the Node.js, for each res.render() function you use in the Node.js code by following:在此之后,您可以通过 Node.js 传递登录和注销状态,对于您在 Node.js 代码中使用的每个 res.render() 函数,请执行以下操作:
res.render("[your route name]", { logStatus:`${req.session.user? 'in':'out'}` });
for Example:例如:
app.get("/upload", checkSignIn, (req, res) => { res.render("upload", { logStatus:`${req.session.user? 'in':'out'}` }); });
Note: Always pass the logStatus with every render to avoid error and you don't need to change anything in that, it will automatically check the status from session and send status.注意:每次渲染时总是传递 logStatus 以避免错误,您不需要更改任何内容,它会自动检查会话状态并发送状态。
If you have different name for session, then change it in the req.session.[name of the session].如果您有不同的会话名称,则在 req.session.[会话名称] 中更改它。 for Example: if I have a session name like autoTocken then it will be like:例如:如果我有一个像 autoTocken 这样的会话名称,那么它将类似于:
app.get("/upload", checkSignIn, (req, res) => { res.render("upload", { logStatus:`${req.session.autoTocken ? 'in':'out'}` }); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.