Skip to main content

HTTP entry point

import {
withHttpAuth,
sanitizeReturnUrl,
decodeJwtPayload,
HttpAuthProvider,
ProfileHttpAuthProvider,
HTTP_AUTH_CONFIG,
HTTP_AUTH_CONFIGURATION_DEFAULTS,
type HttpAuthConfiguration,
type HttpAuthEndpoints,
type HttpAuthTokenResponse,
} from '@ismailza/ngx-auth-client/http';

No extra peer dependency. Requires provideHttpClient() and provideRouter(). See the guide for the backend contract.

withHttpAuth()​

function withHttpAuth(config: HttpAuthConfiguration): AuthFeature;

Configures your own backend as the identity provider. Pass the result to provideAuth().

provideAuth(
withHttpAuth({
endpoints: {
signIn: '/api/auth/login',
refresh: '/api/auth/refresh',
logout: '/api/auth/logout',
},
}),
);

Provides ProfileHttpAuthProvider when endpoints.profile is set, and HttpAuthProvider otherwise.

HttpAuthConfiguration​

OptionTypeDefaultDescription
endpointsHttpAuthEndpoints—Required. Backend endpoints
loginRoutestring'/login'App route hosting the login form; login() navigates here
returnUrlParamstring'returnUrl'Query parameter carrying the page the user wanted
postLogoutRoutestring'/'Where logout() lands without a redirectUri
withCredentialsbooleantrueSend cookies with auth requests — needed cross-origin
minTokenValiditynumber30Seconds ahead of expiry to refresh, capped at half the lifetime
rejectionStatusesreadonly number[][401]Statuses meaning bad credentials / no session
xsrf{ cookieName?, headerName? } | falseXSRF-TOKEN / X-XSRF-TOKENXSRF header on same-origin POSTs
headersReadonly<Record<string, string>>{}Static headers on every auth request
mapTokenResponse(body: unknown) => HttpAuthTokenResponsedefaultMapTokenResponseReads the token from a signIn/refresh response
mapRoles(claims: Claims) => readonly string[]defaultMapRolesReads roles — roles claim, then authorities; strings, { authority } objects or a delimited string
mapProfile(body: unknown) => UserProfiledefaultMapProfileMaps the profile response

HttpAuthEndpoints​

FieldMethodDescription
signInPOSTRequired. Credentials in, access token out; sets the refresh cookie
refreshPOSTRequired. Refresh cookie in, access token out
logoutPOSTEnds the server session and clears the cookie
profileGETUser profile. Enables loadProfile()

URLs are used exactly as given: auth requests bypass HttpClient interceptors. The adapter adds the XSRF header and headers itself.

HttpAuthTokenResponse​

interface HttpAuthTokenResponse {
readonly accessToken: string;
/** Seconds. Preferred over the JWT `exp` claim. */
readonly expiresIn?: number;
/** Supply for opaque tokens; otherwise decoded from the JWT. */
readonly claims?: Claims;
}

HttpAuthProvider​

The adapter. Implements AuthProvider and SupportsCredentialSignIn.

MemberBehaviour
init()Calls refresh to restore the session. No-op on the server
getToken()Returns the token, refreshing first if near expiry. Concurrent calls share one refresh
signIn(creds)Calls signIn. Rejects with InvalidCredentialsError on a rejectionStatuses code
login(options?)Navigates to loginRoute, passing an in-app returnUrl; off-site URLs are dropped
logout(options?)Clears locally, calls logout, navigates. Rejects if the backend call failed

Subclasses can override the protected onSessionChange(session, reason) hook, where reason is 'signIn', 'refresh', 'logout' or 'expired'. It is not called when nothing changed, such as a refused refresh on an anonymous page load.

ProfileHttpAuthProvider​

HttpAuthProvider plus SupportsProfile. loadProfile() calls endpoints.profile with the bearer token; the profile is cleared on sign-in, logout and expiry.

sanitizeReturnUrl()​

function sanitizeReturnUrl(value: unknown, fallback = '/'): string;

Returns value when it is an in-app path, and fallback otherwise. Rejects absolute URLs, //host, /\host and control characters. Use it on the login page before navigating to returnUrl.

decodeJwtPayload()​

function decodeJwtPayload(token: string): Claims;

Decodes a JWT payload (base64url, UTF-8) without verifying the signature. Throws on anything that is not a three-part token with a JSON object payload.

HTTP_AUTH_CONFIG​

const HTTP_AUTH_CONFIG: InjectionToken<ResolvedHttpAuthConfiguration>;

The resolved configuration, with defaults applied.