mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
Hello airbnb
This commit is contained in:
1 parent
f9468536de
commit
8c195661d7
78 files changed
+2032
-1471
No files matched your search
+61
-39
@@ -1,22 +1,24 @@
|
||||
const
|
||||
{ Route, Handler } = require('../service/server.js'),
|
||||
user = require('../model/user'),
|
||||
token = require('../model/token'),
|
||||
system = require('../model/system'),
|
||||
{ sendMail } = require('../lib/mail'),
|
||||
misc = require('../lib/misc'),
|
||||
validator = require('../lib/validator'),
|
||||
options = require('../options'),
|
||||
{ PERM_REGISTER_USER, PERM_LOGGEDIN } = require('../permission'),
|
||||
{ UserAlreadyExistError, InvalidTokenError, VerifyPasswordError,
|
||||
UserNotFoundError, LoginError, SystemError } = require('../error');
|
||||
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 validator = require('../lib/validator');
|
||||
const options = require('../options');
|
||||
const { PERM_REGISTER_USER, PERM_LOGGEDIN } = require('../permission');
|
||||
const {
|
||||
UserAlreadyExistError, InvalidTokenError, VerifyPasswordError,
|
||||
UserNotFoundError, LoginError, SystemError,
|
||||
} = require('../error');
|
||||
|
||||
class UserLoginHandler extends Handler {
|
||||
async get() {
|
||||
this.response.template = 'user_login.html';
|
||||
}
|
||||
|
||||
async post({ uname, password, rememberme = false }) {
|
||||
let udoc = await user.getByUname(uname);
|
||||
const udoc = await user.getByUname(uname);
|
||||
if (!udoc) throw new LoginError(uname);
|
||||
if (udoc) udoc.checkPassword(password);
|
||||
await user.setById(udoc._id, { loginat: new Date(), loginip: this.request.ip });
|
||||
@@ -25,7 +27,7 @@ class UserLoginHandler extends Handler {
|
||||
this.session.uid = udoc._id;
|
||||
this.session.rememberme = rememberme;
|
||||
this.response.body = {};
|
||||
let referer = this.request.headers.referer || '/';
|
||||
const referer = this.request.headers.referer || '/';
|
||||
this.response.redirect = referer.endsWith('/login') ? '/' : referer;
|
||||
}
|
||||
}
|
||||
@@ -34,9 +36,11 @@ class UserLogoutHandler extends Handler {
|
||||
super(ctx);
|
||||
this.checkPerm(PERM_LOGGEDIN);
|
||||
}
|
||||
|
||||
async get() {
|
||||
this.response.template = 'user_logout.html';
|
||||
}
|
||||
|
||||
async post() {
|
||||
this.session = { uid: 1 };
|
||||
this.response.body = {};
|
||||
@@ -47,16 +51,22 @@ class UserRegisterHandler extends Handler {
|
||||
super(ctx);
|
||||
this.checkPerm(PERM_REGISTER_USER);
|
||||
}
|
||||
|
||||
async get() {
|
||||
this.response.template = 'user_register.html';
|
||||
}
|
||||
|
||||
async post({ mail }) {
|
||||
validator.checkEmail(mail);
|
||||
if (await user.getByEmail(mail, true)) throw new UserAlreadyExistError(mail);
|
||||
this.limitRate('send_mail', 3600, 30);
|
||||
let t = await token.add(token.TYPE_REGISTRATION, options.registration_token_expire_seconds, { mail });
|
||||
const t = await token.add(
|
||||
token.TYPE_REGISTRATION,
|
||||
options.registration_token_expire_seconds,
|
||||
{ mail },
|
||||
);
|
||||
if (options.smtp.user) {
|
||||
let m = await this.renderHTML('user_register_mail', { url: `/register/${t}` });
|
||||
const m = await this.renderHTML('user_register_mail', { url: `/register/${t}` });
|
||||
await sendMail(mail, 'Sign Up', 'user_register_mail', m);
|
||||
this.response.body = {};
|
||||
this.response.template = 'user_register_mail_sent.html';
|
||||
@@ -70,18 +80,24 @@ class UserRegisterWithCodeHandler extends Handler {
|
||||
super(ctx);
|
||||
this.checkPerm(PERM_REGISTER_USER);
|
||||
}
|
||||
|
||||
async get({ code }) {
|
||||
this.response.template = 'user_register_with_code.html';
|
||||
let { mail } = await token.get(code, token.TYPE_REGISTRATION);
|
||||
const { mail } = await token.get(code, token.TYPE_REGISTRATION);
|
||||
if (!mail) throw new InvalidTokenError(token.TYPE_REGISTRATION, code);
|
||||
this.response.body = { mail };
|
||||
}
|
||||
async post({ code, password, verify_password, uname }) {
|
||||
let { mail } = await token.get(code, token.TYPE_REGISTRATION);
|
||||
|
||||
async post({
|
||||
code, password, verifyPassword, uname,
|
||||
}) {
|
||||
const { mail } = await token.get(code, token.TYPE_REGISTRATION);
|
||||
if (!mail) throw new InvalidTokenError(token.TYPE_REGISTRATION, code);
|
||||
if (password != verify_password) throw new VerifyPasswordError();
|
||||
let uid = await system.incUserCounter();
|
||||
await user.create({ uid, uname, password, mail, regip: this.request.ip });
|
||||
if (password !== verifyPassword) throw new VerifyPasswordError();
|
||||
const uid = await system.incUserCounter();
|
||||
await user.create({
|
||||
uid, uname, password, mail, regip: this.request.ip,
|
||||
});
|
||||
await token.delete(code, token.TYPE_REGISTRATION);
|
||||
this.session.uid = uid;
|
||||
this.response.body = {};
|
||||
@@ -93,19 +109,21 @@ class UserLostPassHandler extends Handler {
|
||||
if (!options.smtp.user) throw new SystemError('Cannot send mail');
|
||||
super(ctx);
|
||||
}
|
||||
|
||||
async get() {
|
||||
this.response.template = 'user_lostpass.html';
|
||||
}
|
||||
|
||||
async post({ mail }) {
|
||||
validator.checkEmail(mail);
|
||||
let udoc = await user.getByEmail(mail);
|
||||
const udoc = await user.getByEmail(mail);
|
||||
if (!udoc) throw new UserNotFoundError(mail);
|
||||
let tid = await token.add(
|
||||
const tid = await token.add(
|
||||
token.TYPE_LOSTPASS,
|
||||
options.lostpass_token_expire_seconds,
|
||||
{ uid: udoc._id }
|
||||
{ uid: udoc._id },
|
||||
);
|
||||
let m = await this.renderHTML('user_lostpass_mail', { url: `/lostpass/${tid}`, uname: udoc.uname });
|
||||
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.body = {};
|
||||
this.response.template = 'user_lostpass_mail_sent.html';
|
||||
@@ -113,15 +131,16 @@ class UserLostPassHandler extends Handler {
|
||||
}
|
||||
class UserLostPassWithCodeHandler extends Handler {
|
||||
async get({ code }) {
|
||||
let tdoc = await token.get(code, token.TYPE_LOSTPASS);
|
||||
const tdoc = await token.get(code, token.TYPE_LOSTPASS);
|
||||
if (!tdoc) throw new InvalidTokenError(token.TYPE_LOSTPASS, code);
|
||||
let udoc = await user.getById(tdoc.uid);
|
||||
const udoc = await user.getById(tdoc.uid);
|
||||
this.response.body = { uname: udoc.uname };
|
||||
}
|
||||
async post({ code, password, verify_password }) {
|
||||
let tdoc = await token.get(code, token.TYPE_LOSTPASS);
|
||||
|
||||
async post({ code, password, verifyPassword }) {
|
||||
const tdoc = await token.get(code, token.TYPE_LOSTPASS);
|
||||
if (!tdoc) throw new InvalidTokenError(token.TYPE_LOSTPASS, code);
|
||||
if (password != verify_password) throw new VerifyPasswordError();
|
||||
if (password !== verifyPassword) throw new VerifyPasswordError();
|
||||
await user.setPassword(tdoc.uid, password);
|
||||
await token.delete(code, token.TYPE_LOSTPASS);
|
||||
this.response.redirect = '/';
|
||||
@@ -132,28 +151,31 @@ class UserDetailHandler extends Handler {
|
||||
super(ctx);
|
||||
this.response.template = 'user_detail.html';
|
||||
}
|
||||
|
||||
async get({ uid }) {
|
||||
let isSelfProfile = this.ctx.state.user._id == uid;
|
||||
let udoc = await user.getById(uid);
|
||||
const isSelfProfile = this.ctx.state.user._id === uid;
|
||||
const udoc = await user.getById(uid);
|
||||
if (!udoc) throw new UserNotFoundError(uid);
|
||||
let sdoc = await token.getMostRecentSessionByUid(uid);
|
||||
const sdoc = await token.getMostRecentSessionByUid(uid);
|
||||
this.ctx.body = { isSelfProfile, udoc, sdoc };
|
||||
}
|
||||
}
|
||||
class UserSearchHandler extends Handler {
|
||||
async get({ q, exact_match = false }) {
|
||||
async get({ q, exactMatch = false }) {
|
||||
let udocs;
|
||||
if (exact_match) udocs = [];
|
||||
if (exactMatch) udocs = [];
|
||||
else udocs = await user.getPrefixList(q, 20);
|
||||
try {
|
||||
let udoc = await user.getById(parseInt(q));
|
||||
const udoc = await user.getById(parseInt(q));
|
||||
if (udoc) udocs.insert(0, udoc);
|
||||
} catch (e) {
|
||||
/* Ignore */
|
||||
}
|
||||
for (let i in udocs)
|
||||
if (udocs[i].gravatar)
|
||||
for (const i in udocs) {
|
||||
if (udocs[i].gravatar) {
|
||||
udocs[i].gravatar_url = misc.gravatar_url[udocs[i].gravatar];
|
||||
}
|
||||
}
|
||||
this.ctx.body = { udocs };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user