hostr/web/routes/user.js

160 lines
4.5 KiB
JavaScript
Raw Permalink Normal View History

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