Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add reset password functionality to user component

  • Loading branch information...
commit 69909fba61a8a8a3d208f76640c4eccdb66e8eb3 1 parent 379e965
Jim Snodgrass authored
View
45 components/user/index.js
@@ -20,15 +20,28 @@ module.exports = function(app) {
// ROUTES
- app.get('/', function signIn(req, res) {
- if (req.session.user) {
- return res.redirect('/dashboard');
+ app.get('/', [
+ addParams,
+ function (req, res) {
+ if (req.session.user) {
+
+ console.log(req.session.user);
+
+
+ if (req.session.user.resetPassword) {
+ req.session.user.resetPassword = false;
+ UserModel.findByIdAndUpdate(req.session.user._id, {resetPassword:false}, function(){});
+ req.flash("Would you like to update your password to something more memorable?");
+ return res.redirect('/settings');
+ }
+ return res.redirect('/dashboard');
+ }
+ return res.render(path.join(__dirname, 'view/signin'));
}
- return res.render(path.join(__dirname, 'view/signin'));
- });
+ ]);
app.get('/settings', [
- render('view/settings')
+ render('settings')
]);
app.put('/settings', [
@@ -36,7 +49,10 @@ module.exports = function(app) {
redirect('/settings')
]);
- app.get('/register', render('view/register'));
+ app.get('/register', [
+ addParams,
+ render('register')
+ ]);
app.post('/register', [
middleware.doRegister,
@@ -53,6 +69,14 @@ module.exports = function(app) {
redirect('/')
]);
+ app.get('/reset', [
+ render('reset')
+ ]);
+
+ app.post('/reset', [
+ middleware.resetPassword
+ ]);
+
};
@@ -60,7 +84,7 @@ module.exports = function(app) {
function render(view) {
return function (req, res) {
- return res.render(path.join(__dirname, view));
+ return res.render(path.join(__dirname, 'view/'+view));
};
}
@@ -75,4 +99,9 @@ function flash(message) {
req.flash(message);
return next();
};
+}
+
+function addParams(req, res, next) {
+ res.locals.querystring = req.query;
+ return next();
}
View
55 components/user/middleware.js
@@ -67,16 +67,55 @@ module.exports = function(app) {
password: req.body.password
};
UserModel.authenticate(creds, function(err, user) {
- if (user) {
- req.session.user = user;
- return res.redirect('/dashboard');
- }
- else {
- req.flash('Sorry, that username or password was not found.');
- return res.redirect('/');
+ if (user) req.session.user = user;
+ else req.flash('Sorry, that username or password was not found.');
+
+ return res.redirect('/');
+ });
+ },
+
+ resetPassword: function(req,res,next) {
+ UserModel.findByEmail(req.body.email, function(err, user) {
+ if (err || !user || user.length < 1) {
+ req.flash("Sorry, that email does not seem to be registered.");
+ return res.redirect('/reset');
}
+
+ var newPassword = _gen_password();
+ user.password = newPassword;
+ user.resetPassword = true;
+
+ user.save(function(err) {
+ if (err) {
+ req.flash("Sorry, there was an error while resetting the password.");
+ return res.redirect('/reset');
+ }
+
+ req.flash("A new password has been sent to that email address.");
+ res.redirect('/');
+
+ app.emailer.send('password_reset', {
+ name: user.name,
+ to: user.email,
+ password: newPassword,
+ querystring: "password="+encodeURIComponent(newPassword)+"&email="+encodeURIComponent(user.email)
+ });
+
+ });
});
}
};
-};
+};
+
+
+function _gen_password() {
+ var chars = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789',
+ length = 8,
+ password = '';
+ while (length--) {
+ var pos = ~~(Math.random() * chars.length);
+ password += chars.charAt(pos);
+ }
+ return password;
+}
View
3  components/user/model/userModel.js
@@ -7,7 +7,8 @@ module.exports = function() {
var User = new mongoose.Schema({
email : { type: String, index: true, required:true, lowercase: true, trim:true, unique: true, validate: [utils.validate.email, 'not valid'] },
name : { type: String, trim: true },
- password : { type: String, trim: true, required:true, validate: [utils.validate.length(4), 'required to be at least 4 characters'] }
+ password : { type: String, trim: true, required:true, validate: [utils.validate.length(4), 'required to be at least 4 characters'] },
+ resetPassword : { type: Boolean, 'default':false }
}, {strict:true});
// Plugins
View
19 components/user/view/reset.jade
@@ -0,0 +1,19 @@
+extends ../../../shared/views/layout
+
+block content
+
+ section.smallColumn.centered.pushDown
+ .column1of2
+ p.tac: a.large_logo.block(href='/')
+ span.is_hidden Skookum Digital Works
+
+ .column1of2
+ h3.micro Please enter your email address and we will send you a new password.
+ br
+ form(action='/reset', method='post')
+ ul
+ li
+ input.block(type='email', name='email', placeholder="email address..")
+ .button-bar
+ button(type='submit') Send New Password
+ a.micro(href='/register') Cancel
View
12 components/user/view/signin.jade
@@ -3,14 +3,16 @@ extends ../../../shared/views/layout
block subject
block content
-
+
section.smallColumn.centered.pushDown
p.tac: a.logo.block(href='/')
span.is_hidden base12
form(action='/signin', method='post')
- input.block(type='email', name='email', placeholder='email')
- input.block(type='password', name='password', placeholder='password')
+ input.block(type='email', name='email', placeholder='email', value=querystring.email)
+ input.block(type='password', name='password', placeholder='password', value=querystring.password)
+ .button-bar
+ button(type='submit') Sign in
.button-bar
- a.micro(href='/register') Need to register?
- button(type='submit') Sign in
+ a.micro(href='/register') Register
+ a.micro(href='/reset') Reset password?
View
23 lib/emailer/templates/password_reset.html
@@ -0,0 +1,23 @@
+
+"from": "{{contact_email}}",
+"to": "{{ to }}",
+"bcc": "",
+"subject": "Base12: Your password has been reset.",
+"bodyType": "html"
+
+<p>
+ Hi {{name}},
+</p>
+
+<p>
+ Your password has been reset for the Base12 Application. You will find your new password below. After you login with this new password, you can change it to something you can remember. <a href="{{host}}?{{querystring}}">Log in now!</a>
+</p>
+
+<h2>{{password}}</h2>
+
+<p>
+ Thanks,<br/>
+ <a href="http://www.skookum.com">Skookum Digital Works</a><br/>
+</p>
+
+</div>

0 comments on commit 69909fb

Please sign in to comment.
Something went wrong with that request. Please try again.