請教一下各位前輩,
我現在在寫一個網站,目前使用者成功登入後,
能將使用者登入帳號顯示於我的網站的每個分頁中,
但我想顯示的是我DataBase中使用該帳號登入的使用者姓名,想請教一下該怎麼做?
懇請大家幫個忙
這是我的middleware
app.use((req, res, next) => {
res.locals.renderData = {
loginUser: req.session.loginUser,
};
next();
});
//首頁
app.get("/", (req, res) => {
const data = res.locals.renderData;
res.render("home",data);
});
這是我的登入
app.get("/login", (req, res) => {
const data = res.locals.renderData;
if (req.session.flashMsg) {
data.flashMsg = req.session.flashMsg;
delete req.session.flashMsg;
}
res.render("login", data);
});
app.post('/login', (req, res)=>{
db.query("SELECT * FROM `med_basic_info` WHERE `ID`=? AND `password`=SHA1(?)",
[req.body.user, req.body.password],
(error, results, fields)=>{
// console.log(results); // debug
if(! results.length){
req.session.flashMsg = {
type: "danger",
msg: "帳號或密碼錯誤"
};
}
else {
console.log(req.session);
req.session.loginUser = req.body.user;
req.session.flashMsg = {
type: "success",
msg: "登入成功"
};
}
res.redirect('/login');
});
});