/controller/user.controller.js
2018
const db = require('../db/index').lowDB; const ids = require('short-id'); const md5 = require('md5'); module.exports.index = function (req, res) { res.render('users/index', { users: db.get('users').value() }) } module.exports.create = function (req, res) { res.render('users/create') } module.exports.postCreate = function (req, res) { // Add a post req.body.id = ids.generate(); req.body.password = md5(req.body.password); db.get('users').push(req.body).write() res.redirect('/users'); } module.exports.get = function (req, res) { var id = req.params.id; res.render('users/view', { user: db.get('users').find({ id: id }).value() }) } module.exports.search = function (req, res) { var q = req.query.name; var matchUsers = db.get('users').value().filter(f => f.name.toLocaleLowerCase().indexOf(q.toLocaleLowerCase()) !== -1); res.render('users/index', { users: matchUsers }) }
/controller/auth.controller.js
2018
const db = require('../db/index').lowDB; const md5 = require('md5'); module.exports.login = function (req, res, next) { res.render('auth/login'); } module.exports.postLogin = function (req, res, next) { const email = req.body.email; const password = req.body.password; const user = db.get('users').find({ email: email }).value(); if (!user) { res.render('auth/login', { errors: ['User does not exits'], values: res.body }); } console.log(md5(password)); if (md5(password) != user.password) { res.render('auth/login', { errors: ['Wrong password'], values: res.body }); } res.cookie('userId', user.id); res.redirect('/users'); } module.exports.postLogout = function (req, res, next) { res.clearCookie("userId"); res.redirect('/'); }
0 nhận xét:
Post a Comment