All files / src/pages/api session.ts

100% Statements 22/22
100% Branches 8/8
100% Functions 1/1
100% Lines 22/22

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 271x     1x 5x 5x   5x 3x 2x 2x 2x 2x 2x   5x 2x 2x 2x 5x 1x 1x 1x 1x 1x 5x  
import { STATUS_CODE } from '@shared/constants';
import type { APIContext } from 'astro';
 
export const POST = async (context: APIContext) => {
  try {
    const body = await context.request.json().catch(() => ({}));
 
    if (body?.user) {
      await context.session?.set('user', body.user);
      return new Response(
        JSON.stringify({ ok: true, action: 'login', user: body.user }),
        { status: STATUS_CODE.OK },
      );
    }
 
    await context.session?.delete('user');
    return new Response(JSON.stringify({ ok: true, action: 'logout' }), {
      status: STATUS_CODE.OK,
    });
  } catch (err) {
    console.error('Session API error:', err);
    return new Response(JSON.stringify({ ok: false, error: 'Session error' }), {
      status: STATUS_CODE.INTERNAL_SERVER_ERROR,
    });
  }
};