@explorins/pers-sdk-angular
Version:
Angular integration layer for PERS SDK
148 lines (134 loc) • 5.49 kB
text/typescript
/**
* Example: How to integrate PERS SDK with Loyalty App's specific architecture
*
* This shows how the loyalty app can use the generic Angular SDK
* while maintaining its specific facade-based architecture.
*
* Note: This is an example file showing integration patterns.
* Actual imports would reference the loyalty app's specific modules.
*/
import { Injectable, signal, inject } from '@angular/core';
import { BaseAngularAuthProvider } from '../providers/angular-auth-provider';
import { firstValueFrom } from 'rxjs';
// Example: These would be the actual imports in the loyalty app
// import { AUTH_ADMIN_FACADE } from '../../../framework/domains/auth-admin/application/auth-admin.facade';
// import { AUTH_FACADE } from '../../../framework/domains/auth/application/auth.facade';
// import { FIREBASE_AUTH_FACADE } from '../../../framework/domains/firebase-auth/application/firebase-auth.facade';
// import { API_PROJECT_KEY, DEFAULT_PERS_AUTH_TYPE } from '../../../framework/injection-tokens/injectionTokens';
// import { PersAuthTypes } from '../../../framework/entities/PersAuthTypes';
/**
* Loyalty App specific implementation using the app's facade architecture
*
* This demonstrates how to extend BaseAngularAuthProvider with app-specific logic.
*/
()
export class LoyaltyAppAuthProvider extends BaseAngularAuthProvider {
// Example: These would be the actual injections in the loyalty app
// private authAdminService = inject(AUTH_ADMIN_FACADE, { optional: true });
// private authService = inject(AUTH_FACADE, { optional: true });
// private firebaseAuthService = inject(FIREBASE_AUTH_FACADE, { optional: true });
// private $apiProjectKey = inject(API_PROJECT_KEY);
// private persAuthType = inject(DEFAULT_PERS_AUTH_TYPE, { optional: true });
readonly authType: 'admin' | 'user' | 'firebase' = 'user';
constructor() {
super();
// Auto-detect auth type based on available services and configuration
// if (this.authAdminService) {
// this.authType = 'admin';
// } else if (this.persAuthType === PersAuthTypes.USER && this.authService) {
// this.authType = 'firebase';
// } else {
// this.authType = 'user';
// }
}
async getToken(): Promise<string | null> {
try {
switch (this.authType) {
case 'admin':
// Example: const adminJwt = this.authAdminService?.$AdminJwt();
// return adminJwt || null;
return null;
case 'firebase':
// Example: Firebase token retrieval
// if (this.firebaseAuthService?.firebaseJWT$) {
// try {
// return await firstValueFrom(this.firebaseAuthService.firebaseJWT$);
// } catch (error) {
// console.warn('LoyaltyAppAuthProvider: Failed to get Firebase JWT, falling back to user token', error);
// return this.getUserToken();
// }
// }
return this.getUserToken();
case 'user':
default:
return this.getUserToken();
}
} catch (error) {
console.error('LoyaltyAppAuthProvider: Error getting token', error);
return null;
}
}
async getProjectKey(): Promise<string | null> {
try {
// Example: return this.$apiProjectKey() || null;
return 'example-project-key';
} catch (error) {
console.error('LoyaltyAppAuthProvider: Error getting project key', error);
return null;
}
}
async onTokenExpired(): Promise<void> {
switch (this.authType) {
case 'admin':
// Example: Admin token refresh
// if (this.authAdminService) {
// try {
// await this.authAdminService.refreshAccessToken();
// console.log('LoyaltyAppAuthProvider: Admin token refreshed successfully');
// } catch (error) {
// console.error('LoyaltyAppAuthProvider: Failed to refresh admin token', error);
// throw error;
// }
// }
break;
case 'firebase':
// Example: Firebase token refresh
// if (this.firebaseAuthService?.$firebaseAuth) {
// try {
// const auth = this.firebaseAuthService.$firebaseAuth();
// if (auth.currentUser) {
// await auth.currentUser.getIdToken(true);
// console.log('LoyaltyAppAuthProvider: Firebase token refreshed successfully');
// }
// } catch (error) {
// console.error('LoyaltyAppAuthProvider: Failed to refresh Firebase token', error);
// throw error;
// }
// }
break;
default:
console.warn('LoyaltyAppAuthProvider: Token refresh not supported for auth type:', this.authType);
}
}
private getUserToken(): string | null {
if (typeof localStorage !== 'undefined') {
return localStorage.getItem('userJwt');
}
return null;
}
}
/**
* Provider configuration for the loyalty app
*
* This shows how the app would configure the SDK with its specific auth provider.
*/
export function provideLoyaltyAppSDKIntegration() {
return [
// The loyalty app's specific auth provider
{
provide: BaseAngularAuthProvider,
useClass: LoyaltyAppAuthProvider
}
// Other SDK configuration...
];
}