hostr/web/routes/user.js

143 lines
4.3 KiB
JavaScript
Raw Normal View History

2016-06-06 15:37:00 +01:00
import {
authenticate, setupSession, signup as signupUser, activateUser, sendResetToken,
validateResetToken, updatePassword,
} from '../lib/auth';
2016-06-19 10:14:47 -07:00
import models from '../../models';
2015-08-23 16:50:40 +01:00
import debugname from 'debug';
const debug = debugname('hostr-web:user');
2015-07-09 23:01:43 +01:00
export function* signin() {
if (!this.request.body.email) {
2016-06-06 15:37:00 +01:00
yield this.render('signin', { csrf: this.csrf });
return;
2015-07-09 23:01:43 +01:00
}
2015-08-22 23:07:34 +01:00
this.statsd.incr('auth.attempt', 1);
this.assertCSRF(this.request.body);
2015-08-22 16:16:15 +01:00
const user = yield authenticate.call(this, this.request.body.email, this.request.body.password);
2015-08-23 22:12:32 +01:00
if (!user) {
2015-08-09 17:21:39 +01:00
this.statsd.incr('auth.failure', 1);
2016-06-06 15:37:00 +01:00
yield this.render('signin', { error: 'Invalid login details', csrf: this.csrf });
return;
2015-07-09 23:01:43 +01:00
} else if (user.activationCode) {
2016-06-06 15:37:00 +01:00
yield this.render('signin', {
error: 'Your account hasn\'t been activated yet. Check your for an activation email.',
csrf: this.csrf,
});
return;
2015-07-09 23:01:43 +01:00
}
2015-08-23 22:12:32 +01:00
this.statsd.incr('auth.success', 1);
yield setupSession.call(this, user);
this.redirect('/');
2015-07-09 23:01:43 +01:00
}
export function* signup() {
if (!this.request.body.email) {
2016-06-06 15:37:00 +01:00
yield this.render('signup', { csrf: this.csrf });
return;
2015-07-09 23:01:43 +01:00
}
2015-08-22 23:07:34 +01:00
this.assertCSRF(this.request.body);
2015-07-09 23:01:43 +01:00
if (this.request.body.email !== this.request.body.confirm_email) {
2016-06-06 15:37:00 +01:00
yield this.render('signup', { error: 'Emails do not match.', csrf: this.csrf });
return;
2015-07-09 23:01:43 +01:00
} else if (this.request.body.email && !this.request.body.terms) {
2016-06-06 15:37:00 +01:00
yield this.render('signup', { error: 'You must agree to the terms of service.',
csrf: this.csrf });
return;
2015-07-09 23:01:43 +01:00
} else if (this.request.body.password && this.request.body.password.length < 7) {
2016-06-06 15:37:00 +01:00
yield this.render('signup', { error: 'Password must be at least 7 characters long.',
csrf: this.csrf });
return;
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) {
2016-06-06 15:37:00 +01:00
yield this.render('signup', { error: e.message, csrf: this.csrf });
return;
2015-07-09 23:01:43 +01:00
}
2015-08-09 17:21:39 +01:00
this.statsd.incr('auth.signup', 1);
2016-06-06 15:37:00 +01:00
yield this.render('signup', {
message: 'Thanks for signing up, we\'ve sent you an email to activate your account.',
csrf: '',
});
return;
2015-07-09 23:01:43 +01:00
}
2015-08-22 23:07:34 +01:00
export function* forgot() {
const token = this.params.token;
2015-08-23 16:50:40 +01:00
if (this.request.body.password) {
2015-07-09 23:01:43 +01:00
if (this.request.body.password.length < 7) {
2016-06-06 15:37:00 +01:00
yield this.render('forgot', {
error: 'Password needs to be at least 7 characters long.',
csrf: this.csrf,
token,
});
return;
2015-07-09 23:01:43 +01:00
}
2015-08-23 16:50:40 +01:00
this.assertCSRF(this.request.body);
2016-06-19 10:14:47 -07:00
const user = yield validateResetToken(token);
yield updatePassword(user.id, this.request.body.password);
yield models.reset.deleteById(token);
yield setupSession(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('/');
2015-08-23 16:50:40 +01:00
} else if (token) {
2016-06-19 10:14:47 -07:00
const tokenUser = yield validateResetToken(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);
2016-06-06 15:37:00 +01:00
yield this.render('forgot', {
error: 'Invalid password reset token. It might be expired, or has already been used.',
csrf: this.csrf,
token: null,
});
return;
2015-07-09 23:01:43 +01:00
}
2016-06-06 15:37:00 +01:00
yield this.render('forgot', { csrf: this.csrf, token });
return;
2015-08-23 16:50:40 +01:00
} else if (this.request.body.email) {
this.assertCSRF(this.request.body);
try {
2015-08-23 22:12:32 +01:00
const email = this.request.body.email;
2015-08-23 16:50:40 +01:00
yield sendResetToken.call(this, email);
this.statsd.incr('auth.reset.request', 1);
2016-06-06 15:37:00 +01:00
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`,
csrf: this.csrf,
token: null,
});
return;
2015-08-23 16:50:40 +01:00
} catch (error) {
debug(error);
}
2015-07-09 23:01:43 +01:00
} else {
2016-06-06 15:37:00 +01:00
yield this.render('forgot', { csrf: this.csrf, token: null });
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);
2016-06-06 15:37:00 +01:00
this.cookies.set('r', { expires: new Date(1), path: '/' });
2015-07-09 23:01:43 +01:00
this.session = null;
this.redirect('/');
}
2015-08-22 23:07:34 +01:00
export function* activate() {
const code = this.params.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-08-22 23:07:34 +01:00
this.redirect('/');
} else {
this.throw(400);
2015-08-09 17:21:39 +01:00
}
2015-07-09 23:01:43 +01:00
}