All files / api/src/v1/super-agents/auth status.ts

70.83% Statements 17/24
75% Branches 3/4
100% Functions 0/0
70.83% Lines 17/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 351x   1x 1x 1x 1x   1x           1x 3x 3x   3x 1x 1x   2x 2x 2x 2x                 3x  
import { getAccessPassword, getAuthJwtSecret } from '@api/constants';
import type { AppEnv } from '@api/types/hono';
import { AUTH_COOKIE_NAME } from '@api/utils/auth-cookie';
import { Hono } from 'hono';
import { getCookie } from 'hono/cookie';
import { verify } from 'hono/jwt';
 
export const statusRouter = new Hono<AppEnv>()
  /**
   * Returns authentication status:
   * - authRequired: whether ACCESS_PASSWORD is set (auth is enabled)
   * - authenticated: whether the user has a valid JWT cookie
   */
  .get(async (c): Promise<Response> => {
    const accessPassword = getAccessPassword(c);
    const authRequired = Boolean(accessPassword);
 
    if (!authRequired) {
      return c.json({ authRequired: false, authenticated: true });
    }
 
    const accessToken = getCookie(c, AUTH_COOKIE_NAME);
    if (!accessToken) {
      return c.json({ authRequired: true, authenticated: false });
    }
 
    const jwtSecret = getAuthJwtSecret(c);
    try {
      await verify(accessToken, jwtSecret);
      return c.json({ authRequired: true, authenticated: true });
    } catch {
      return c.json({ authRequired: true, authenticated: false });
    }
  });