magically-sdk
Version:
Official SDK for Magically - Build mobile apps with AI
24 lines (23 loc) • 879 B
text/typescript
import { APIClient } from './APIClient';
import { MagicallyAuth } from './MagicallyAuth';
/**
* Get the appropriate authentication token based on the environment
*
* In edge environments (Cloudflare Workers), the API key is used automatically
* and no token needs to be passed. In browser/Node environments, we need to
* get the JWT token from the auth system.
*
* @param apiClient - The APIClient instance to check for edge environment
* @param auth - The MagicallyAuth instance to get JWT token from
* @returns The token to use (null in edge environment, JWT string otherwise)
*/
export async function getAuthToken(
apiClient: APIClient,
auth: MagicallyAuth
): Promise<string | null> {
// In edge environment, API key is used automatically
// Otherwise, get user token
return apiClient.isEdgeEnvironment()
? null
: await auth.getValidToken();
}