opengenerals/server/middlewares/auth-required.mjs

17 lines
558 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.' };
} else {
return next();
}
}