@oxyhq/auth 2.0.2 → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,145 +0,0 @@
1
- /**
2
- * Authentication helper utilities to reduce code duplication across hooks and utilities.
3
- * These functions handle common token validation and authentication error patterns.
4
- */
5
- /**
6
- * Error thrown when session sync is required
7
- */
8
- export class SessionSyncRequiredError extends Error {
9
- constructor(message = 'Session needs to be synced. Please try again.') {
10
- super(message);
11
- this.name = 'SessionSyncRequiredError';
12
- }
13
- }
14
- /**
15
- * Error thrown when authentication fails
16
- */
17
- export class AuthenticationFailedError extends Error {
18
- constructor(message = 'Authentication failed. Please sign in again.') {
19
- super(message);
20
- this.name = 'AuthenticationFailedError';
21
- }
22
- }
23
- /**
24
- * Ensures a valid token exists before making authenticated API calls.
25
- * If no valid token exists and an active session ID is available,
26
- * attempts to refresh the token using the session.
27
- *
28
- * @param oxyServices - The OxyServices instance
29
- * @param activeSessionId - The active session ID (if available)
30
- * @throws {SessionSyncRequiredError} If the session needs to be synced (offline session)
31
- * @throws {Error} If token refresh fails for other reasons
32
- *
33
- * @example
34
- * ```ts
35
- * // In a mutation or query function:
36
- * await ensureValidToken(oxyServices, activeSessionId);
37
- * return await oxyServices.updateProfile(updates);
38
- * ```
39
- */
40
- export async function ensureValidToken(oxyServices, activeSessionId) {
41
- if (oxyServices.hasValidToken() || !activeSessionId) {
42
- return;
43
- }
44
- try {
45
- await oxyServices.getTokenBySession(activeSessionId);
46
- }
47
- catch (tokenError) {
48
- const errorMessage = tokenError instanceof Error ? tokenError.message : String(tokenError);
49
- if (errorMessage.includes('AUTH_REQUIRED_OFFLINE_SESSION') || errorMessage.includes('offline')) {
50
- throw new SessionSyncRequiredError();
51
- }
52
- throw tokenError;
53
- }
54
- }
55
- /**
56
- * Checks if an error is an authentication error (401 or auth-related message)
57
- *
58
- * @param error - The error to check
59
- * @returns True if the error is an authentication error
60
- */
61
- export function isAuthenticationError(error) {
62
- if (!error || typeof error !== 'object') {
63
- return false;
64
- }
65
- const errorObj = error;
66
- const errorMessage = errorObj.message || '';
67
- const status = errorObj.status || errorObj.response?.status;
68
- return (status === 401 ||
69
- errorMessage.includes('Authentication required') ||
70
- errorMessage.includes('Invalid or missing authorization header'));
71
- }
72
- /**
73
- * Wraps an API call with authentication error handling.
74
- * If an authentication error occurs, it can optionally attempt to sync the session and retry.
75
- *
76
- * @param apiCall - The API call function to execute
77
- * @param options - Optional error handling configuration
78
- * @returns The result of the API call
79
- * @throws {AuthenticationFailedError} If authentication fails and cannot be recovered
80
- * @throws {Error} If the API call fails for non-auth reasons
81
- *
82
- * @example
83
- * ```ts
84
- * // Simple usage:
85
- * const result = await withAuthErrorHandling(
86
- * () => oxyServices.updateProfile(updates)
87
- * );
88
- *
89
- * // With retry on auth failure:
90
- * const result = await withAuthErrorHandling(
91
- * () => oxyServices.updateProfile(updates),
92
- * { syncSession, activeSessionId, oxyServices }
93
- * );
94
- * ```
95
- */
96
- export async function withAuthErrorHandling(apiCall, options) {
97
- try {
98
- return await apiCall();
99
- }
100
- catch (error) {
101
- if (!isAuthenticationError(error)) {
102
- throw error;
103
- }
104
- // If we have sync capabilities, try to recover
105
- if (options?.syncSession && options?.activeSessionId && options?.oxyServices) {
106
- try {
107
- await options.syncSession();
108
- await options.oxyServices.getTokenBySession(options.activeSessionId);
109
- // Retry the API call after refreshing token
110
- return await apiCall();
111
- }
112
- catch {
113
- throw new AuthenticationFailedError();
114
- }
115
- }
116
- throw new AuthenticationFailedError();
117
- }
118
- }
119
- /**
120
- * Combines token validation and auth error handling for a complete authenticated API call.
121
- * This is the recommended helper for most authenticated API operations.
122
- *
123
- * @param oxyServices - The OxyServices instance
124
- * @param activeSessionId - The active session ID
125
- * @param apiCall - The API call function to execute
126
- * @param syncSession - Optional callback to sync session on auth failure
127
- * @returns The result of the API call
128
- *
129
- * @example
130
- * ```ts
131
- * return await authenticatedApiCall(
132
- * oxyServices,
133
- * activeSessionId,
134
- * () => oxyServices.updateProfile(updates)
135
- * );
136
- * ```
137
- */
138
- export async function authenticatedApiCall(oxyServices, activeSessionId, apiCall, syncSession) {
139
- await ensureValidToken(oxyServices, activeSessionId);
140
- return withAuthErrorHandling(apiCall, {
141
- syncSession,
142
- activeSessionId,
143
- oxyServices,
144
- });
145
- }
@@ -1,98 +0,0 @@
1
- /**
2
- * Authentication helper utilities to reduce code duplication across hooks and utilities.
3
- * These functions handle common token validation and authentication error patterns.
4
- */
5
- import type { OxyServices } from '@oxyhq/core';
6
- /**
7
- * Error thrown when session sync is required
8
- */
9
- export declare class SessionSyncRequiredError extends Error {
10
- constructor(message?: string);
11
- }
12
- /**
13
- * Error thrown when authentication fails
14
- */
15
- export declare class AuthenticationFailedError extends Error {
16
- constructor(message?: string);
17
- }
18
- /**
19
- * Ensures a valid token exists before making authenticated API calls.
20
- * If no valid token exists and an active session ID is available,
21
- * attempts to refresh the token using the session.
22
- *
23
- * @param oxyServices - The OxyServices instance
24
- * @param activeSessionId - The active session ID (if available)
25
- * @throws {SessionSyncRequiredError} If the session needs to be synced (offline session)
26
- * @throws {Error} If token refresh fails for other reasons
27
- *
28
- * @example
29
- * ```ts
30
- * // In a mutation or query function:
31
- * await ensureValidToken(oxyServices, activeSessionId);
32
- * return await oxyServices.updateProfile(updates);
33
- * ```
34
- */
35
- export declare function ensureValidToken(oxyServices: OxyServices, activeSessionId: string | null | undefined): Promise<void>;
36
- /**
37
- * Options for handling API authentication errors
38
- */
39
- export interface HandleApiErrorOptions {
40
- /** Optional callback to attempt session sync and retry */
41
- syncSession?: () => Promise<unknown>;
42
- /** The active session ID for retry attempts */
43
- activeSessionId?: string | null;
44
- /** The OxyServices instance for retry attempts */
45
- oxyServices?: OxyServices;
46
- }
47
- /**
48
- * Checks if an error is an authentication error (401 or auth-related message)
49
- *
50
- * @param error - The error to check
51
- * @returns True if the error is an authentication error
52
- */
53
- export declare function isAuthenticationError(error: unknown): boolean;
54
- /**
55
- * Wraps an API call with authentication error handling.
56
- * If an authentication error occurs, it can optionally attempt to sync the session and retry.
57
- *
58
- * @param apiCall - The API call function to execute
59
- * @param options - Optional error handling configuration
60
- * @returns The result of the API call
61
- * @throws {AuthenticationFailedError} If authentication fails and cannot be recovered
62
- * @throws {Error} If the API call fails for non-auth reasons
63
- *
64
- * @example
65
- * ```ts
66
- * // Simple usage:
67
- * const result = await withAuthErrorHandling(
68
- * () => oxyServices.updateProfile(updates)
69
- * );
70
- *
71
- * // With retry on auth failure:
72
- * const result = await withAuthErrorHandling(
73
- * () => oxyServices.updateProfile(updates),
74
- * { syncSession, activeSessionId, oxyServices }
75
- * );
76
- * ```
77
- */
78
- export declare function withAuthErrorHandling<T>(apiCall: () => Promise<T>, options?: HandleApiErrorOptions): Promise<T>;
79
- /**
80
- * Combines token validation and auth error handling for a complete authenticated API call.
81
- * This is the recommended helper for most authenticated API operations.
82
- *
83
- * @param oxyServices - The OxyServices instance
84
- * @param activeSessionId - The active session ID
85
- * @param apiCall - The API call function to execute
86
- * @param syncSession - Optional callback to sync session on auth failure
87
- * @returns The result of the API call
88
- *
89
- * @example
90
- * ```ts
91
- * return await authenticatedApiCall(
92
- * oxyServices,
93
- * activeSessionId,
94
- * () => oxyServices.updateProfile(updates)
95
- * );
96
- * ```
97
- */
98
- export declare function authenticatedApiCall<T>(oxyServices: OxyServices, activeSessionId: string | null | undefined, apiCall: () => Promise<T>, syncSession?: () => Promise<unknown>): Promise<T>;