1

我想在登录表单中发送一条消息。当用户名或密码错误时,发送一条username/password incorrect不显示的闪信。

我尝试调试控制台日志local.error_message,username/password incorrect当我输入用户名和密码错误时显示成功。当我评论调试代码并实现到 htmltemplate 时,它​​不能正常工作。

pages.js

const express = require('express');
const User = require('./login');

const htmlTemplate = (error_message) => {
    return `
    <!doctype html>
    <html class="no-js" lang="en">'
    <body>
         <form action="/login" method="POST" class="login-background">
            <img class="img-responsive" alt="logo" src="img/logo.png">
            <h5>LOGIN</h5>
            ${error_message ? `<div class="alert alert danger">${error_message}</div>` : ""}
            <input type="text" name="username" class="form-control input-lg" placeholder="Username">
            <input type="password" name="password" class="form-control input-lg" placeholder="Password">

            <button type="submit" value="login" class="btn btn-primary">Masuk</button>
        </form>
    </body>
    </html>
`};

//Get the index page
const getIndexPage = ('/', (req, res, next) => {
    let user = req.session.user;
    let login = "";

    if (user) {
        res.redirect('/dashboard');
        return;
    }

    res.send(htmlTemplate(login))
});

//get Homepage
const getHome = ('/home', (req, res, next) => {
    let user = req.session.user;

    if (user) {
        res.render('/dashboard', { opp: req.session.opp, name: user.nama });
        return;
    }
    res.redirect('/');
});
const postLogin = ('/login', (req, res, next) => {
    user.login(req.body.username, req.body.password, function (result) {
        if (result) {
            req.session.user = result;
            req.session.opp = 1;
            res.redirect('/dashboard');
        } else {
            req.flash('error_message', 'Username/password incorrect!');
            res.locals.error_message = req.flash('error_message');
            // var message = res.locals.error_message = req.flash('error_message');
            // console.log(message);
            res.redirect('/dashboard');
        }
    })
});
module.exports = {
    getIndexPage,
    postLogin,
    getHome
};

index.js

const express = require("express");
const mysql = require("mysql");
const bodyParser = require("body-parser");
const session = require('express-session');
const cookieParser = require('cookie-parser');
const flash = require('connect-flash');
const app = express();

const port = 3000;
const login = require("./pages");

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));

// parse application/json
app.use(bodyParser.json());

app.use(cookieParser());

//session
app.use(session({
  secret: 'ghhft',
  resave: false,
  saveUninitialized: false,
  cookie: {
    maxAge: 60 * 1000 * 30
  }
}));

//flash
app.use(flash());

const onlyLoggedinUser = next => (req, res) => {
  console.log(`onlyLoggedinUser ${req.path}`)
  const level = getUserContext(req).level
  if (level !== TYPE_ADMIN && level !== TYPE_MANAGER) {
    res.redirect("/loggout")
    return;
  }

  next(req, res)
}

const onlyLoggedoutUser = next => (req, res) => {
  const level = getUserContext(req).level
  if (level !== TYPE_UNKNOWN) {
    res.redirect("/dashboard")
    return;
  }

  next(req, res)
}

app.get("/", onlyLoggedoutUser(login.getIndexPage));
app.post("/login", onlyLoggedoutUser(login.postLogin));
app.get("/home", onlyLoggedinUser(login.getHome));

助手.js

const TYPE_UNKNOWN = "UNKNOWN" // 0
const TYPE_ADMIN = "ADMIN" // 1
const TYPE_MANAGER = "MANAGER" // 2

function getUserContext(req) {
    const user = _.get(req, "session.user", false)

    let level = TYPE_UNKNOWN

    if (user === false) return {
        level,
        username: "",
    }

    if (user.level === "1")
        level = TYPE_ADMIN

    if (user.level === "2")
        level = TYPE_MANAGER

    return {
        level,
        username: user.username
    }
}

你能帮我解决这个问题吗?

4

0 回答 0