(fn: (params: OptionalAuthContext) => Promise<T>)
| 46 | }; |
| 47 | |
| 48 | export const withOptionalAuth = async <T>(fn: (params: OptionalAuthContext) => Promise<T>) => { |
| 49 | const authContext = await getAuthContext(); |
| 50 | if (isServiceError(authContext)) { |
| 51 | return authContext; |
| 52 | } |
| 53 | |
| 54 | if ( |
| 55 | (!authContext.user || !authContext.role) && |
| 56 | !(await isAnonymousAccessEnabled()) |
| 57 | ) { |
| 58 | return notAuthenticated(); |
| 59 | } |
| 60 | |
| 61 | return fn(authContext); |
| 62 | }; |
| 63 | |
| 64 | export const getAuthContext = async (): Promise<OptionalAuthContext | ServiceError> => { |
| 65 | const authResult = await getAuthenticatedUser(); |
no test coverage detected