UNPKG

magically-sdk

Version:

Official SDK for Magically - Build mobile apps with AI

101 lines (100 loc) 2.71 kB
import { AuthState, User, SDKConfig } from './types'; export declare class MagicallyAuth { private config; private logger; private authState; private listeners; private refreshTimer; private tokenKey; private allowedOrigins; constructor(config: SDKConfig); /** * Simple helper - is user authenticated? */ get isAuthenticated(): boolean; /** * Initialize authentication - check for stored tokens */ private initializeAuth; /** * Sign in with email and password * TODO: Implement email/password authentication * For now, this method is commented out - use signInWithGoogle() instead */ /** * Sign in with Google OAuth * Handles ALL complexity internally - just returns user or throws error */ signInWithGoogle(): Promise<User>; /** * Sign out user * Handles ALL complexity internally - just clears everything */ signOut(): Promise<void>; /** * Get current user - simple getter */ get currentUser(): User | null; /** * Get current auth state - for debugging/UI */ get state(): AuthState; /** * Get a valid access token for API calls * Automatically refreshes if needed */ getValidToken(): Promise<string>; /** * Subscribe to auth state changes */ onAuthStateChanged(callback: (state: AuthState) => void): () => void; /** * Refresh access token */ refreshToken(): Promise<string>; /** * Check if token needs refresh and refresh if necessary */ ensureValidToken(): Promise<string>; private setAuthState; private setLoading; private setError; private notifyListeners; private getApiUrl; private getRedirectUri; private getClientId; private getOAuthClient; /** * Determine platform for OAuth redirect URI */ private getPlatform; private buildOAuthUrl; private generateState; private extractCodeFromUrl; /** * Parse authentication data from URL (supports both hash fragments and query params) */ private parseAuthDataFromUrl; private getUserFromToken; private decodeJWT; private validateToken; private storeTokens; private getStoredTokens; private clearStoredTokens; /** * Set up postMessage listener for OAuth callback (web only) */ private setupPostMessageListener; /** * Start background token refresh */ private startBackgroundRefresh; /** * Stop background token refresh */ private stopBackgroundRefresh; /** * Ensure user exists in project database */ private ensureUserInDatabase; }