UNPKG

@wizecorp/stratusjs

Version:
163 lines 5.03 kB
/** * Authentication service */ export class AuthService { constructor(storageService) { Object.defineProperty(this, "name", { enumerable: true, configurable: true, writable: true, value: 'AuthService' }); Object.defineProperty(this, "user", { enumerable: true, configurable: true, writable: true, value: null }); Object.defineProperty(this, "token", { enumerable: true, configurable: true, writable: true, value: null }); Object.defineProperty(this, "storageService", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "TOKEN_KEY", { enumerable: true, configurable: true, writable: true, value: 'auth_token' }); Object.defineProperty(this, "USER_KEY", { enumerable: true, configurable: true, writable: true, value: 'auth_user' }); this.storageService = storageService; } async initialize() { // Load auth state from storage on initialization if (this.storageService) { this.token = this.storageService.get(this.TOKEN_KEY); this.user = this.storageService.get(this.USER_KEY); } } isAuthenticated() { return this.token !== null && this.user !== null; } async login(credentials) { // This is a basic implementation - replace with your API calls try { // Simulate API call const response = await this.callLoginAPI(credentials); this.token = response.token; this.user = response.user; // Persist to storage if (this.storageService) { this.storageService.set(this.TOKEN_KEY, this.token); this.storageService.set(this.USER_KEY, this.user); } return this.user; } catch (error) { throw new Error(`Login failed: ${error instanceof Error ? error.message : 'Unknown error'}`); } } async logout() { try { // Call logout API if needed await this.callLogoutAPI(); } catch (error) { console.warn('Logout API call failed:', error); } finally { // Always clear local state this.token = null; this.user = null; if (this.storageService) { this.storageService.remove(this.TOKEN_KEY); this.storageService.remove(this.USER_KEY); } } } getUser() { return this.user; } getToken() { return this.token; } /** * Update user information */ updateUser(userData) { if (!this.user) return; this.user = { ...this.user, ...userData }; if (this.storageService) { this.storageService.set(this.USER_KEY, this.user); } } /** * Check if user has specific permission */ hasPermission(permission) { if (!this.user) return false; const permissions = this.user.permissions || []; return permissions.includes(permission); } /** * Check if user has specific role */ hasRole(role) { if (!this.user) return false; const roles = this.user.roles || []; return roles.includes(role); } /** * Simulate login API call - replace with your actual implementation */ async callLoginAPI(credentials) { // This is a mock implementation // Replace with actual API call using HttpService return new Promise((resolve, reject) => { setTimeout(() => { // Simulate successful login for demo if (credentials.password === 'password') { resolve({ token: 'mock_jwt_token_' + Date.now(), user: { id: '1', email: credentials.email || credentials.username, name: 'John Doe', roles: ['user'], permissions: ['read', 'write'] } }); } else { reject(new Error('Invalid credentials')); } }, 1000); }); } /** * Simulate logout API call - replace with your actual implementation */ async callLogoutAPI() { // This is a mock implementation // Replace with actual API call using HttpService return new Promise(resolve => { setTimeout(resolve, 500); }); } } //# sourceMappingURL=AuthService.js.map