magically-sdk
Version:
Official SDK for Magically - Build mobile apps with AI
73 lines (72 loc) • 3.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const MagicallySDK_1 = require("./MagicallySDK");
async function testJWT() {
console.log('Testing JWT authentication in SDK...\n');
const jwtToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxOGM2NmMzZi0zOTI2LTQzNzgtOTU2NC0xMTU1ZmY2Njc2NTciLCJhcHBVc2VySWQiOiI2OGE0YzM0MjEwZjQ5N2E4MTEzMjg1YzYiLCJhdWQiOiJVaGYxOFN0NTFnRWtnbkRFZFpIX05vVW80STZVRVc0LSIsImFwcF9pZCI6IjAzOTk3ZGUyLTRmYjEtNDhlMi1hNmEwLWQyMDAxMGRlODczMCIsInByb2plY3RfaWQiOiIwMzk5N2RlMi00ZmIxLTQ4ZTItYTZhMC1kMjAwMTBkZTg3MzAiLCJzaGFyZWRfcHJvamVjdF9pZCI6IjAzOTk3ZGUyLTRmYjEtNDhlMi1hNmEwLWQyMDAxMGRlODczMCIsImlzcyI6Imh0dHA6Ly9sb2NhbGhvc3Q6MzAwMCIsImV4cCI6MTc1NTcxODU5NywiaWF0IjoxNzU1NzE0OTk3LCJlbWFpbCI6InJhamF0LmFkeUBnbWFpbC5jb20iLCJuYW1lIjoiS3VtYXIgRGl2eWEgUmFqYXQiLCJnaXZlbl9uYW1lIjpudWxsLCJmYW1pbHlfbmFtZSI6bnVsbH0.pldLOuhJ2qngn78ZQQ0H6salJf-vv4GAskm-aqXaOqc';
console.log('=== Test: SDK with JWT (no API key) ===');
try {
const sdk = new MagicallySDK_1.MagicallySDK({
projectId: '03997de2-4fb1-48e2-a6a0-d20010de8730',
apiUrl: 'https://meet-bream-gratefully.ngrok-free.app'
});
console.log('SDK created successfully');
// Check internal state
const dataClient = sdk.data;
console.log('Data client apiClient:', dataClient.apiClient);
console.log('Data client apiClient.isEdgeEnvironment():', dataClient.apiClient?.isEdgeEnvironment?.());
console.log('Data client apiClient has apiKey:', !!dataClient.apiClient?.apiKey);
// Check auth state
const authClient = sdk.auth;
console.log('\nAuth client state:');
console.log('- Has token storage:', !!authClient.tokenStorage);
console.log('- Has API client:', !!authClient.apiClient);
// Use getUser like in edge functions to set the token in auth state
console.log('\n=== Using getUser() to set JWT (like in edge functions) ===');
const mockRequest = {
headers: {
get: (name) => {
const value = name.toLowerCase() === 'authorization' ? `Bearer ${jwtToken}` : null;
console.log(` headers.get('${name}') => ${value ? value.substring(0, 50) + '...' : 'null'}`);
return value;
}
}
};
console.log('Calling sdk.auth.getUser(request)...');
const { user } = await sdk.auth.getUser(mockRequest);
if (user) {
console.log('User extracted successfully:', {
id: user.id,
email: user.email,
name: user.name
});
}
else {
console.log('Failed to get user from token');
// Check auth state after getUser
console.log('Auth state after getUser:', sdk.auth.authState);
}
// Now check if we can get the token after getUser
try {
const token = await authClient.getValidToken();
console.log('Got valid token after getUser:', token.substring(0, 50) + '...');
}
catch (e) {
console.log('Failed to get valid token after getUser:', e.message);
}
// Try making a query
console.log('\n=== Testing data.query with JWT ===');
try {
const result = await sdk.data.query('test_collection', { test: true }, { limit: 1 });
console.log('Query successful:', result);
}
catch (e) {
console.log('Query failed:', e.message);
console.log('Error details:', e);
}
}
catch (error) {
console.log('SDK creation/test failed:', error.message);
}
}
testJWT().catch(console.error);