Skip to main content

AuthEvent

Package: @nauth-toolkit/client Type: Interface

Authentication lifecycle event emitted by the SDK throughout the authentication flow.

import { AuthEvent } from '@nauth-toolkit/client';

Structure

Authentication events use a discriminated union for type safety:

type AuthEvent =
| AuthLoginEvent
| AuthSignupEvent
| AuthSuccessEvent
| AuthChallengeEvent
| AuthErrorEvent
| AuthLogoutEvent
| AuthRefreshEvent
| OAuthStartedEvent
| OAuthCallbackEvent
| OAuthCompletedEvent
| OAuthErrorEvent;

// Each event has a specific structure
interface AuthLoginEvent {
type: 'auth:login';
data: { identifier: string };
timestamp: number;
}

interface AuthSuccessEvent {
type: 'auth:success';
data: AuthResponse;
timestamp: number;
}

interface AuthErrorEvent {
type: 'auth:error';
data: NAuthClientError;
timestamp: number;
}

// ... (other event types follow the same pattern)

Properties

All events share these base properties:

PropertyTypeDescription
typeAuthEventTypeEvent type identifier
dataVaries by eventEvent-specific data (strongly typed per event)
timestampnumberEvent timestamp in milliseconds since epoch

AuthEventType

Event type enumeration:

type AuthEventType =
| 'auth:login' // Login initiated
| 'auth:signup' // Signup initiated
| 'auth:success' // User successfully authenticated
| 'auth:challenge' // Challenge required (MFA, verification, etc.)
| 'auth:error' // Authentication failed
| 'auth:logout' // User logged out
| 'auth:refresh' // Token refresh attempted
| 'oauth:started' // Social login initiated
| 'oauth:callback' // OAuth callback detected
| 'oauth:completed' // OAuth flow completed
| 'oauth:error'; // OAuth flow failed

Event Data by Type

Event TypeData TypeDescription
auth:login{ identifier: string }Login initiated with identifier
auth:signup{ email: string }Signup initiated with email
auth:successAuthResponseContains user and tokens
auth:challengeAuthResponseContains challengeName and session
auth:errorNAuthClientErrorError details
auth:logout{ forgetDevice: boolean, global: boolean }Logout flags
auth:refresh{ success: boolean }Token refresh result
oauth:started{ provider: string }OAuth provider name
oauth:callback{ provider: string }OAuth provider name
oauth:completedAuthResponseAuthentication result
oauth:errorNAuthClientErrorError details

Examples

TypeScript

import { AuthEvent } from '@nauth-toolkit/client';

// Event handler with full type safety
function handleAuthEvent(event: AuthEvent): void {
switch (event.type) {
case 'auth:login':
// TypeScript knows data is { identifier: string }
console.log('Login attempt:', event.data.identifier);
break;

case 'auth:success':
// TypeScript knows data is AuthResponse
console.log('User authenticated:', event.data.user);
break;

case 'auth:challenge':
// TypeScript knows data is AuthResponse with challengeName
console.log('Challenge required:', event.data.challengeName);
break;

case 'auth:error':
case 'oauth:error':
// TypeScript knows data is NAuthClientError
console.error('Auth error:', event.data.message);
break;

case 'auth:logout':
// TypeScript knows data is { forgetDevice: boolean, global: boolean }
console.log('Logged out:', event.data.global ? 'globally' : 'session only');
break;

case 'auth:refresh':
// TypeScript knows data is { success: boolean }
console.log('Token refresh:', event.data.success ? 'success' : 'failed');
break;
}
}

Angular

import { Component, OnInit } from '@angular/core';
import { AuthService } from '@nauth-toolkit/client-angular';
import { AuthEvent } from '@nauth-toolkit/client';

@Component({ /* ... */ })
export class AppComponent implements OnInit {
constructor(private auth: AuthService) {}

ngOnInit(): void {
this.auth.authEvents$.subscribe((event: AuthEvent) => {
console.log('Event:', event.type, 'at', new Date(event.timestamp));

// Type-safe event handling
switch (event.type) {
case 'auth:success':
// TypeScript knows event.data is AuthResponse
this.router.navigate(['/dashboard']);
break;

case 'auth:error':
// TypeScript knows event.data is NAuthClientError
this.toastr.error(event.data.message);
break;

case 'auth:refresh':
// TypeScript knows event.data is { success: boolean }
if (!event.data.success) {
console.warn('Token refresh failed');
}
break;
}
});
}
}

See Also