windhamdavid 4 years ago
parent
commit
d903ba117e
4 changed files with 130 additions and 0 deletions
  1. 43 0
      controllers/user.js
  2. 59 0
      models/user.js
  3. 2 0
      package.json
  4. 26 0
      src/views/account/register.hbs

+ 43 - 0
controllers/user.js

@@ -5,4 +5,47 @@ exports.getLogin = function (req, res) {
   res.render('account/login', {
     title: 'Login'
   });
+};
+
+exports.postLogin = function(req, res, next) {
+  req.assert('email', 'Email is not valid').isEmail();
+  req.assert('password', 'Password cannot be blank').notEmpty();
+
+  var errors = req.validationErrors();
+
+  if (errors) {
+    req.flash('errors', errors);
+    return res.redirect('/login');
+  }
+
+  passport.authenticate('local', function(err, user, info) {
+    if (err) {
+      return next(err);
+    }
+    if (!user) {
+      req.flash('errors', { msg: info.message });
+      return res.redirect('/login');
+    }
+    req.logIn(user, function(err) {
+      if (err) {
+        return next(err);
+      }
+      req.flash('success', { msg: 'Success! You are logged in.' });
+      res.redirect(req.session.returnTo || '/');
+    });
+  })(req, res, next);
+};
+
+exports.logout = function(req, res) {
+  req.logout();
+  res.redirect('/');
+};
+
+exports.getSignup = function(req, res) {
+  if (req.user) {
+    return res.redirect('/');
+  }
+  res.render('account/register', {
+    title: 'Register'
+  });
 };

+ 59 - 0
models/user.js

@@ -0,0 +1,59 @@
+var bcrypt = require('bcrypt-nodejs');
+var crypto = require('crypto');
+var mongoose = require('mongoose');
+
+var userSchema = new mongoose.Schema({
+  email: { type: String, unique: true, lowercase: true },
+  password: String,
+  tokens: Array,
+
+  profile: {
+    name: { type: String, default: '' },
+    website: { type: String, default: '' },
+    picture: { type: String, default: '' }
+  },
+
+  resetPasswordToken: String,
+  resetPasswordExpires: Date
+});
+
+userSchema.pre('save', function(next) {
+  var user = this;
+  if (!user.isModified('password')) {
+    return next();
+  }
+  bcrypt.genSalt(10, function(err, salt) {
+    if (err) {
+      return next(err);
+    }
+    bcrypt.hash(user.password, salt, null, function(err, hash) {
+      if (err) {
+        return next(err);
+      }
+      user.password = hash;
+      next();
+    });
+  });
+});
+
+userSchema.methods.comparePassword = function(candidatePassword, cb) {
+  bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
+    if (err) {
+      return cb(err);
+    }
+    cb(null, isMatch);
+  });
+};
+
+userSchema.methods.gravatar = function(size) {
+  if (!size) {
+    size = 200;
+  }
+  if (!this.email) {
+    return 'https://gravatar.com/avatar/?s=' + size + '&d=retro';
+  }
+  var md5 = crypto.createHash('md5').update(this.email).digest('hex');
+  return 'https://gravatar.com/avatar/' + md5 + '?s=' + size + '&d=retro';
+};
+
+module.exports = mongoose.model('User', userSchema);

+ 2 - 0
package.json

@@ -13,6 +13,7 @@
     "node": ">=4.0.0"
   },
   "dependencies": {
+    "bcrypt-nodejs": "0.0.3",
     "bcryptjs": "^2.3.0",
     "body-parser": "^1.14.1",
     "connect-mongo": "^0.8.2",
@@ -30,6 +31,7 @@
     "mongoose": "^4.2.9",
     "morgan": "~1.6.1",
     "passport": "^0.3.2",
+    "passport-local": "^1.0.0",
     "serve-favicon": "~2.3.0"
   },
   "devDependencies": {

+ 26 - 0
src/views/account/register.hbs

@@ -0,0 +1,26 @@
+<form id="signup-form" method="POST" class="form-horizontal">
+   <input type="hidden" name="_csrf" value="Ho631wzoqHU7byq539m5MkUk68pvgyS6nnH3o=">
+   <div class="form-group">
+      <label for="email" class="col-sm-3 control-label">Email</label>
+      <div class="col-sm-7">
+         <input type="email" name="email" id="email" placeholder="Email" autofocus class="form-control">
+      </div>
+   </div>
+   <div class="form-group">
+      <label for="password" class="col-sm-3 control-label">Password</label>
+      <div class="col-sm-7">
+         <input type="password" name="password" id="password" placeholder="Password" class="form-control">
+      </div>
+   </div>
+   <div class="form-group">
+      <label for="confirmPassword" class="col-sm-3 control-label">Confirm Password</label>
+      <div class="col-sm-7">
+         <input type="password" name="confirmPassword" id="confirmPassword" placeholder="Confirm Password" class="form-control">
+      </div>
+   </div>
+   <div class="form-group">
+      <div class="col-sm-offset-3 col-sm-7">
+         <button type="submit" class="btn btn-success"><i class="fa fa-user-plus"></i>Signup</button>
+      </div>
+   </div>
+</form>