hostr/web/routes/user.js

96 lines
3.8 KiB
JavaScript
Raw Normal View History

2015-07-09 23:01:43 +01:00
import { authenticate, setupSession, signup as signupUser, activateUser, sendResetToken, validateResetToken, updatePassword } from '../lib/auth';
export function* signin() {
if (!this.request.body.email) {
return yield this.render('signin', {csrf: this.csrf});
2015-07-09 23:01:43 +01:00
}
2015-08-09 17:21:39 +01:00
this.statsd.incr('auth.attempt', 1);
2015-08-22 16:16:15 +01:00
const user = yield authenticate.call(this, this.request.body.email, this.request.body.password);
2015-07-09 23:01:43 +01:00
if(!user) {
2015-08-09 17:21:39 +01:00
this.statsd.incr('auth.failure', 1);
return yield this.render('signin', {error: 'Invalid login details', csrf: this.csrf});
2015-07-09 23:01:43 +01:00
} else if (user.activationCode) {
return yield this.render('signin', {error: 'Your account hasn\'t been activated yet. Check your for an activation email.', csrf: this.csrf});
2015-07-09 23:01:43 +01:00
} else {
2015-08-09 17:21:39 +01:00
this.statsd.incr('auth.success', 1);
2015-08-22 16:16:15 +01:00
yield setupSession.call(this, user);
2015-07-09 23:01:43 +01:00
this.redirect('/');
}
}
export function* signup() {
if (!this.request.body.email) {
return yield this.render('signup', {csrf: this.csrf});
2015-07-09 23:01:43 +01:00
}
if (this.request.body.email !== this.request.body.confirm_email) {
return yield this.render('signup', {error: 'Emails do not match.', csrf: this.csrf});
2015-07-09 23:01:43 +01:00
} else if (this.request.body.email && !this.request.body.terms) {
return yield this.render('signup', {error: 'You must agree to the terms of service.', csrf: this.csrf});
2015-07-09 23:01:43 +01:00
} else if (this.request.body.password && this.request.body.password.length < 7) {
return yield this.render('signup', {error: 'Password must be at least 7 characters long.', csrf: this.csrf});
2015-07-09 23:01:43 +01:00
}
const ip = this.headers['x-real-ip'] || this.ip;
const email = this.request.body.email;
const password = this.request.body.password;
try {
2015-08-22 16:16:15 +01:00
yield signupUser.call(this, email, password, ip);
2015-07-09 23:01:43 +01:00
} catch (e) {
return yield this.render('signup', {error: e.message, csrf: this.csrf});
2015-07-09 23:01:43 +01:00
}
2015-08-09 17:21:39 +01:00
this.statsd.incr('auth.signup', 1);
2015-07-09 23:01:43 +01:00
return yield this.render('signup', {message: 'Thanks for signing up, we\'ve sent you an email to activate your account.'});
}
export function* forgot(token) {
const Reset = this.db.Reset;
const Users = this.db.Users;
if (this.request.body.email) {
var email = this.request.body.email;
2015-08-22 16:16:15 +01:00
yield sendResetToken.call(this, email);
2015-08-09 17:21:39 +01:00
this.statsd.incr('auth.reset.request', 1);
return yield this.render('forgot', {message: 'We\'ve sent an email with a link to reset your password. Be sure to check your spam folder if you it doesn\'t appear within a few minutes', token: null, csrf: this.csrf});
2015-07-09 23:01:43 +01:00
} else if (token && this.request.body.password) {
if (this.request.body.password.length < 7) {
return yield this.render('forgot', {error: 'Password needs to be at least 7 characters long.', token: token, csrf: this.csrf});
2015-07-09 23:01:43 +01:00
}
2015-08-22 16:16:15 +01:00
const tokenUser = yield validateResetToken.call(this, token);
2015-07-09 23:01:43 +01:00
var userId = tokenUser._id;
2015-08-22 16:16:15 +01:00
yield updatePassword.call(this, userId, this.request.body.password);
2015-07-09 23:01:43 +01:00
yield Reset.remove({_id: userId});
const user = yield Users.findOne({_id: userId});
2015-08-22 16:16:15 +01:00
yield setupSession.call(this, user);
2015-08-09 17:21:39 +01:00
this.statsd.incr('auth.reset.success', 1);
2015-07-09 23:01:43 +01:00
this.redirect('/');
} else if (token.length) {
2015-08-22 16:16:15 +01:00
const tokenUser = yield validateResetToken.call(this, token);
2015-07-09 23:01:43 +01:00
if (!tokenUser) {
2015-08-09 17:21:39 +01:00
this.statsd.incr('auth.reset.fail', 1);
return yield this.render('forgot', {error: 'Invalid password reset token. It might be expired, or has already been used.', token: null, csrf: this.csrf});
2015-07-09 23:01:43 +01:00
} else {
return yield this.render('forgot', {token: token, csrf: this.csrf});
2015-07-09 23:01:43 +01:00
}
} else {
return yield this.render('forgot', {token: null, csrf: this.csrf});
2015-07-09 23:01:43 +01:00
}
}
export function* logout() {
2015-08-09 17:21:39 +01:00
this.statsd.incr('auth.logout', 1);
2015-07-09 23:01:43 +01:00
this.cookies.set('r', {expires: new Date(1), path: '/'});
this.session = null;
this.redirect('/');
}
export function* activate(code) {
2015-08-22 16:16:15 +01:00
if (yield activateUser.call(this, code)) {
2015-08-09 17:21:39 +01:00
this.statsd.incr('auth.activation', 1);
}
2015-07-09 23:01:43 +01:00
this.redirect('/');
}