mirror of
https://github.com/wassname/talk.git
synced 2026-07-28 11:27:05 +08:00
* feat: suspending, banning, now propogation * feat: added email rendering + localization support * fix: fix related to lib * refactor: moved juicer to queue task * refactor: cleanup of job processor * refactor: improved error messaging around failed email * feat: initial forgot passwor impl * fix: fixed rebase errors * feat: send back Content-Language header with requests * feat: added ban email * feat: implemented forgotten password API * fix: linting * feat: support more emails * fix: promise patches * feat: initial confirm email API * feat: added rate limiting * feat: added URL support * feat: added email docs * fix: updated docs * chore: documentation review * fix: fixed build bug * feat: implement forgot password in auth popup * test: add tests + fixes * chore: rename StatelessComponent to FunctionComponent * fix: types and test fixes * chore: upgrade deps * fix: THANK YOU TESTS FOR SAVING MY A** * chore: reorder imports * chore: remove obsolete ! * feat: implement accounts bundle * refactor: review suggestion * fix: rebase upgrade error * fix: rebase bug * feat: reset password link support * test: add tests for account password reset page * fix: remove redirect uri * fix: revert local state changes
64 lines
1.6 KiB
TypeScript
64 lines
1.6 KiB
TypeScript
import uuid from "uuid/v1";
|
|
|
|
import { TenantNotFoundError } from "talk-server/errors";
|
|
import logger from "talk-server/logger";
|
|
import TenantCache from "talk-server/services/tenant/cache";
|
|
import { RequestHandler } from "talk-server/types/express";
|
|
|
|
export interface MiddlewareOptions {
|
|
cache: TenantCache;
|
|
passNoTenant?: boolean;
|
|
}
|
|
|
|
export const tenantMiddleware = ({
|
|
cache,
|
|
passNoTenant = false,
|
|
}: MiddlewareOptions): RequestHandler => async (req, res, next) => {
|
|
try {
|
|
if (!req.talk) {
|
|
const id = uuid();
|
|
|
|
// Write the ID on the request.
|
|
res.set("X-Trace-ID", id);
|
|
|
|
// The only call to `new Date()` as a part of the request process. This
|
|
// is passed around the request to ensure constant-time actions.
|
|
const now = new Date();
|
|
|
|
// Set Talk on the request.
|
|
req.talk = { id, now, logger: logger.child({ traceID: id }) };
|
|
}
|
|
|
|
// Set the Talk Tenant Cache on the request.
|
|
if (!req.talk.cache) {
|
|
req.talk.cache = {
|
|
// Attach the tenant cache to the request.
|
|
tenant: cache,
|
|
};
|
|
}
|
|
|
|
// Attach the tenant to the request.
|
|
const tenant = await cache.retrieveByDomain(req.hostname);
|
|
if (!tenant) {
|
|
if (passNoTenant) {
|
|
return next();
|
|
}
|
|
|
|
return next(new TenantNotFoundError(req.hostname));
|
|
}
|
|
|
|
// Attach the tenant to the request.
|
|
req.talk.tenant = tenant;
|
|
|
|
// Attach the tenant's language to the request.
|
|
res.setHeader("Content-Language", tenant.locale);
|
|
|
|
// Attach the tenant to the view locals.
|
|
res.locals.tenant = tenant;
|
|
|
|
next();
|
|
} catch (err) {
|
|
next(err);
|
|
}
|
|
};
|