UNPKG

@visionfi/server-sdk

Version:

Server-side SDK for VisionFI API access using Google Service Account authentication

47 lines (46 loc) 1.5 kB
/** * Auth Client - handles authentication operations in Server SDK * Copyright (c) 2024-2025 VisionFI. All Rights Reserved. */ import { VisionFiError } from '@visionfi/core'; export class AuthClient { apiClient; constructor(apiClient) { this.apiClient = apiClient; } /** * Verify authentication status */ async verify() { try { const response = await this.apiClient.post('/auth/verify', {}); // API returns { data: true } on success, or throws 401 on failure return { authenticated: response.data?.data === true, user: response.data?.user }; } catch (error) { // If we get a 401, it means authentication failed if (error.response?.status === 401) { return { authenticated: false, user: undefined }; } throw this.handleError(error, 'Failed to verify authentication'); } } /** * Handle errors consistently */ handleError(error, defaultMessage) { if (error instanceof VisionFiError) { return error; } const message = error.response?.data?.message || error.message || defaultMessage; const statusCode = error.response?.status; const code = error.response?.data?.code || 'unknown_error'; return new VisionFiError(message, statusCode, code); } }