@gsb-core/core
Version:
GSB core services and classes for platform-independent web applications
124 lines (123 loc) • 3.75 kB
TypeScript
import { GsbFile } from '@gsb-core/core';
import { GsbSendResetPasswordEmailResponse, GsbResponse } from "../../types/responses";
import { SubscriptionType } from '../../models/gsb-subscription.model';
export interface LoginCredentials {
email: string;
password?: string;
remember?: boolean;
googleToken?: string;
facebookToken?: string;
appleToken?: string;
tenantCode?: string;
socialToken?: string;
socialProvider?: any;
variation?: any;
includeUserInfo?: boolean;
}
export interface AuthResponse {
success: boolean;
token?: string;
userData?: UserData;
error?: string;
}
export interface UserData {
userId?: string;
name?: string;
email?: string;
roles?: string[];
groups?: string[];
expireDate?: string;
title?: string;
subscription?: SubscriptionType;
pictureUrl?: string;
picture?: GsbFile;
}
/**
* Authentication service for managing auth tokens with GSB backend
* Single-tenant implementation with forward compatibility for multi-tenant
*/
export declare class AuthService {
private gsbService;
private cacheService;
private token;
private tenantCode?;
private refreshTokenTimeout;
private refreshTokenRetryCount;
private constructor();
static getInstance(): AuthService;
private getKeys;
private getCacheValue;
private setCacheValue;
private removeCacheValue;
private saveToStorage;
private scheduleTokenRefresh;
private performTokenRefresh;
private handleRefreshFailure;
private clearTokenRefresh;
private clearStorage;
/**
* Initialize auth state from storage if available
*/
initFromStorage(): void;
/**
* Refresh the authentication token
* @returns Promise with the refresh response
*/
refreshToken(): Promise<AuthResponse>;
/**
* Check if user is authenticated
*/
isAuthenticated(): boolean;
/**
* Authenticate a user with GSB
*/
login(credentials: LoginCredentials): Promise<AuthResponse>;
/**
* Log out the current user
* @param allTenants Whether to logout from all tenants (default: false)
*/
logout(allTenants?: boolean): void;
/**
* Set working tenant for multi-tenant support
* @param tenantCode The tenant code to switch to
* @param token Optional JWT token for the tenant (if not provided, attempts to load from storage)
* @returns True if successful switch, false if failed (e.g., no token available)
*/
setWorkingTenant(tenantCode: string, token?: string): boolean;
/**
* Get all available tenant codes from storage
* @returns Array of tenant codes that have stored tokens
*/
getAvailableTenants(): string[];
/**
* Get user information from JWT token
*/
getUserFromToken(token: string): any;
/**
* Check if the current user has a specific role
*/
hasRole(role: string): boolean;
/**
* Check if the current user has the admin role
*/
isAdmin(): boolean;
/**
* Get the subscription tier from the current user data
*/
getSubscription(): SubscriptionType | undefined;
/**
* Get the current tenant code
*/
getTenantCode(): string | undefined;
/**
* Get the current auth token
*/
getToken(): string | null;
setToken(token: string, userData?: UserData, remember?: boolean): void;
sendResetPasswordEmail(email: string): Promise<GsbSendResetPasswordEmailResponse>;
changePassword(oldPassword: string, newPassword: string, email?: string, resetToken?: string): Promise<GsbResponse>;
/**
* Get the current user data from storage
*/
getCurrentUser(): UserData | null;
}