@oxyhq/services 5.7.5 → 5.8.1
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.
- package/README.md +76 -76
- package/lib/commonjs/core/index.js +177 -102
- package/lib/commonjs/core/index.js.map +1 -1
- package/lib/commonjs/index.js +88 -29
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/node/createAuth.js +585 -7
- package/lib/commonjs/node/createAuth.js.map +1 -1
- package/lib/commonjs/node/index.js +38 -1
- package/lib/commonjs/node/index.js.map +1 -1
- package/lib/commonjs/ui/components/Avatar.js +15 -6
- package/lib/commonjs/ui/components/Avatar.js.map +1 -1
- package/lib/commonjs/ui/components/GroupedItem.js +58 -13
- package/lib/commonjs/ui/components/GroupedItem.js.map +1 -1
- package/lib/commonjs/ui/components/GroupedSection.js +7 -1
- package/lib/commonjs/ui/components/GroupedSection.js.map +1 -1
- package/lib/commonjs/ui/components/Header.js +322 -0
- package/lib/commonjs/ui/components/Header.js.map +1 -0
- package/lib/commonjs/ui/components/OxyProvider.js +23 -7
- package/lib/commonjs/ui/components/OxyProvider.js.map +1 -1
- package/lib/commonjs/ui/components/index.js +7 -0
- package/lib/commonjs/ui/components/index.js.map +1 -1
- package/lib/commonjs/ui/components/internal/GroupedPillButtons.js +1 -1
- package/lib/commonjs/ui/components/internal/GroupedPillButtons.js.map +1 -1
- package/lib/commonjs/ui/components/internal/TextField.js +606 -546
- package/lib/commonjs/ui/components/internal/TextField.js.map +1 -1
- package/lib/commonjs/ui/components/internal/TextField.md +436 -0
- package/lib/commonjs/ui/context/OxyContext.js +122 -78
- package/lib/commonjs/ui/context/OxyContext.js.map +1 -1
- package/lib/commonjs/ui/hooks/useSessionSocket.js +5 -2
- package/lib/commonjs/ui/hooks/useSessionSocket.js.map +1 -1
- package/lib/commonjs/ui/navigation/OxyRouter.js +1 -1
- package/lib/commonjs/ui/navigation/OxyRouter.js.map +1 -1
- package/lib/commonjs/ui/screens/AccountCenterScreen.js +6 -6
- package/lib/commonjs/ui/screens/AccountCenterScreen.js.map +1 -1
- package/lib/commonjs/ui/screens/AccountManagementDemo.js +3 -3
- package/lib/commonjs/ui/screens/AccountManagementDemo.js.map +1 -1
- package/lib/commonjs/ui/screens/AccountOverviewScreen.js +241 -598
- package/lib/commonjs/ui/screens/AccountOverviewScreen.js.map +1 -1
- package/lib/commonjs/ui/screens/AccountSettingsScreen.js +1151 -406
- package/lib/commonjs/ui/screens/AccountSettingsScreen.js.map +1 -1
- package/lib/commonjs/ui/screens/AccountSwitcherScreen.js +135 -237
- package/lib/commonjs/ui/screens/AccountSwitcherScreen.js.map +1 -1
- package/lib/commonjs/ui/screens/AppInfoScreen.js +246 -463
- package/lib/commonjs/ui/screens/AppInfoScreen.js.map +1 -1
- package/lib/commonjs/ui/screens/FeedbackScreen.js +3 -3
- package/lib/commonjs/ui/screens/FeedbackScreen.js.map +1 -1
- package/lib/commonjs/ui/screens/PaymentGatewayScreen.js +808 -650
- package/lib/commonjs/ui/screens/PaymentGatewayScreen.js.map +1 -1
- package/lib/commonjs/ui/screens/RecoverAccountScreen.js +51 -72
- package/lib/commonjs/ui/screens/RecoverAccountScreen.js.map +1 -1
- package/lib/commonjs/ui/screens/SessionManagementScreen.js +11 -29
- package/lib/commonjs/ui/screens/SessionManagementScreen.js.map +1 -1
- package/lib/commonjs/ui/screens/SignInScreen.js +30 -303
- package/lib/commonjs/ui/screens/SignInScreen.js.map +1 -1
- package/lib/commonjs/ui/screens/SignUpScreen.js +4 -4
- package/lib/commonjs/ui/screens/SignUpScreen.js.map +1 -1
- package/lib/commonjs/ui/screens/internal/SignInPasswordStep.js +19 -31
- package/lib/commonjs/ui/screens/internal/SignInPasswordStep.js.map +1 -1
- package/lib/commonjs/ui/screens/internal/SignInUsernameStep.js +7 -10
- package/lib/commonjs/ui/screens/internal/SignInUsernameStep.js.map +1 -1
- package/lib/commonjs/ui/screens/internal/SignUpIdentityStep.js +11 -5
- package/lib/commonjs/ui/screens/internal/SignUpIdentityStep.js.map +1 -1
- package/lib/commonjs/ui/screens/internal/SignUpSecurityStep.js +11 -4
- package/lib/commonjs/ui/screens/internal/SignUpSecurityStep.js.map +1 -1
- package/lib/commonjs/ui/stores/authStore.js +12 -0
- package/lib/commonjs/ui/stores/authStore.js.map +1 -1
- package/lib/commonjs/ui/styles/authStyles.js +337 -0
- package/lib/commonjs/ui/styles/authStyles.js.map +1 -0
- package/lib/commonjs/ui/styles/index.js +11 -0
- package/lib/commonjs/ui/styles/index.js.map +1 -1
- package/lib/module/core/index.js +177 -41
- package/lib/module/core/index.js.map +1 -1
- package/lib/module/index.js +26 -4
- package/lib/module/index.js.map +1 -1
- package/lib/module/node/createAuth.js +584 -7
- package/lib/module/node/createAuth.js.map +1 -1
- package/lib/module/node/index.js +7 -1
- package/lib/module/node/index.js.map +1 -1
- package/lib/module/ui/components/Avatar.js +15 -6
- package/lib/module/ui/components/Avatar.js.map +1 -1
- package/lib/module/ui/components/GroupedItem.js +59 -14
- package/lib/module/ui/components/GroupedItem.js.map +1 -1
- package/lib/module/ui/components/GroupedSection.js +7 -1
- package/lib/module/ui/components/GroupedSection.js.map +1 -1
- package/lib/module/ui/components/Header.js +317 -0
- package/lib/module/ui/components/Header.js.map +1 -0
- package/lib/module/ui/components/OxyProvider.js +25 -9
- package/lib/module/ui/components/OxyProvider.js.map +1 -1
- package/lib/module/ui/components/index.js +1 -0
- package/lib/module/ui/components/index.js.map +1 -1
- package/lib/module/ui/components/internal/GroupedPillButtons.js +1 -1
- package/lib/module/ui/components/internal/GroupedPillButtons.js.map +1 -1
- package/lib/module/ui/components/internal/TextField.js +607 -547
- package/lib/module/ui/components/internal/TextField.js.map +1 -1
- package/lib/module/ui/components/internal/TextField.md +436 -0
- package/lib/module/ui/context/OxyContext.js +121 -77
- package/lib/module/ui/context/OxyContext.js.map +1 -1
- package/lib/module/ui/hooks/useSessionSocket.js +5 -2
- package/lib/module/ui/hooks/useSessionSocket.js.map +1 -1
- package/lib/module/ui/navigation/OxyRouter.js +1 -1
- package/lib/module/ui/navigation/OxyRouter.js.map +1 -1
- package/lib/module/ui/screens/AccountCenterScreen.js +6 -6
- package/lib/module/ui/screens/AccountCenterScreen.js.map +1 -1
- package/lib/module/ui/screens/AccountManagementDemo.js +3 -3
- package/lib/module/ui/screens/AccountManagementDemo.js.map +1 -1
- package/lib/module/ui/screens/AccountOverviewScreen.js +242 -597
- package/lib/module/ui/screens/AccountOverviewScreen.js.map +1 -1
- package/lib/module/ui/screens/AccountSettingsScreen.js +1152 -407
- package/lib/module/ui/screens/AccountSettingsScreen.js.map +1 -1
- package/lib/module/ui/screens/AccountSwitcherScreen.js +135 -237
- package/lib/module/ui/screens/AccountSwitcherScreen.js.map +1 -1
- package/lib/module/ui/screens/AppInfoScreen.js +248 -465
- package/lib/module/ui/screens/AppInfoScreen.js.map +1 -1
- package/lib/module/ui/screens/FeedbackScreen.js +3 -3
- package/lib/module/ui/screens/FeedbackScreen.js.map +1 -1
- package/lib/module/ui/screens/PaymentGatewayScreen.js +809 -651
- package/lib/module/ui/screens/PaymentGatewayScreen.js.map +1 -1
- package/lib/module/ui/screens/RecoverAccountScreen.js +53 -74
- package/lib/module/ui/screens/RecoverAccountScreen.js.map +1 -1
- package/lib/module/ui/screens/SessionManagementScreen.js +11 -29
- package/lib/module/ui/screens/SessionManagementScreen.js.map +1 -1
- package/lib/module/ui/screens/SignInScreen.js +32 -305
- package/lib/module/ui/screens/SignInScreen.js.map +1 -1
- package/lib/module/ui/screens/SignUpScreen.js +5 -5
- package/lib/module/ui/screens/SignUpScreen.js.map +1 -1
- package/lib/module/ui/screens/internal/SignInPasswordStep.js +19 -31
- package/lib/module/ui/screens/internal/SignInPasswordStep.js.map +1 -1
- package/lib/module/ui/screens/internal/SignInUsernameStep.js +7 -10
- package/lib/module/ui/screens/internal/SignInUsernameStep.js.map +1 -1
- package/lib/module/ui/screens/internal/SignUpIdentityStep.js +11 -5
- package/lib/module/ui/screens/internal/SignUpIdentityStep.js.map +1 -1
- package/lib/module/ui/screens/internal/SignUpSecurityStep.js +11 -4
- package/lib/module/ui/screens/internal/SignUpSecurityStep.js.map +1 -1
- package/lib/module/ui/stores/authStore.js +12 -0
- package/lib/module/ui/stores/authStore.js.map +1 -1
- package/lib/module/ui/styles/authStyles.js +332 -0
- package/lib/module/ui/styles/authStyles.js.map +1 -0
- package/lib/module/ui/styles/index.js +1 -0
- package/lib/module/ui/styles/index.js.map +1 -1
- package/lib/typescript/core/index.d.ts +68 -24
- package/lib/typescript/core/index.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +13 -3
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/node/createAuth.d.ts +112 -0
- package/lib/typescript/node/createAuth.d.ts.map +1 -1
- package/lib/typescript/node/index.d.ts +2 -0
- package/lib/typescript/node/index.d.ts.map +1 -1
- package/lib/typescript/ui/components/Avatar.d.ts.map +1 -1
- package/lib/typescript/ui/components/GroupedItem.d.ts +6 -0
- package/lib/typescript/ui/components/GroupedItem.d.ts.map +1 -1
- package/lib/typescript/ui/components/GroupedSection.d.ts +6 -0
- package/lib/typescript/ui/components/GroupedSection.d.ts.map +1 -1
- package/lib/typescript/ui/components/Header.d.ts +22 -0
- package/lib/typescript/ui/components/Header.d.ts.map +1 -0
- package/lib/typescript/ui/components/OxyProvider.d.ts.map +1 -1
- package/lib/typescript/ui/components/index.d.ts +1 -0
- package/lib/typescript/ui/components/index.d.ts.map +1 -1
- package/lib/typescript/ui/components/internal/TextField.d.ts +31 -16
- package/lib/typescript/ui/components/internal/TextField.d.ts.map +1 -1
- package/lib/typescript/ui/context/OxyContext.d.ts +5 -2
- package/lib/typescript/ui/context/OxyContext.d.ts.map +1 -1
- package/lib/typescript/ui/hooks/useSessionSocket.d.ts.map +1 -1
- package/lib/typescript/ui/navigation/types.d.ts +9 -2
- package/lib/typescript/ui/navigation/types.d.ts.map +1 -1
- package/lib/typescript/ui/screens/AccountOverviewScreen.d.ts.map +1 -1
- package/lib/typescript/ui/screens/AccountSettingsScreen.d.ts.map +1 -1
- package/lib/typescript/ui/screens/AccountSwitcherScreen.d.ts.map +1 -1
- package/lib/typescript/ui/screens/AppInfoScreen.d.ts.map +1 -1
- package/lib/typescript/ui/screens/PaymentGatewayScreen.d.ts.map +1 -1
- package/lib/typescript/ui/screens/RecoverAccountScreen.d.ts +5 -1
- package/lib/typescript/ui/screens/RecoverAccountScreen.d.ts.map +1 -1
- package/lib/typescript/ui/screens/SessionManagementScreen.d.ts.map +1 -1
- package/lib/typescript/ui/screens/SignInScreen.d.ts.map +1 -1
- package/lib/typescript/ui/screens/internal/SignInPasswordStep.d.ts +1 -1
- package/lib/typescript/ui/screens/internal/SignInPasswordStep.d.ts.map +1 -1
- package/lib/typescript/ui/screens/internal/SignInUsernameStep.d.ts +0 -1
- package/lib/typescript/ui/screens/internal/SignInUsernameStep.d.ts.map +1 -1
- package/lib/typescript/ui/screens/internal/SignUpIdentityStep.d.ts.map +1 -1
- package/lib/typescript/ui/screens/internal/SignUpSecurityStep.d.ts.map +1 -1
- package/lib/typescript/ui/stores/authStore.d.ts.map +1 -1
- package/lib/typescript/ui/styles/authStyles.d.ts +326 -0
- package/lib/typescript/ui/styles/authStyles.d.ts.map +1 -0
- package/lib/typescript/ui/styles/index.d.ts +1 -0
- package/lib/typescript/ui/styles/index.d.ts.map +1 -1
- package/package.json +1 -4
- package/src/core/index.ts +195 -41
- package/src/index.ts +72 -4
- package/src/node/createAuth.ts +623 -7
- package/src/node/index.ts +19 -1
- package/src/ui/components/Avatar.tsx +11 -5
- package/src/ui/components/GroupedItem.tsx +57 -9
- package/src/ui/components/GroupedSection.tsx +12 -0
- package/src/ui/components/Header.tsx +364 -0
- package/src/ui/components/OxyProvider.tsx +31 -15
- package/src/ui/components/index.ts +1 -0
- package/src/ui/components/internal/GroupedPillButtons.tsx +1 -1
- package/src/ui/components/internal/TextField.md +436 -0
- package/src/ui/components/internal/TextField.tsx +720 -620
- package/src/ui/context/OxyContext.tsx +150 -63
- package/src/ui/hooks/useSessionSocket.ts +5 -2
- package/src/ui/navigation/OxyRouter.tsx +1 -1
- package/src/ui/navigation/types.ts +10 -2
- package/src/ui/screens/AccountCenterScreen.tsx +5 -5
- package/src/ui/screens/AccountManagementDemo.tsx +9 -9
- package/src/ui/screens/AccountOverviewScreen.tsx +265 -414
- package/src/ui/screens/AccountSettingsScreen.tsx +1165 -403
- package/src/ui/screens/AccountSwitcherScreen.tsx +158 -202
- package/src/ui/screens/AppInfoScreen.tsx +270 -497
- package/src/ui/screens/FeedbackScreen.tsx +3 -3
- package/src/ui/screens/PaymentGatewayScreen.tsx +668 -365
- package/src/ui/screens/ProfileScreen.tsx +5 -5
- package/src/ui/screens/RecoverAccountScreen.tsx +46 -74
- package/src/ui/screens/SessionManagementScreen.tsx +14 -22
- package/src/ui/screens/SignInScreen.tsx +27 -294
- package/src/ui/screens/SignUpScreen.tsx +5 -5
- package/src/ui/screens/internal/SignInPasswordStep.tsx +11 -22
- package/src/ui/screens/internal/SignInUsernameStep.tsx +3 -10
- package/src/ui/screens/internal/SignUpIdentityStep.tsx +2 -5
- package/src/ui/screens/internal/SignUpSecurityStep.tsx +3 -4
- package/src/ui/stores/authStore.ts +12 -0
- package/src/ui/styles/authStyles.ts +352 -0
- package/src/ui/styles/index.ts +1 -0
- package/lib/commonjs/core/auth-manager.js +0 -440
- package/lib/commonjs/core/auth-manager.js.map +0 -1
- package/lib/commonjs/core/use-auth.js +0 -244
- package/lib/commonjs/core/use-auth.js.map +0 -1
- package/lib/module/core/auth-manager.js +0 -432
- package/lib/module/core/auth-manager.js.map +0 -1
- package/lib/module/core/use-auth.js +0 -235
- package/lib/module/core/use-auth.js.map +0 -1
- package/lib/typescript/core/auth-manager.d.ts +0 -136
- package/lib/typescript/core/auth-manager.d.ts.map +0 -1
- package/lib/typescript/core/use-auth.d.ts +0 -79
- package/lib/typescript/core/use-auth.d.ts.map +0 -1
- package/src/__tests__/middleware.test.ts +0 -105
- package/src/__tests__/setup.ts +0 -10
- package/src/__tests__/zero-config-auth.test.ts +0 -607
- package/src/core/auth-manager.ts +0 -500
- package/src/core/use-auth.tsx +0 -245
package/src/core/auth-manager.ts
DELETED
|
@@ -1,500 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Zero-Config Authentication Manager
|
|
3
|
-
*
|
|
4
|
-
* This module provides automatic token management, session handling,
|
|
5
|
-
* and seamless authentication for Oxy services.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import axios, { AxiosInstance, AxiosError, InternalAxiosRequestConfig } from 'axios';
|
|
9
|
-
import { jwtDecode } from 'jwt-decode';
|
|
10
|
-
import AsyncStorage from '@react-native-async-storage/async-storage';
|
|
11
|
-
|
|
12
|
-
interface AuthTokens {
|
|
13
|
-
accessToken: string;
|
|
14
|
-
refreshToken: string;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
interface JwtPayload {
|
|
18
|
-
exp: number;
|
|
19
|
-
userId: string;
|
|
20
|
-
username: string;
|
|
21
|
-
[key: string]: any;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
interface AuthState {
|
|
25
|
-
isAuthenticated: boolean;
|
|
26
|
-
user: any | null;
|
|
27
|
-
tokens: AuthTokens | null;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
interface LoginCredentials {
|
|
31
|
-
username: string;
|
|
32
|
-
password: string;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
interface LoginResponse {
|
|
36
|
-
success: boolean;
|
|
37
|
-
accessToken: string;
|
|
38
|
-
refreshToken: string;
|
|
39
|
-
user: any;
|
|
40
|
-
message?: string; // Make message optional
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export class AuthenticationManager {
|
|
44
|
-
private client: AxiosInstance;
|
|
45
|
-
private tokens: AuthTokens | null = null;
|
|
46
|
-
private user: any | null = null;
|
|
47
|
-
private refreshPromise: Promise<AuthTokens> | null = null;
|
|
48
|
-
private listeners: ((state: AuthState) => void)[] = [];
|
|
49
|
-
private storageKey = '@oxy/auth-tokens';
|
|
50
|
-
|
|
51
|
-
constructor(baseURL: string) {
|
|
52
|
-
this.client = axios.create({
|
|
53
|
-
baseURL,
|
|
54
|
-
timeout: 15000,
|
|
55
|
-
withCredentials: true, // Enable cookies for session management
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
this.setupInterceptors();
|
|
59
|
-
this.initializeFromStorage();
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Setup axios interceptors for automatic token management
|
|
64
|
-
*/
|
|
65
|
-
private setupInterceptors(): void {
|
|
66
|
-
// Request interceptor - automatically add auth headers
|
|
67
|
-
this.client.interceptors.request.use(
|
|
68
|
-
async (config: InternalAxiosRequestConfig) => {
|
|
69
|
-
// Skip auth for login/signup endpoints
|
|
70
|
-
if (this.isPublicEndpoint(config.url || '')) {
|
|
71
|
-
return config;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// Ensure we have a valid token
|
|
75
|
-
await this.ensureValidToken();
|
|
76
|
-
|
|
77
|
-
// Add authorization header if we have a token
|
|
78
|
-
if (this.tokens?.accessToken) {
|
|
79
|
-
config.headers = config.headers || {};
|
|
80
|
-
config.headers.Authorization = `Bearer ${this.tokens.accessToken}`;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
return config;
|
|
84
|
-
},
|
|
85
|
-
(error) => Promise.reject(error)
|
|
86
|
-
);
|
|
87
|
-
|
|
88
|
-
// Response interceptor - handle token expiration and auto-retry
|
|
89
|
-
this.client.interceptors.response.use(
|
|
90
|
-
(response) => response,
|
|
91
|
-
async (error: AxiosError) => {
|
|
92
|
-
const originalRequest = error.config as InternalAxiosRequestConfig & { _retry?: boolean };
|
|
93
|
-
|
|
94
|
-
// If it's a 401 and we haven't already retried, attempt token refresh
|
|
95
|
-
if (
|
|
96
|
-
error.response?.status === 401 &&
|
|
97
|
-
!originalRequest._retry &&
|
|
98
|
-
this.tokens?.refreshToken &&
|
|
99
|
-
!this.isPublicEndpoint(originalRequest?.url || '')
|
|
100
|
-
) {
|
|
101
|
-
originalRequest._retry = true;
|
|
102
|
-
|
|
103
|
-
try {
|
|
104
|
-
await this.refreshTokens();
|
|
105
|
-
|
|
106
|
-
// Retry original request with new token
|
|
107
|
-
if (originalRequest && this.tokens?.accessToken) {
|
|
108
|
-
originalRequest.headers = originalRequest.headers || {};
|
|
109
|
-
originalRequest.headers.Authorization = `Bearer ${this.tokens.accessToken}`;
|
|
110
|
-
return this.client(originalRequest);
|
|
111
|
-
}
|
|
112
|
-
} catch (refreshError) {
|
|
113
|
-
// Refresh failed, clear tokens and notify listeners
|
|
114
|
-
await this.logout();
|
|
115
|
-
return Promise.reject(error);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// For non-auth errors or failed retries, reject with formatted error
|
|
120
|
-
return Promise.reject(this.formatError(error));
|
|
121
|
-
}
|
|
122
|
-
);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Check if endpoint is public (doesn't require authentication)
|
|
127
|
-
*/
|
|
128
|
-
private isPublicEndpoint(url: string): boolean {
|
|
129
|
-
const publicPaths = [
|
|
130
|
-
'/auth/login',
|
|
131
|
-
'/auth/signup',
|
|
132
|
-
'/auth/register',
|
|
133
|
-
'/auth/check-username',
|
|
134
|
-
'/auth/check-email',
|
|
135
|
-
'/health',
|
|
136
|
-
'/'
|
|
137
|
-
];
|
|
138
|
-
|
|
139
|
-
return publicPaths.some(path => url.includes(path));
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* Initialize authentication state from persistent storage
|
|
144
|
-
*/
|
|
145
|
-
private async initializeFromStorage(): Promise<void> {
|
|
146
|
-
try {
|
|
147
|
-
const storedData = await AsyncStorage.getItem(this.storageKey);
|
|
148
|
-
if (storedData) {
|
|
149
|
-
const tokens = JSON.parse(storedData) as AuthTokens;
|
|
150
|
-
|
|
151
|
-
// Validate that tokens haven't expired
|
|
152
|
-
if (await this.validateStoredTokens(tokens)) {
|
|
153
|
-
this.tokens = tokens;
|
|
154
|
-
await this.fetchCurrentUser();
|
|
155
|
-
this.notifyStateChange();
|
|
156
|
-
} else {
|
|
157
|
-
// Tokens expired, clear storage
|
|
158
|
-
await AsyncStorage.removeItem(this.storageKey);
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
} catch (error) {
|
|
162
|
-
console.warn('[OxyAuth] Failed to initialize from storage:', error);
|
|
163
|
-
await AsyncStorage.removeItem(this.storageKey);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Validate stored tokens without making network calls if possible
|
|
169
|
-
*/
|
|
170
|
-
private async validateStoredTokens(tokens: AuthTokens): Promise<boolean> {
|
|
171
|
-
try {
|
|
172
|
-
// First check if access token is expired
|
|
173
|
-
const decoded = jwtDecode<JwtPayload>(tokens.accessToken);
|
|
174
|
-
const now = Math.floor(Date.now() / 1000);
|
|
175
|
-
|
|
176
|
-
// If access token is still valid, we're good
|
|
177
|
-
if (decoded.exp > now + 60) { // 60 second buffer
|
|
178
|
-
return true;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
// Access token expired, try refresh token
|
|
182
|
-
const refreshDecoded = jwtDecode<JwtPayload>(tokens.refreshToken);
|
|
183
|
-
return refreshDecoded.exp > now;
|
|
184
|
-
} catch {
|
|
185
|
-
return false;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* Ensure we have a valid access token
|
|
191
|
-
*/
|
|
192
|
-
private async ensureValidToken(): Promise<void> {
|
|
193
|
-
if (!this.tokens?.accessToken) {
|
|
194
|
-
return;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
try {
|
|
198
|
-
const decoded = jwtDecode<JwtPayload>(this.tokens.accessToken);
|
|
199
|
-
const now = Math.floor(Date.now() / 1000);
|
|
200
|
-
|
|
201
|
-
// Refresh if token expires within 5 minutes
|
|
202
|
-
if (decoded.exp - now < 300) {
|
|
203
|
-
await this.refreshTokens();
|
|
204
|
-
}
|
|
205
|
-
} catch (error) {
|
|
206
|
-
console.warn('[OxyAuth] Token validation error:', error);
|
|
207
|
-
// If token is malformed, try refresh
|
|
208
|
-
if (this.tokens?.refreshToken) {
|
|
209
|
-
await this.refreshTokens();
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
* Login with credentials
|
|
216
|
-
*/
|
|
217
|
-
async login(credentials: LoginCredentials): Promise<LoginResponse> {
|
|
218
|
-
try {
|
|
219
|
-
const response = await this.client.post('/auth/login', credentials);
|
|
220
|
-
const loginData = response.data as LoginResponse;
|
|
221
|
-
|
|
222
|
-
if (loginData.success && loginData.accessToken && loginData.refreshToken) {
|
|
223
|
-
await this.setTokens({
|
|
224
|
-
accessToken: loginData.accessToken,
|
|
225
|
-
refreshToken: loginData.refreshToken,
|
|
226
|
-
});
|
|
227
|
-
|
|
228
|
-
this.user = loginData.user;
|
|
229
|
-
this.notifyStateChange();
|
|
230
|
-
|
|
231
|
-
return loginData;
|
|
232
|
-
} else {
|
|
233
|
-
throw new Error(loginData.message || 'Login failed');
|
|
234
|
-
}
|
|
235
|
-
} catch (error) {
|
|
236
|
-
throw this.formatError(error);
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
/**
|
|
241
|
-
* Register new user
|
|
242
|
-
*/
|
|
243
|
-
async register(userData: { username: string; email: string; password: string }): Promise<LoginResponse> {
|
|
244
|
-
try {
|
|
245
|
-
const response = await this.client.post('/auth/register', userData);
|
|
246
|
-
const registerData = response.data as LoginResponse;
|
|
247
|
-
|
|
248
|
-
if (registerData.success && registerData.accessToken && registerData.refreshToken) {
|
|
249
|
-
await this.setTokens({
|
|
250
|
-
accessToken: registerData.accessToken,
|
|
251
|
-
refreshToken: registerData.refreshToken,
|
|
252
|
-
});
|
|
253
|
-
|
|
254
|
-
this.user = registerData.user;
|
|
255
|
-
this.notifyStateChange();
|
|
256
|
-
|
|
257
|
-
return registerData;
|
|
258
|
-
} else {
|
|
259
|
-
throw new Error(registerData.message || 'Registration failed');
|
|
260
|
-
}
|
|
261
|
-
} catch (error) {
|
|
262
|
-
throw this.formatError(error);
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
/**
|
|
267
|
-
* Logout user and clear all tokens
|
|
268
|
-
*/
|
|
269
|
-
async logout(): Promise<void> {
|
|
270
|
-
// Attempt server-side logout if we have tokens
|
|
271
|
-
if (this.tokens?.refreshToken) {
|
|
272
|
-
try {
|
|
273
|
-
await this.client.post('/auth/logout', {
|
|
274
|
-
refreshToken: this.tokens.refreshToken,
|
|
275
|
-
});
|
|
276
|
-
} catch (error) {
|
|
277
|
-
console.warn('[OxyAuth] Server logout failed:', error);
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
// Clear local state
|
|
282
|
-
this.tokens = null;
|
|
283
|
-
this.user = null;
|
|
284
|
-
|
|
285
|
-
// Clear storage
|
|
286
|
-
try {
|
|
287
|
-
await AsyncStorage.removeItem(this.storageKey);
|
|
288
|
-
} catch (error) {
|
|
289
|
-
console.warn('[OxyAuth] Failed to clear storage:', error);
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
this.notifyStateChange();
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
/**
|
|
296
|
-
* Refresh access token using refresh token
|
|
297
|
-
*/
|
|
298
|
-
private async refreshTokens(): Promise<AuthTokens> {
|
|
299
|
-
if (!this.tokens?.refreshToken) {
|
|
300
|
-
throw new Error('No refresh token available');
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
// If refresh is already in progress, return that promise
|
|
304
|
-
if (this.refreshPromise) {
|
|
305
|
-
return this.refreshPromise;
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
this.refreshPromise = this.performTokenRefresh();
|
|
309
|
-
|
|
310
|
-
try {
|
|
311
|
-
const newTokens = await this.refreshPromise;
|
|
312
|
-
this.refreshPromise = null;
|
|
313
|
-
return newTokens;
|
|
314
|
-
} catch (error) {
|
|
315
|
-
this.refreshPromise = null;
|
|
316
|
-
throw error;
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
/**
|
|
321
|
-
* Perform the actual token refresh
|
|
322
|
-
*/
|
|
323
|
-
private async performTokenRefresh(): Promise<AuthTokens> {
|
|
324
|
-
try {
|
|
325
|
-
const response = await this.client.post('/auth/refresh', {
|
|
326
|
-
refreshToken: this.tokens!.refreshToken,
|
|
327
|
-
});
|
|
328
|
-
|
|
329
|
-
const newTokens: AuthTokens = {
|
|
330
|
-
accessToken: response.data.accessToken,
|
|
331
|
-
refreshToken: response.data.refreshToken,
|
|
332
|
-
};
|
|
333
|
-
|
|
334
|
-
await this.setTokens(newTokens);
|
|
335
|
-
return newTokens;
|
|
336
|
-
} catch (error) {
|
|
337
|
-
// Refresh failed, clear all tokens
|
|
338
|
-
await this.logout();
|
|
339
|
-
throw this.formatError(error);
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
/**
|
|
344
|
-
* Set tokens and persist to storage
|
|
345
|
-
*/
|
|
346
|
-
private async setTokens(newTokens: AuthTokens): Promise<void> {
|
|
347
|
-
this.tokens = newTokens;
|
|
348
|
-
|
|
349
|
-
try {
|
|
350
|
-
await AsyncStorage.setItem(this.storageKey, JSON.stringify(newTokens));
|
|
351
|
-
} catch (error) {
|
|
352
|
-
console.warn('[OxyAuth] Failed to persist tokens:', error);
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
/**
|
|
357
|
-
* Fetch current user profile
|
|
358
|
-
*/
|
|
359
|
-
private async fetchCurrentUser(): Promise<void> {
|
|
360
|
-
try {
|
|
361
|
-
const response = await this.client.get('/auth/me');
|
|
362
|
-
this.user = response.data.data || response.data;
|
|
363
|
-
} catch (error) {
|
|
364
|
-
console.warn('[OxyAuth] Failed to fetch current user:', error);
|
|
365
|
-
// Don't throw here, we can still function without user profile
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
/**
|
|
370
|
-
* Get current authentication state
|
|
371
|
-
*/
|
|
372
|
-
getAuthState(): AuthState {
|
|
373
|
-
return {
|
|
374
|
-
isAuthenticated: !!(this.tokens?.accessToken),
|
|
375
|
-
user: this.user,
|
|
376
|
-
tokens: this.tokens,
|
|
377
|
-
};
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
/**
|
|
381
|
-
* Get current user (loads if not cached)
|
|
382
|
-
*/
|
|
383
|
-
async getCurrentUser(): Promise<any> {
|
|
384
|
-
if (!this.tokens?.accessToken) {
|
|
385
|
-
throw new Error('Not authenticated');
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
if (!this.user) {
|
|
389
|
-
await this.fetchCurrentUser();
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
return this.user;
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
/**
|
|
396
|
-
* Subscribe to authentication state changes
|
|
397
|
-
*/
|
|
398
|
-
onAuthStateChange(callback: (state: AuthState) => void): () => void {
|
|
399
|
-
this.listeners.push(callback);
|
|
400
|
-
|
|
401
|
-
// Immediately call with current state
|
|
402
|
-
callback(this.getAuthState());
|
|
403
|
-
|
|
404
|
-
// Return unsubscribe function
|
|
405
|
-
return () => {
|
|
406
|
-
this.listeners = this.listeners.filter(listener => listener !== callback);
|
|
407
|
-
};
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
/**
|
|
411
|
-
* Notify all listeners of state changes
|
|
412
|
-
*/
|
|
413
|
-
private notifyStateChange(): void {
|
|
414
|
-
const state = this.getAuthState();
|
|
415
|
-
this.listeners.forEach(listener => {
|
|
416
|
-
try {
|
|
417
|
-
listener(state);
|
|
418
|
-
} catch (error) {
|
|
419
|
-
console.error('[OxyAuth] Listener error:', error);
|
|
420
|
-
}
|
|
421
|
-
});
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
/**
|
|
425
|
-
* Format error responses consistently
|
|
426
|
-
*/
|
|
427
|
-
private formatError(error: any): Error {
|
|
428
|
-
if (error?.response?.data?.message) {
|
|
429
|
-
return new Error(error.response.data.message);
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
if (error?.message) {
|
|
433
|
-
return new Error(error.message);
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
return new Error('An unexpected error occurred');
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
/**
|
|
440
|
-
* Get authenticated HTTP client for making API calls
|
|
441
|
-
*/
|
|
442
|
-
getClient(): AxiosInstance {
|
|
443
|
-
return this.client;
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
/**
|
|
447
|
-
* Check username availability
|
|
448
|
-
*/
|
|
449
|
-
async checkUsernameAvailability(username: string): Promise<{ available: boolean; message: string }> {
|
|
450
|
-
try {
|
|
451
|
-
const response = await this.client.get(`/auth/check-username/${encodeURIComponent(username)}`);
|
|
452
|
-
return response.data;
|
|
453
|
-
} catch (error) {
|
|
454
|
-
const axiosError = error as AxiosError;
|
|
455
|
-
if (axiosError?.response?.status === 400) {
|
|
456
|
-
return (axiosError.response as any).data;
|
|
457
|
-
}
|
|
458
|
-
throw this.formatError(error);
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
/**
|
|
463
|
-
* Check email availability
|
|
464
|
-
*/
|
|
465
|
-
async checkEmailAvailability(email: string): Promise<{ available: boolean; message: string }> {
|
|
466
|
-
try {
|
|
467
|
-
const response = await this.client.post('/auth/check-email', { email });
|
|
468
|
-
return response.data;
|
|
469
|
-
} catch (error) {
|
|
470
|
-
const axiosError = error as AxiosError;
|
|
471
|
-
if (axiosError?.response?.status === 400) {
|
|
472
|
-
return (axiosError.response as any).data;
|
|
473
|
-
}
|
|
474
|
-
throw this.formatError(error);
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
// Global auth manager instance
|
|
480
|
-
let globalAuthManager: AuthenticationManager | null = null;
|
|
481
|
-
|
|
482
|
-
/**
|
|
483
|
-
* Initialize global authentication manager
|
|
484
|
-
*/
|
|
485
|
-
export function initializeAuth(baseURL: string): AuthenticationManager {
|
|
486
|
-
if (!globalAuthManager) {
|
|
487
|
-
globalAuthManager = new AuthenticationManager(baseURL);
|
|
488
|
-
}
|
|
489
|
-
return globalAuthManager;
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
/**
|
|
493
|
-
* Get global authentication manager instance
|
|
494
|
-
*/
|
|
495
|
-
export function getAuthManager(): AuthenticationManager {
|
|
496
|
-
if (!globalAuthManager) {
|
|
497
|
-
throw new Error('Authentication manager not initialized. Call initializeAuth() first.');
|
|
498
|
-
}
|
|
499
|
-
return globalAuthManager;
|
|
500
|
-
}
|