Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add flash messages and fix signup validation fixes #18 #23

  • Loading branch information...
commit 28f02a15339784220aa4bc3fe22b2d9d9a16cbc6 1 parent ae3c5b8
@madhums authored
View
10 app/controllers/users.js
@@ -11,14 +11,16 @@ exports.authCallback = function (req, res, next) {
// login
exports.login = function (req, res) {
res.render('users/login', {
- title: 'Login'
+ title: 'Login'
+ , message: req.flash('error')
})
}
// sign up
exports.signup = function (req, res) {
res.render('users/signup', {
- title: 'Sign up'
+ title: 'Sign up'
+ , user: new User()
})
}
@@ -38,7 +40,9 @@ exports.create = function (req, res) {
var user = new User(req.body)
user.provider = 'local'
user.save(function (err) {
- if (err) return res.render('users/signup', { errors: err.errors })
+ if (err) {
+ return res.render('users/signup', { errors: err.errors, user: user })
+ }
req.logIn(user, function(err) {
if (err) return next(err)
return res.redirect('/')
View
7 app/models/user.js
@@ -53,6 +53,12 @@ UserSchema.path('username').validate(function (username) {
return username.length
}, 'Username cannot be blank')
+UserSchema.path('hashed_password').validate(function (hashed_password) {
+ // if you are authenticating by any of the oauth strategies, don't validate
+ if (authTypes.indexOf(this.provider) !== -1) return true
+ return hashed_password.length
+}, 'Password cannot be blank')
+
// pre save hooks
UserSchema.pre('save', function(next) {
@@ -74,6 +80,7 @@ UserSchema.method('makeSalt', function() {
})
UserSchema.method('encryptPassword', function(password) {
+ if (!password) return ''
return crypto.createHmac('sha1', this.salt).update(password).digest('hex')
})
View
1  app/views/users/login.jade
@@ -2,6 +2,7 @@ extends auth
block auth
form.login.form-horizontal(action="/users/session", method="post")
+ p.error= message
.control-group
label.control-label(for='email') Email
.controls
View
6 app/views/users/signup.jade
@@ -5,17 +5,17 @@ block auth
.control-group
label.control-label(for='name') Full name
.controls
- input#name(type='text', name="name", placeholder='Full name')
+ input#name(type='text', name="name", placeholder='Full name', value=user.name)
.control-group
label.control-label(for='email') Email
.controls
- input#email(type='text', name="email", placeholder='Email')
+ input#email(type='text', name="email", placeholder='Email', value=user.email)
.control-group
label.control-label(for='username') Username
.controls
- input#username(type='text', name="username", placeholder='Username')
+ input#username(type='text', name="username", placeholder='Username', value=user.username)
.control-group
label.control-label(for='password') Password
View
2  config/routes.js
@@ -12,7 +12,7 @@ module.exports = function (app, passport, auth) {
app.get('/signup', users.signup)
app.get('/logout', users.logout)
app.post('/users', users.create)
- app.post('/users/session', passport.authenticate('local', {failureRedirect: '/login'}), users.session)
+ app.post('/users/session', passport.authenticate('local', {failureRedirect: '/login', failureFlash: 'Invalid email or password.'}), users.session)
app.get('/users/:userId', users.show)
app.get('/auth/facebook', passport.authenticate('facebook', { scope: [ 'email', 'user_about_me'], failureRedirect: '/login' }), users.signin)
app.get('/auth/facebook/callback', passport.authenticate('facebook', { failureRedirect: '/login' }), users.authCallback)
View
1  package.json
@@ -17,6 +17,7 @@
, "jade": "latest"
, "mongoose": "latest"
, "connect-mongodb": "latest"
+ , "connect-flash": "latest"
, "passport": "latest"
, "passport-local": "latest"
, "passport-facebook": "latest"
View
4 public/css/app.css
@@ -43,4 +43,6 @@ a.tag {
margin: 0 10px 0 0;
}
-/*form.signup { display: none; }*/
+.error {
+ color: #FF2602;
+}
View
3  settings.js
@@ -5,6 +5,7 @@
var express = require('express')
, mongoStore = require('connect-mongodb')
+ , flash = require('connect-flash')
exports.boot = function(app, config, passport){
bootApplication(app, config, passport)
@@ -77,6 +78,8 @@ function bootApplication(app, config, passport) {
})
}))
+ app.use(flash())
+
app.use(passport.initialize())
app.use(passport.session())
Please sign in to comment.
Something went wrong with that request. Please try again.