withUser throws an error if the user is not authenticated

This commit is contained in:
2024-01-09 14:39:17 +01:00
parent 90d443b747
commit c7b4829d03

View File

@@ -59,16 +59,11 @@ export const isAuthErrorMessage = (obj: any): obj is AuthErrorMessage => {
return (obj.message && obj.errors && obj.errors.message); return (obj.message && obj.errors && obj.errors.message);
} }
export const withUser = <T>(fn: (user: AuthenticatedUser, ...args:any) => Promise<T>) => async (...args:any) => { export const withUser = <T, A extends any[]>(fn: (user: AuthenticatedUser, ...args:A) => Promise<T>) => async (...args:A) => {
const session = await auth(); const session = await auth();
if(!session) { if(!session) {
return({ throw new Error("Not authenticated")
errors: {
message: "Not authenticated",
},
message: "Not authenticated",
} as AuthErrorMessage);
} }
const { user } = session; const { user } = session;