You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Hydro/hydro/handler/user.js

175 lines
6.2 KiB
JavaScript

5 years ago
const { Route, Handler } = require('../service/server.js');
const user = require('../model/user');
const token = require('../model/token');
const system = require('../model/system');
const { sendMail } = require('../lib/mail');
const misc = require('../lib/misc');
const {
UserAlreadyExistError, InvalidTokenError, VerifyPasswordError,
UserNotFoundError, LoginError, SystemError,
4 years ago
PermissionError, BlacklistedError,
5 years ago
} = require('../error');
class UserLoginHandler extends Handler {
async get() {
this.response.template = 'user_login.html';
}
5 years ago
async post({
domainId, uname, password, rememberme = false,
}) {
const udoc = await user.getByUname(domainId, uname);
if (!udoc) throw new LoginError(uname);
if (udoc) udoc.checkPassword(password);
await user.setById(udoc._id, { loginat: new Date(), loginip: this.request.ip });
4 years ago
if (udoc.ban) throw new BlacklistedError(uname);
this.session.uid = udoc._id;
this.session.rememberme = rememberme;
5 years ago
const referer = this.request.headers.referer || '/';
this.response.redirect = referer.endsWith('/login') ? '/' : referer;
}
}
class UserLogoutHandler extends Handler {
async get() {
this.response.template = 'user_logout.html';
}
5 years ago
async post() {
this.session.uid = 1;
}
}
class UserRegisterHandler extends Handler {
4 years ago
async prepare() { // eslint-disable-line class-methods-use-this
if (!await system.get('user.register')) {
throw new PermissionError('Register');
}
}
5 years ago
async get() {
this.response.template = 'user_register.html';
}
5 years ago
async post({ mail }) {
if (await user.getByEmail('system', mail, true)) throw new UserAlreadyExistError(mail);
this.limitRate('send_mail', 3600, 30);
5 years ago
const t = await token.add(
token.TYPE_REGISTRATION,
5 years ago
await system.get('registration_token_expire_seconds'),
5 years ago
{ mail },
);
5 years ago
if (await system.get('smtp.user')) {
const m = await this.renderHTML('user_register_mail.html', {
4 years ago
url: `${await system.get('server.url')}/register/${t[0]}`,
});
await sendMail(mail, 'Sign Up', 'user_register_mail', m);
this.response.template = 'user_register_mail_sent.html';
} else {
this.response.redirect = `/register/${t[0]}`;
}
}
}
class UserRegisterWithCodeHandler extends Handler {
async get({ code }) {
this.response.template = 'user_register_with_code.html';
5 years ago
const { mail } = await token.get(code, token.TYPE_REGISTRATION);
if (!mail) throw new InvalidTokenError(token.TYPE_REGISTRATION, code);
this.response.body = { mail };
}
5 years ago
async post({
code, password, verifyPassword, uname,
}) {
const { mail } = await token.get(code, token.TYPE_REGISTRATION);
if (!mail) throw new InvalidTokenError(token.TYPE_REGISTRATION, code);
5 years ago
if (password !== verifyPassword) throw new VerifyPasswordError();
const uid = await system.inc('user');
5 years ago
await user.create({
uid, uname, password, mail, regip: this.request.ip,
});
await token.delete(code, token.TYPE_REGISTRATION);
this.session.uid = uid;
this.response.redirect = '/';
}
}
class UserLostPassHandler extends Handler {
async get() {
5 years ago
if (!await system.get('smtp.user')) throw new SystemError('Cannot send mail');
this.response.template = 'user_lostpass.html';
}
5 years ago
async post({ mail }) {
5 years ago
if (!await system.get('smtp.user')) throw new SystemError('Cannot send mail');
5 years ago
const udoc = await user.getByEmail(mail);
if (!udoc) throw new UserNotFoundError(mail);
5 years ago
const tid = await token.add(
token.TYPE_LOSTPASS,
5 years ago
await system.get('lostpass_token_expire_seconds'),
5 years ago
{ uid: udoc._id },
);
5 years ago
const m = await this.renderHTML('user_lostpass_mail', { url: `/lostpass/${tid}`, uname: udoc.uname });
await sendMail(mail, 'Lost Password', 'user_lostpass_mail', m);
this.response.template = 'user_lostpass_mail_sent.html';
}
}
class UserLostPassWithCodeHandler extends Handler {
async get({ domainId, code }) {
5 years ago
const tdoc = await token.get(code, token.TYPE_LOSTPASS);
if (!tdoc) throw new InvalidTokenError(token.TYPE_LOSTPASS, code);
const udoc = await user.getById(domainId, tdoc.uid);
this.response.body = { uname: udoc.uname };
}
5 years ago
async post({ code, password, verifyPassword }) {
const tdoc = await token.get(code, token.TYPE_LOSTPASS);
if (!tdoc) throw new InvalidTokenError(token.TYPE_LOSTPASS, code);
5 years ago
if (password !== verifyPassword) throw new VerifyPasswordError();
5 years ago
await user.setPassword(tdoc.uid, password);
await token.delete(code, token.TYPE_LOSTPASS);
this.response.redirect = '/';
}
}
class UserDetailHandler extends Handler {
async get({ domainId, uid }) {
5 years ago
const isSelfProfile = this.user._id === uid;
const udoc = await user.getById(domainId, uid, true);
5 years ago
const sdoc = await token.getMostRecentSessionByUid(uid);
5 years ago
this.response.template = 'user_detail.html';
5 years ago
this.response.body = { isSelfProfile, udoc, sdoc };
}
}
class UserSearchHandler extends Handler {
async get({ domainId, q, exactMatch = false }) {
let udocs;
5 years ago
if (exactMatch) udocs = [];
else udocs = await user.getPrefixList(q, 20);
const udoc = await user.getById(domainId, parseInt(q));
if (udoc) udocs.push(udoc);
5 years ago
for (const i in udocs) {
if (udocs[i].gravatar) {
udocs[i].gravatar_url = misc.gravatar(udocs[i].gravatar);
5 years ago
}
}
this.response.body = udocs;
}
}
5 years ago
async function apply() {
4 years ago
Route('/login', UserLoginHandler);
Route('/register', UserRegisterHandler);
Route('/register/:code', UserRegisterWithCodeHandler);
Route('/logout', UserLogoutHandler);
4 years ago
Route('/lostpass', UserLostPassHandler);
Route('/lostpass/:code', UserLostPassWithCodeHandler);
Route('/user/search', UserSearchHandler);
Route('/user/:uid', UserDetailHandler);
5 years ago
}
4 years ago
global.Hydro.handler.user = module.exports = apply;