A comprehensive Angular library for OAuth2 authentication with PKCE (Proof Key for Code Exchange) support. This library provides secure, configurable, and easy-to-use OAuth2 authentication for Angular applications.
- âś… PKCE Support: Implements OAuth2 PKCE flow with SHA-256 for enhanced security
- âś… Automatic Token Refresh: Handles token refresh automatically with configurable thresholds
- âś… SSR Compatible: Works with Angular Universal and server-side rendering
- âś… Configurable Storage: Support for localStorage, sessionStorage, or custom storage
- âś… HTTP Interceptor: Automatically adds Bearer tokens to HTTP requests
- âś… Route Guards: Protect routes with authentication and role-based guards
- âś… TypeScript: Full TypeScript support with comprehensive type definitions
- âś… Standalone Components: Support for Angular 14+ standalone components
- âś… Customizable: Highly configurable to work with different OAuth2 providers
npm install witspry-auth-ng-client
// app.config.ts
import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';
import { provideOAuth2 } from 'witspry-auth-ng-client';
export const appConfig: ApplicationConfig = {
providers: [
provideRouter(routes),
provideOAuth2({
clientId: 'your-client-id',
redirectUri: 'http://localhost:4200/auth/callback',
authorizationEndpoint: 'https://your-auth-server.com/auth/authorize',
tokenEndpoint: 'https://your-auth-server.com/auth/oauth/token',
revokeEndpoint: 'https://your-auth-server.com/auth/oauth/revoke',
userInfoEndpoint: 'https://your-auth-server.com/userinfo',
scope: 'openid profile email',
audience: 'https://your-api.com'
})
]
};
// app.routes.ts
import { Routes } from '@angular/router';
import { OAuth2CallbackComponent } from 'witspry-auth-ng-client';
export const routes: Routes = [
{
path: 'auth/callback',
component: OAuth2CallbackComponent
},
// ... other routes
];
// login.component.ts
import { Component } from '@angular/core';
import { OAuth2Service } from 'witspry-auth-ng-client';
@Component({
selector: 'app-login',
template: `
<button (click)="login()" [disabled]="!oauth2Service.isAuthenticationAvailable()">
Login with OAuth2
</button>
`
})
export class LoginComponent {
constructor(public oauth2Service: OAuth2Service) {}
login() {
this.oauth2Service.startAuthorization();
}
}
// app.routes.ts
import { Routes } from '@angular/router';
import { oauth2AuthGuard } from 'witspry-auth-ng-client';
export const routes: Routes = [
{
path: 'protected',
component: ProtectedComponent,
canActivate: [oauth2AuthGuard]
}
];
interface OAuth2Config {
clientId: string; // OAuth2 Client ID
redirectUri: string; // Redirect URI for callback
authorizationEndpoint: string; // Authorization endpoint URL
tokenEndpoint: string; // Token endpoint URL
revokeEndpoint?: string; // Token revocation endpoint (optional)
userInfoEndpoint?: string; // OIDC UserInfo endpoint (optional)
audience?: string; // OAuth2 audience parameter (optional)
scope: string; // OAuth2 scope parameter
responseType?: 'code'; // Response type (default: 'code')
codeChallengeMethod?: 'S256'; // PKCE method (default: 'S256')
storage?: 'localStorage' | 'sessionStorage' | 'custom'; // Storage strategy
customStorage?: OAuth2Storage; // Custom storage implementation
autoRefresh?: boolean; // Enable auto token refresh (default: true)
refreshThreshold?: number; // Seconds before expiry to refresh (default: 300)
logLevel?: 'none' | 'error' | 'warn' | 'info' | 'debug'; // Logging level
nonce?: string; // Custom nonce (optional)
}
import { OAuth2Storage } from 'witspry-auth-ng-client';
class CustomStorage implements OAuth2Storage {
getItem(key: string): string | null {
// Your custom storage logic
return null;
}
setItem(key: string, value: string): void {
// Your custom storage logic
}
removeItem(key: string): void {
// Your custom storage logic
}
}
// Use in configuration
provideOAuth2({
// ... other config
storage: 'custom',
customStorage: new CustomStorage()
})
import { OAuth2Config } from 'witspry-auth-ng-client';
const oauth2Config: OAuth2Config = {
clientId: 'your-client-id',
redirectUri: 'http://localhost:4200/oauth2/callback',
authorizationEndpoint: 'https://auth.example.com/oauth2/authorize',
tokenEndpoint: 'https://auth.example.com/oauth2/token',
scope: 'openid profile email',
redirectRoute: '/dashboard'
logoutRedirectRoute: '/home'
};
import { oauth2RoleGuard } from 'witspry-auth-ng-client';
const routes: Routes = [
{
path: 'admin',
component: AdminComponent,
canActivate: [oauth2RoleGuard({
roles: ['admin', 'super-admin'],
requireAll: false // User needs ANY of the roles
})]
}
];
// If you want to configure the HTTP interceptor manually
import { provideOAuth2WithoutInterceptor, oauth2InterceptorFn } from 'witspry-auth-ng-client';
import { provideHttpClient, withInterceptors } from '@angular/common/http';
export const appConfig: ApplicationConfig = {
providers: [
provideOAuth2WithoutInterceptor(config),
provideHttpClient(
withInterceptors([oauth2InterceptorFn])
)
]
};
// app.module.ts
import { NgOAuth2PkceModule } from 'witspry-auth-ng-client';
@NgModule({
imports: [
NgOAuth2PkceModule.forRoot({
clientId: 'your-client-id',
// ... other config
})
]
})
export class AppModule {}
startAuthorization(): Promise<void>
- Start OAuth2 authorization flowhandleCallback(): Promise<void>
- Handle OAuth2 callbackgetAccessToken(): string | null
- Get current valid access tokengetRefreshToken(): string | null
- Get current refresh tokenisAuthenticated(): boolean
- Check if user is authenticatedgetUserInfo(): Promise<UserInfoResponse>
- Get user informationrefreshAccessToken(): Promise<string>
- Manually refresh access tokenlogout(): Promise<void>
- Logout user and clear tokens
authState$: Observable<AuthState>
- Observable of authentication stateauthState: Signal<AuthState>
- Signal of authentication state
oauth2AuthGuard
- Protect routes requiring authenticationoauth2UnauthGuard
- Protect routes for unauthenticated users onlyoauth2RoleGuard(config)
- Protect routes based on user roles/permissions
OAuth2CallbackComponent
- Handle OAuth2 callback with UI feedback
This library supports all modern browsers that support:
- Web Crypto API (for PKCE)
- ES2017+ features
- Angular 19+
Contributions are welcome! Please read our contributing guidelines and submit pull requests to our repository.
Update the version in package.json. Make sure the test cases are passing. Then run the following commands:
npm run build
cd .\dist\witspry-auth-ng-client\
npm publish
MIT License - see LICENSE file for details.
- Initial release
- OAuth2 PKCE implementation
- Automatic token refresh
- SSR support
- Configurable storage
- HTTP interceptor
- Route guards
- TypeScript support
- Added config parameter logoutRedirectRoute
- Fix: error section was visible even on successful login.
- Downgraded to Angular 18 for LLM compatibility
- Resolved peer deps from Angular 18 till 20
- Fixed the logoutRedirectRoute issue
- Automatic token refresh with intervals based on access token expiry