First step:
Edit layout of home page.
--- views/index.jade.bak 2017-03-15 03:08:39.126666666 +0000
+++ views/index.jade 2017-03-15 03:08:48.830000000 +0000
@@ -1,5 +1,21 @@
-extends layout
+doctype html
+html
+ head
+ title= 'Blog System'
+ body
+ div(style='float:left;margin:10px;')
+ a(href='/') Home
-block content
- h1= title
- p Welcome to #{title}
+ div(style='float:right')
+ if (username && authenticated)
+ a(href='/users/signout', style='margin:10px;') Sign Out
+ a(href='/users/add_article', style='margin:10px;') Add Article
+ a(href='/users/profile', style='margin:10px;') Profile
+ span #{username}
+ else
+ a(href='/users/signin', style='margin:10px;') Log In
+ a(href='/users/register', style='margin:10px;') Register
+ a(href='/users/forget', style='margin:10px;') Forgot password
+
+ div(style='padding:50px;')
+ h1= title
Edit routing rule.
--- routes/index.js.bak 2017-03-15 03:14:35.019999998 +0000
+++ routes/index.js 2017-03-15 03:14:47.996666664 +0000
@@ -3,7 +3,9 @@
/* GET home page. */
router.get('/', function(req, res, next) {
- res.render('index', { title: 'Express' });
+ res.locals.username = req.session.name ;
+ res.locals.authenticated = req.session.logined;
+ res.render( 'index', {title : 'Blog System'});
});
module.exports = router;
Second step:
Design the layout of webpage.
doctype html
html
head
title= Register
body
div(style='float:left;margin:10px;')
a(href='/') Home
div(style='float:right')
a(href='/users/signin', style='margin:10px;') Log In
a(href='/users/forget', style='margin:10px;') Forgot password
div(style='padding:50px;')
form(name="login", action="/apis/login", method="post")
h1 Register
div
p
label(style='padding-right:50px') Username or e-mail
input(type="text", name="user", placeholder='username@example.com')
p
label(style='padding-right:110px') password
input(type="password", name="passwd")
p
span(style='padding-right:260px')
input(type="submit", name="submit", value="Sing Up")
--- routes/users.js.bak 2017-03-15 03:39:48.486666666 +0000
+++ routes/users.js 2017-03-15 03:42:55.686666666 +0000
@@ -3,22 +3,36 @@
/* ______________. */
router.get('/register', function(req, res, next) {
- res.send("This is the register page.");
+ if (req.session.logined) {
+ res.redirect('/');
+ return;
+ }
+ res.render('users/register');
});
/* ______________. */
router.get('/signin', function(req, res, next) {
- res.send("This is the signin page.");
+ if (req.session.logined) {
+ res.redirect('/');
+ return;
+ }
+ res.render('users/signin');
});
/* ______________. */
router.get('/signout', function(req, res, next) {
- res.send("This is the signout page.");
+ req.session.logined = false;
+ res.redirect('/');
+ res.end();
});
/* ____________. */
router.get('/forget', function(req, res, next) {
- res.send("This is the forget page.");
+ if (req.session.logined) {
+ res.redirect('/');
+ return;
+ }
+ res.render('users/forget');
});
--- routes/apis.js.bak 2017-03-15 03:41:16.383333333 +0000
+++ routes/apis.js 2017-03-15 03:41:49.346666665 +0000
@@ -8,7 +8,14 @@
/* __________________. */
router.post('/login', function(req, res, next) {
- res.send("This is the login function.");
+ if ((!req.body.user) || (!req.body.passwd)) {
+ res.redirect('register');
+ return;
+ }
+ req.session.name = req.body.user;
+ req.session.passwd = req.body.passwd;
+ req.session.logined = true;
+ res.redirect('/');
});
Reference:
0 意見:
張貼留言