import { Authentication , Authenticator , Identity } from "~/types" ; export const createAuthentication = < TIdentity extends Identity = Identity > ( ) : Authentication < TIdentity > => { let identity ; const authenticators = [ ] ; return { getIdentity < TIdentity extends Identity = Identity > ( ) { return identity as unknown as TIdentity ; } , setIdentity ( newIdentity ) { identity = newIdentity ; } , addAuthenticator ( authenticator ) { authenticators . push ( authenticator ) ; } , getAuthenticators ( ) { return authenticators ; } , async authenticate ( token ) { for ( const authenticator of authenticators ) { const identity = await authenticator ( token ) ; if ( identity ) { this . setIdentity ( identity ) ; return ; } } } } ; } ;