Auth

AuthServiceFactory

Factory statica per creare istanze di AuthService con configurazione consistente.

Cosa fa

AuthServiceFactory applica il Factory Pattern per centralizzare la creazione di istanze AuthService. Mantiene lo stesso stile dichiarativo delle altre factory del package (AuthStrategyFactory, CacheStrategyFactory, HttpClientFactory), facilitando refactoring e testing.

API

class AuthServiceFactory {
  static create(httpClient: IHttpClient, authStrategy: IAuthStrategy): IAuthService
}

create(httpClient, authStrategy)

ParametroTipoDescrizione
httpClientIHttpClientClient HTTP per le richieste a /auth/*
authStrategyIAuthStrategyStrategia di persistenza token

Ritorna un'istanza che implementa IAuthService (e internamente anche IAuthHandler).

Esempio

import {
  HttpClientFactory,
  AuthStrategyFactory,
  AuthServiceFactory,
} from '@pzeta/postgrest'

const httpClient = HttpClientFactory.createFetchClient()
const strategy = AuthStrategyFactory.createJwtStrategy()
const authService = AuthServiceFactory.create(httpClient, strategy)

// Collega l'handler per retry automatico su 401
httpClient.setAuthHandler(authService)

await authService.login({ email, password })

Quando usarla

Sempre, in luogo di new AuthService(...) diretto. Mantiene il codice client uniforme con le altre parti del package e isola eventuali parametri opzionali futuri (es. logger) dietro al contratto della factory.

Vedi anche