opengenerals/server/middlewares/auth-required.mjs
2024-03-06 20:41:04 +08:00

17 lines
574 B
JavaScript

/** @typedef {import('koa').Context} Context */
/** @typedef {import('koa').Next} Next */
/**
* Middleware function to check if user is logged in.
* @param {Context} ctx - Koa context object.
* @param {Next} next - Next middleware function.
*/
export function loginRequired(ctx, next) {
if (ctx.state.user == null || ctx.state.user.auth_step != 'done') {
ctx.status = 401; // 401 (Unauthorized)
ctx.body = { error: 'You are not identified or your authentication process is incomplete.', errno: 'EAUTH' };
} else {
return next();
}
}