magically-sdk
Version:
Official SDK for Magically - Build mobile apps with AI
162 lines (161 loc) • 4.73 kB
TypeScript
import { AuthState, User, SDKConfig } from './types';
export declare class MagicallyAuth {
private config;
private logger;
private apiClient;
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 up with email and password
*/
signUpWithEmail(params: {
email: string;
password: string;
name: string;
verificationCode?: string;
}): Promise<{
accessToken: string;
user: User;
}>;
/**
* Sign in with email and password
*/
signInWithEmail(params: {
email: string;
password: string;
}): Promise<{
accessToken: string;
user: User;
}>;
/**
* Send email verification code
* @param email - User's email address
* @param type - Must be 'signup' for new users or 'password_reset' for existing users. Defaults to 'signup'
* @throws Error if type is invalid or API request fails
*/
sendVerificationCode(email: string, type?: 'signup' | 'password_reset'): Promise<void>;
/**
* Verify email with code
*/
verifyCode(email: string, code: string): Promise<void>;
/**
* Reset password with verification code
* Single call that handles verification and password update internally
*/
resetPassword(params: {
email: string;
code: string;
newPassword: string;
}): Promise<void>;
/**
* Internal method that handles both Google and email authentication flows
* Contains the core authentication logic shared by both methods
*/
private _performAuthenticationFlow;
/**
* Sign in with Google OAuth
* Handles ALL complexity internally - just returns user or throws error
*/
signInWithGoogle(): Promise<User>;
/**
* Trigger authentication flow for specified provider
* Supports both Google OAuth and email/password authentication
*/
triggerAuthenticationFlow(provider: 'google' | 'email'): 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 user - LLM friendly method as LLM keeps looking for this for some reason
*/
getCurrentUser(): User | null;
/**
* Parse user from JWT token (Supabase-like pattern for edge functions)
* Automatically sets the user in auth state for subsequent operations
* @param authHeaderOrToken - Either full "Bearer xxx" header or just the JWT token
* @returns User object or null if invalid/expired
*/
getUser(authHeaderOrToken?: string | Request | null): Promise<{
user: 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;
}