$ express user-auth --git --hbs
$ cd user-auth
- Create this awesome README and outline all steps as we go!
$ git init
$ git add -A
- '$ npm install'
$ DEBUG=user-auth:* npm start
- Visit http://localhost:3000/ and ensure all is well
- Commit
- In layout.hbs, add above
{{{body}}}
:
<nav>
<a href="/signup">Sign up</a>
</nav>
- In
routes/index.js
, add route:
router.get('/signup', function(req, res, next) {
res.render('users/new');
});
- Create a file
views/users/new.hbs
with the following content:
<form action="/users" method="post">
<label for="email">Email</label>
<input type="email" name="email" value="">
<br>
<label for="password">Password</label>
<input type="password" name="password" value="">
<br>
<input type="submit" value="Sign Up">
</form>
- Add the dependencies needed to save user to the database to
package.json
:
"bcrypt":"~0.8.3",
"cookie-session": "~1.2.0",
"monk": "~1.0.1",
$ npm install
- Add to
app.js
in the top requires:
var cookieSession = require('cookie-session')
- Add to
app.js
under the engine setup:
app.set('trust proxy', 1)
app.use(cookieSession({
name: 'session',
keys: ['key1', 'key2']
}))
- Add to
routes/users.js
under the requires:
var bcrypt = require('bcrypt');
var db = require('monk')('localhost/user-auth');
var User = db.get('users');
- Add route to create user from signup form in
users.js
:
router.post('/', function(req, res, next) {
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash(req.body.password, salt, function(err, hash) {
user = User.insert({ email: req.body.email, passwordDigest: hash });
req.session.currentUserEmail = user.query.email;
res.redirect('/');
});
});
});
- Pass in user email into views in the 'routes/index.js' file, updating root path like so:
router.get('/', function(req, res, next) {
res.render('index', { title: 'Cool App, Dude', currentUserEmail: req.session.currentUserEmail});
});
- And finally, update the layout view nav to use your new session:
<nav>
{{#if currentUserEmail}}
<h1>Welcome, {{currentUserEmail}}!</h1>
{{else}}
<a href="/signup">Sign up</a>
{{/if}}
</nav>
- Add a signout link to layout within
{{#if currentUserEmail}}
:
<a href="/signout">Sign out</a>
- Add route to
index.js
:
router.get('/signout', function(req, res, next) {
req.session = null;
res.redirect('/');
});
- Add a signin link to layout.hbs within else portion of
{{#if currentUserEmail}}
:
<a href="/signin">Sign in</a>
- Add route to
index.js
router.get('/signin', function(req, res, next) {
res.render('authentication/new');
});
- Add
views/authentication/new.hbs
with the following content:
<h1>Sign in!</h1>
<form action="/authentication" method="post">
<label for="email">Email</label>
<input type="email" name="email" value="">
<br>
<label for="password">Password</label>
<input type="password" name="password" value="">
<br>
<input type="submit" value="Sign In">
</form>
- Add a authentication router to
app.js
:
var authentication = require('./routes/authentication');
near other like route variables
app.use('/authentication', authentication);
near other like app.use
route middleware
- Add a new route file
routes/authentication.js
with the following content:
router.post('/', function(req, res, next) {
User.findOne({ email: req.body.email }).on('success', function (user) {
bcrypt.compare(req.body.password, user.passwordDigest, function(err, valid) {
if (valid) {
req.session.currentUserEmail = user.email;
res.redirect('/');
};
});
});
});