Skip to content

AuthService

AuthService is an injectable Angular service that manages OIDC authentication state using Angular signals. It wraps OidcClient and provides reactive state and methods for login, logout, token refresh, and profile fetching.

Terminal window
npm install oidc-js-angular
import { Component, inject } from "@angular/core";
import { AuthService } from "oidc-js-angular";
@Component({
selector: "app-profile",
template: `
@if (auth.isLoading()) {
<p>Loading...</p>
} @else if (auth.isAuthenticated()) {
<p>Hello, {{ auth.user()?.claims.sub }}</p>
<button (click)="auth.logout()">Log out</button>
} @else {
<button (click)="auth.login()">Log in</button>
}
`,
})
export class ProfileComponent {
auth = inject(AuthService);
}

All state properties are Angular read-only signals that update reactively.

SignalTypeDescription
user()AuthUser | nullThe authenticated user (claims + profile)
isAuthenticated()booleanWhether the user is logged in
isLoading()booleanWhether initialization is in progress
error()Error | nullThe most recent auth error
tokens()AuthTokensCurrent access, id, and refresh tokens
MethodDescription
login(options?)Redirect to IdP login. Options: returnTo, extraParams
logout()Clear state and redirect to IdP logout endpoint
refresh()Exchange refresh token for new tokens
fetchProfile()Fetch the UserInfo endpoint and update user().profile
interface AuthTokens {
access: string | null;
id: string | null;
refresh: string | null;
expiresAt: number | null;
}

Tokens are stored in memory only. They are never persisted to localStorage or sessionStorage.