oidc-client-ts, it handles all the complexity of modern authentication flows with built-in security features.
npm install authsafe-tsyarn add authsafe-tspnpm add authsafe-tsimport { OAuthService } from 'authsafe-ts';
const oauthService = new OAuthService({
clientId: 'your-client-id',
redirectUri: 'http://localhost:3000/callback',
scope: ['openid', 'profile', 'email', 'offline_access'],
env: 'production',
});
// Initialize on app startup to restore sessions
await oauthService.initialize();// Redirect to login page
async function login() {
await oauthService.signinRedirect();
}
// Or use a popup (no page navigation)
async function loginPopup() {
const user = await oauthService.signinPopup();
console.log('Logged in:', user);
}// On your callback page (e.g., /callback)
async function handleCallback() {
try {
const user = await oauthService.signinRedirectCallback();
console.log('Authentication successful:', user);
// Redirect to your app
window.location.href = '/dashboard';
} catch (error) {
console.error('Authentication failed:', error);
}
}async function getCurrentUser() {
const user = await oauthService.getUser();
if (user && !user.expired) {
console.log('User:', user.profile);
console.log('Access Token:', user.access_token);
} else {
console.log('Not authenticated');
}
}async function logout() {
await oauthService.signoutRedirect();
}Main service for OAuth 2.1 and OIDC flows. Handles authentication, token management, silent refresh, and session monitoring.
HTTP client wrapper with automatic Bearer token injection, error handling, and request/response interceptors.
Fetch branding configuration for OAuth clients (logo, colors, theme).
Manage multi-factor authentication methods (TOTP, Email OTP, WebAuthn, Backup Codes).
Query SSO provider configuration for enterprise single sign-on.
Manage user profile information and account operations.
Complete TypeScript type definitions for all APIs, requests, and responses.
Helper functions including password strength calculation and validation utilities.
code_verifier and code_challengeimport { OAuthService } from 'authsafe-ts';
class AuthManager {
private oauthService: OAuthService;
constructor() {
this.oauthService = new OAuthService({
clientId: 'your-client-id',
redirectUri: window.location.origin + '/callback',
scope: ['openid', 'profile', 'email', 'offline_access'],
env: 'production',
});
this.oauthService.onStateChange(() => {
this.updateUI();
});
}
async init() {
await this.oauthService.initialize();
await this.updateUI();
}
async login() {
await this.oauthService.signinRedirect();
}
async logout() {
await this.oauthService.signoutRedirect();
}
async handleCallback() {
await this.oauthService.signinRedirectCallback();
window.location.href = '/';
}
async updateUI() {
const user = await this.oauthService.getUser();
const loginBtn = document.getElementById('login-btn');
const logoutBtn = document.getElementById('logout-btn');
const userInfo = document.getElementById('user-info');
if (user && !user.expired) {
loginBtn?.classList.add('hidden');
logoutBtn?.classList.remove('hidden');
if (userInfo) {
userInfo.textContent = `Welcome, ${user.profile.name}!`;
}
} else {
loginBtn?.classList.remove('hidden');
logoutBtn?.classList.add('hidden');
if (userInfo) {
userInfo.textContent = '';
}
}
}
}
// Initialize
const authManager = new AuthManager();
authManager.init();// main.ts
import { OAuthService } from 'authsafe-ts';
const oauthService = new OAuthService({
clientId: import.meta.env.VITE_CLIENT_ID,
redirectUri: import.meta.env.VITE_REDIRECT_URI,
scope: ['openid', 'profile', 'email', 'offline_access'],
env: import.meta.env.VITE_ENV,
});
// Check if this is a callback
if (window.location.pathname === '/callback') {
oauthService
.signinRedirectCallback()
.then(() => {
window.location.href = '/';
})
.catch(console.error);
} else {
// Initialize and restore session
oauthService.initialize();
}
export { oauthService };Detailed documentation for the main OAuth service including all methods, events, and configuration options.
Learn how to implement multi-factor authentication with TOTP, Email OTP, and WebAuthn.
Explore the full AuthSafe API for advanced integrations.