11import { AuthError } from '@a-type/auth' ;
22import { AlefError } from '@alef/common' ;
3+ import { Context } from 'hono' ;
34import { ZodError } from 'zod' ;
5+ import { sessions } from '../public-api/auth/session' ;
6+ import { Env } from '../public-api/config/ctx' ;
47
5- export function handleError ( reason : unknown ) : Response {
8+ export function handleError ( reason : unknown , ctx : Context < Env > ) : Response {
69 if ( AlefError . isInstance ( reason ) ) {
710 if ( reason . code > AlefError . Code . InternalServerError ) {
811 console . error ( 'Unexpected AlefError:' , reason ) ;
@@ -11,12 +14,19 @@ export function handleError(reason: unknown): Response {
1114 }
1215
1316 if ( reason instanceof AuthError ) {
14- return new Response ( reason . message , {
15- status : reason . statusCode ,
16- headers : {
17- 'Content-Type' : 'text/plain' ,
18- } ,
19- } ) ;
17+ // for invalid sessions, log the user out.
18+ if ( reason . message === AuthError . Messages . InvalidSession || reason . message === AuthError . Messages . InvalidRefreshToken ) {
19+ const { headers } = sessions . clearSession ( ctx ) ;
20+ return new Response ( reason . message , {
21+ status : 401 ,
22+ headers : {
23+ 'Content-Type' : 'text/plain' ,
24+ 'x-alef-error' : AlefError . Code . Unauthorized . toString ( ) ,
25+ ...headers ,
26+ } ,
27+ } ) ;
28+ }
29+ return reason . toResponse ( ) ;
2030 }
2131
2232 if ( reason instanceof ZodError ) {
0 commit comments