A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
// SPDX-License-Identifier: AGPL-3.0-or-later
import type {AuthTokenType, HonoEnv} from '@app/api/types/HonoEnv';
import {AccessDeniedError} from '@fluxer/errors/src/domains/core/AccessDeniedError';
import {UnauthorizedError} from '@fluxer/errors/src/domains/core/UnauthorizedError';
import {createMiddleware} from 'hono/factory';
function ensureOAuth2BearerRouteSupport(
authTokenType: AuthTokenType | undefined,
oauthBearerAllowed: boolean | undefined,
): void {
if (authTokenType === 'bearer' && !oauthBearerAllowed) {
throw new AccessDeniedError();
}
}
export const LoginRequired = createMiddleware<HonoEnv>(async (ctx, next) => {
const user = ctx.get('user');
if (!user) {
throw new UnauthorizedError();
}
ensureOAuth2BearerRouteSupport(ctx.get('authTokenType'), ctx.get('oauthBearerAllowed'));
await next();
});
export const DefaultUserOnly = createMiddleware<HonoEnv>(async (ctx, next) => {
const user = ctx.get('user');
if (!user) {
throw new UnauthorizedError();
}
if (user.isBot) {
throw new AccessDeniedError();
}
await next();
});
export const BotOnly = createMiddleware<HonoEnv>(async (ctx, next) => {
const user = ctx.get('user');
if (!user) {
throw new UnauthorizedError();
}
if (!user.isBot) {
throw new AccessDeniedError();
}
await next();
});