In the previous post, we installed the stack,
Now we’ll figure out the user interface and files.
first we’ll go to app.js and create a Redis session:
var session = require('express-session'); var RedisStore = require('connect-redis')(session); app.use(session({ store: new RedisStore({ db: 0 }), secret: 'SOME RANDOM WORDS', resave: true, saveUninitialized: true }, connected = false));
second we’ll edit the routes\user.js file
router.get('/', function(req, res, next) { res.render('users/login'); }); router.get('/login', function(req, res, next) { res.render('users/login'); }); router.get('/register', function(req, res, next) { res.render('users/register'); }); router.post('/auth', function(req, res, next) { users.findOne({ email: req.body.email }, function(err, result) { if (err) throw err; if (result) { if ("password" in result) { if (result.password == req.body.password) { res.locals.session.connected = true; res.locals.session.UserName = result.name; res.send("ok"); } else { res.send("pw"); // pasword doesn't much } } else { res.send("oe"); // other error } } else { res.send("und"); // user not in db } }); }); router.post('/reg', function(req, res, next) { users.findOne({ email: req.body.email }, function(err, result) { if (err) throw err; if (!result) { users.insert({ email: req.body.email, name: req.body.name, password: req.body.password }); res.send('ok'); } else { res.send('User already exist'); } }); }); router.get('/validateEmail', function (req, res) { users.findOne({ email: req.query.email }, function(err, result) { if (err) throw err; if (result) { res.writeHead(400, 'Email already exist'); res.send(); } else { res.sendStatus(200); } }); }); router.get('/logout', function(req, res, next) { res.locals.session.connected = false; res.redirect("/"); });
Third we’ll add the views views/users/login.jade and views/users/register.jade
I’m using jade with bootstrap, and bootstrap validator
login.jade
extends ../layout block content .row .col-md-4 .col-md-4 h1 Login : .col-md-4 .row .col-md-4 .col-md-4 #errorMessage.text-center form(data-toggle="validator" role="form") .form-group label.control-label(for='inputEmail') Email input#Email.form-control(type='email', placeholder='Email', data-error='Bruh, that email address is invalid', required='') .help-block.with-errors .form-group label.control-label(for='inputEmail') Password input#Password.form-control(type='password', placeholder='Password', required='') .help-block.with-errors .form-group button#btnLogin.btn.btn-primary(type='submit') Submit .row .col-md-8 .col-md-4 a(href="/users/register") Register .col-md-4 block scripts script(src='/js/auth.js') script(src='/js/validator.min.js')
register.jade
extends ../layout block content .row .col-md-4 .col-md-4 h1 Register: .col-md-4 .row .col-md-4 .col-md-4 form(data-toggle="validator" role="form") .form-group label.control-label(for='Name') Name input#Name.form-control(type='text', placeholder='John Doe', required='') .help-block.with-errors .form-group label.control-label(for='inputEmail') Email input#Email.form-control(type='email', name="email", placeholder='Email', data-remote="/users/validateEmail", required='') .help-block.with-errors .form-group label.control-label(for='inputPassword') Password .form-inline.row .form-group.col-sm-6 input#Password.form-control(type='password', data-minlength='6', placeholder='Password', required='') .help-block Minimum of 6 characters .form-group.col-sm-6 input#PasswordConfirm.form-control(type='password', data-match='#Password', data-match-error="Whoops, these don't match", placeholder='Confirm', required='') .help-block.with-errors .form-group .checkbox label input#terms(type='checkbox', required='') | We will send you alerts from time to time, And occasionally updates about the project .help-block.with-errors .form-group button#register.btn.btn-primary(type='submit') Submit .col-md-4 block scripts script(src='/js/auth.js') script(src='/js/validator.min.js')
and last, we’ll use it in a pages
router.get('/CURRENT_PAGE', function(req, res, next) { if (!res.locals.session.connected) { return res.redirect('/users/login?returnpath=' + encodeURIComponent('/CURRENT_PAGE')); } else { GENERATE THE PAGE ETC... }); });