UNPKG

@furystack/rest-service

Version:

Repository implementation for FuryStack

18 lines 812 B
import { isAuthenticated } from '@furystack/core'; import { sleepAsync } from '@furystack/utils'; import { HttpUserContext } from './http-user-context.js'; import { JsonResult } from './request-action-implementation.js'; export const Authenticate = () => (action) => { const wrapped = async (args) => { const { injector } = args; const authenticated = await isAuthenticated(injector); if (!authenticated) { await sleepAsync(Math.random() * 1000); return JsonResult({ error: 'unauthorized' }, 401, injector.getInstance(HttpUserContext).authentication.enableBasicAuth ? { 'WWW-Authenticate': 'Basic' } : {}); } return (await action(args)); }; wrapped.isAuthenticated = true; return wrapped; }; //# sourceMappingURL=authenticate.js.map