@cranberry-money/shared-services 3.0.1 → 4.1.0
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/dist/index.d.ts +0 -27
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -115
- package/package.json +4 -3
- package/dist/api/functional-client.d.ts +0 -66
- package/dist/api/functional-client.d.ts.map +0 -1
- package/dist/api/functional-client.js +0 -165
- package/dist/auth/functional/auth-operations.d.ts +0 -116
- package/dist/auth/functional/auth-operations.d.ts.map +0 -1
- package/dist/auth/functional/auth-operations.js +0 -246
- package/dist/auth/functional/auth-state.d.ts +0 -38
- package/dist/auth/functional/auth-state.d.ts.map +0 -1
- package/dist/auth/functional/auth-state.js +0 -87
- package/dist/auth/functional/token-storage.d.ts +0 -44
- package/dist/auth/functional/token-storage.d.ts.map +0 -1
- package/dist/auth/functional/token-storage.js +0 -178
- package/dist/auth/react/AuthProvider.d.ts +0 -92
- package/dist/auth/react/AuthProvider.d.ts.map +0 -1
- package/dist/auth/react/AuthProvider.js +0 -207
- package/dist/integration/blueberry-integration.d.ts +0 -21
- package/dist/integration/blueberry-integration.d.ts.map +0 -1
- package/dist/integration/blueberry-integration.js +0 -109
- package/dist/integration/index.d.ts +0 -8
- package/dist/integration/index.d.ts.map +0 -1
- package/dist/integration/index.js +0 -18
- package/dist/services/accounts.d.ts +0 -220
- package/dist/services/accounts.d.ts.map +0 -1
- package/dist/services/accounts.js +0 -175
- package/dist/services/banks.d.ts +0 -123
- package/dist/services/banks.d.ts.map +0 -1
- package/dist/services/banks.js +0 -151
- package/dist/services/cash-accounts.d.ts +0 -112
- package/dist/services/cash-accounts.d.ts.map +0 -1
- package/dist/services/cash-accounts.js +0 -222
- package/dist/services/documents.d.ts +0 -143
- package/dist/services/documents.d.ts.map +0 -1
- package/dist/services/documents.js +0 -253
- package/dist/services/factories/account-factory.d.ts +0 -16
- package/dist/services/factories/account-factory.d.ts.map +0 -1
- package/dist/services/factories/account-factory.js +0 -79
- package/dist/services/factories/auth-factory.d.ts +0 -23
- package/dist/services/factories/auth-factory.d.ts.map +0 -1
- package/dist/services/factories/auth-factory.js +0 -75
- package/dist/services/factories/bank-factory.d.ts +0 -16
- package/dist/services/factories/bank-factory.d.ts.map +0 -1
- package/dist/services/factories/bank-factory.js +0 -72
- package/dist/services/factories/cash-account-factory.d.ts +0 -16
- package/dist/services/factories/cash-account-factory.d.ts.map +0 -1
- package/dist/services/factories/cash-account-factory.js +0 -74
- package/dist/services/factories/document-factory.d.ts +0 -16
- package/dist/services/factories/document-factory.d.ts.map +0 -1
- package/dist/services/factories/document-factory.js +0 -85
- package/dist/services/factories/index.d.ts +0 -21
- package/dist/services/factories/index.d.ts.map +0 -1
- package/dist/services/factories/index.js +0 -40
- package/dist/services/factories/instrument-factory.d.ts +0 -16
- package/dist/services/factories/instrument-factory.d.ts.map +0 -1
- package/dist/services/factories/instrument-factory.js +0 -68
- package/dist/services/factories/master-factory.d.ts +0 -74
- package/dist/services/factories/master-factory.d.ts.map +0 -1
- package/dist/services/factories/master-factory.js +0 -183
- package/dist/services/factories/portfolio-factory.d.ts +0 -16
- package/dist/services/factories/portfolio-factory.d.ts.map +0 -1
- package/dist/services/factories/portfolio-factory.js +0 -74
- package/dist/services/factories/portfolio-template-factory.d.ts +0 -16
- package/dist/services/factories/portfolio-template-factory.d.ts.map +0 -1
- package/dist/services/factories/portfolio-template-factory.js +0 -76
- package/dist/services/factories/reference-data-factory.d.ts +0 -16
- package/dist/services/factories/reference-data-factory.d.ts.map +0 -1
- package/dist/services/factories/reference-data-factory.js +0 -86
- package/dist/services/factories/tax-residency-factory.d.ts +0 -16
- package/dist/services/factories/tax-residency-factory.d.ts.map +0 -1
- package/dist/services/factories/tax-residency-factory.js +0 -73
- package/dist/services/factories/trade-factory.d.ts +0 -16
- package/dist/services/factories/trade-factory.d.ts.map +0 -1
- package/dist/services/factories/trade-factory.js +0 -79
- package/dist/services/factories/types.d.ts +0 -250
- package/dist/services/factories/types.d.ts.map +0 -1
- package/dist/services/factories/types.js +0 -32
- package/dist/services/factories/withdrawal-factory.d.ts +0 -16
- package/dist/services/factories/withdrawal-factory.d.ts.map +0 -1
- package/dist/services/factories/withdrawal-factory.js +0 -78
- package/dist/services/instruments.d.ts +0 -138
- package/dist/services/instruments.d.ts.map +0 -1
- package/dist/services/instruments.js +0 -178
- package/dist/services/portfolio-templates.d.ts +0 -142
- package/dist/services/portfolio-templates.d.ts.map +0 -1
- package/dist/services/portfolio-templates.js +0 -201
- package/dist/services/portfolios.d.ts +0 -157
- package/dist/services/portfolios.d.ts.map +0 -1
- package/dist/services/portfolios.js +0 -144
- package/dist/services/reference-data.d.ts +0 -185
- package/dist/services/reference-data.d.ts.map +0 -1
- package/dist/services/reference-data.js +0 -245
- package/dist/services/tax-residencies.d.ts +0 -83
- package/dist/services/tax-residencies.d.ts.map +0 -1
- package/dist/services/tax-residencies.js +0 -179
- package/dist/services/trades.d.ts +0 -190
- package/dist/services/trades.d.ts.map +0 -1
- package/dist/services/trades.js +0 -207
- package/dist/services/withdrawals.d.ts +0 -236
- package/dist/services/withdrawals.d.ts.map +0 -1
- package/dist/services/withdrawals.js +0 -345
|
@@ -1,246 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Pure Authentication Operations
|
|
4
|
-
*
|
|
5
|
-
* All authentication operations as pure functions with explicit dependencies.
|
|
6
|
-
* No side effects, no global state - everything is explicit and testable.
|
|
7
|
-
*/
|
|
8
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.validateSignupData = exports.validateSigninCredentials = exports.initializeFromStorage = exports.resendVerificationCode = exports.verifyEmail = exports.getUserProfile = exports.refreshTokens = exports.signup = exports.signout = exports.signin = void 0;
|
|
10
|
-
// Pure authentication operations
|
|
11
|
-
/**
|
|
12
|
-
* Sign in with credentials
|
|
13
|
-
*
|
|
14
|
-
* @param credentials - User login credentials
|
|
15
|
-
* @param deps - Required dependencies (API client and token storage)
|
|
16
|
-
* @returns Promise resolving to user profile and tokens
|
|
17
|
-
*/
|
|
18
|
-
const signin = async (credentials, deps) => {
|
|
19
|
-
try {
|
|
20
|
-
// Call signin API
|
|
21
|
-
const response = await deps.apiClient.post('/api/signin/', credentials);
|
|
22
|
-
// Extract tokens from response
|
|
23
|
-
const tokens = {
|
|
24
|
-
access: response.access,
|
|
25
|
-
refresh: response.refresh,
|
|
26
|
-
};
|
|
27
|
-
// Store tokens
|
|
28
|
-
await deps.tokenStorage.storeTokens(tokens);
|
|
29
|
-
// Get user profile (either from signin response or separate call)
|
|
30
|
-
const user = response.user || await (0, exports.getUserProfile)(deps.apiClient);
|
|
31
|
-
return { user, tokens };
|
|
32
|
-
}
|
|
33
|
-
catch (error) {
|
|
34
|
-
throw new Error(extractErrorMessage(error));
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
exports.signin = signin;
|
|
38
|
-
/**
|
|
39
|
-
* Sign out user
|
|
40
|
-
*
|
|
41
|
-
* @param deps - Required dependencies
|
|
42
|
-
* @returns Promise resolving when signout is complete
|
|
43
|
-
*/
|
|
44
|
-
const signout = async (deps) => {
|
|
45
|
-
try {
|
|
46
|
-
// Best effort API call - don't fail signout if API call fails
|
|
47
|
-
await deps.apiClient.post('/api/signout/');
|
|
48
|
-
}
|
|
49
|
-
catch (error) {
|
|
50
|
-
console.warn('API signout failed, continuing with local cleanup:', error);
|
|
51
|
-
}
|
|
52
|
-
finally {
|
|
53
|
-
// Always clear local tokens
|
|
54
|
-
await deps.tokenStorage.clearTokens();
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
exports.signout = signout;
|
|
58
|
-
/**
|
|
59
|
-
* Sign up new user
|
|
60
|
-
*
|
|
61
|
-
* @param userData - User registration data
|
|
62
|
-
* @param deps - Required dependencies
|
|
63
|
-
* @returns Promise resolving to success status and optional message
|
|
64
|
-
*/
|
|
65
|
-
const signup = async (userData, deps) => {
|
|
66
|
-
try {
|
|
67
|
-
const response = await deps.apiClient.post('/api/signup/', userData);
|
|
68
|
-
return {
|
|
69
|
-
success: true,
|
|
70
|
-
message: response.message || 'Account created successfully'
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
catch (error) {
|
|
74
|
-
return {
|
|
75
|
-
success: false,
|
|
76
|
-
message: extractErrorMessage(error)
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
};
|
|
80
|
-
exports.signup = signup;
|
|
81
|
-
/**
|
|
82
|
-
* Refresh authentication tokens
|
|
83
|
-
*
|
|
84
|
-
* @param currentTokens - Current token pair
|
|
85
|
-
* @param deps - Required dependencies
|
|
86
|
-
* @returns Promise resolving to new token pair
|
|
87
|
-
*/
|
|
88
|
-
const refreshTokens = async (currentTokens, deps) => {
|
|
89
|
-
try {
|
|
90
|
-
const response = await deps.apiClient.post('/api/token/refresh/', { refresh: currentTokens.refresh });
|
|
91
|
-
const newTokens = {
|
|
92
|
-
access: response.access,
|
|
93
|
-
refresh: response.refresh,
|
|
94
|
-
};
|
|
95
|
-
// Store new tokens
|
|
96
|
-
await deps.tokenStorage.storeTokens(newTokens);
|
|
97
|
-
return newTokens;
|
|
98
|
-
}
|
|
99
|
-
catch (error) {
|
|
100
|
-
// If refresh fails, clear stored tokens
|
|
101
|
-
await deps.tokenStorage.clearTokens();
|
|
102
|
-
throw new Error('Token refresh failed');
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
|
-
exports.refreshTokens = refreshTokens;
|
|
106
|
-
/**
|
|
107
|
-
* Get user profile from API
|
|
108
|
-
*
|
|
109
|
-
* @param apiClient - API client instance
|
|
110
|
-
* @returns Promise resolving to user profile
|
|
111
|
-
*/
|
|
112
|
-
const getUserProfile = async (apiClient) => {
|
|
113
|
-
try {
|
|
114
|
-
return await apiClient.get('/api/profile/');
|
|
115
|
-
}
|
|
116
|
-
catch (error) {
|
|
117
|
-
throw new Error(`Failed to fetch user profile: ${extractErrorMessage(error)}`);
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
|
-
exports.getUserProfile = getUserProfile;
|
|
121
|
-
/**
|
|
122
|
-
* Verify email address
|
|
123
|
-
*
|
|
124
|
-
* @param verificationData - Email verification data
|
|
125
|
-
* @param deps - Required dependencies
|
|
126
|
-
* @returns Promise resolving to success status
|
|
127
|
-
*/
|
|
128
|
-
const verifyEmail = async (verificationData, deps) => {
|
|
129
|
-
try {
|
|
130
|
-
await deps.apiClient.post('/api/email-verification/', verificationData);
|
|
131
|
-
return { success: true, message: 'Email verified successfully' };
|
|
132
|
-
}
|
|
133
|
-
catch (error) {
|
|
134
|
-
return { success: false, message: extractErrorMessage(error) };
|
|
135
|
-
}
|
|
136
|
-
};
|
|
137
|
-
exports.verifyEmail = verifyEmail;
|
|
138
|
-
/**
|
|
139
|
-
* Resend verification code
|
|
140
|
-
*
|
|
141
|
-
* @param deps - Required dependencies
|
|
142
|
-
* @returns Promise resolving to success status
|
|
143
|
-
*/
|
|
144
|
-
const resendVerificationCode = async (deps) => {
|
|
145
|
-
try {
|
|
146
|
-
await deps.apiClient.post('/api/resend-verification/');
|
|
147
|
-
return { success: true, message: 'Verification code sent' };
|
|
148
|
-
}
|
|
149
|
-
catch (error) {
|
|
150
|
-
return { success: false, message: extractErrorMessage(error) };
|
|
151
|
-
}
|
|
152
|
-
};
|
|
153
|
-
exports.resendVerificationCode = resendVerificationCode;
|
|
154
|
-
/**
|
|
155
|
-
* Initialize authentication from stored tokens
|
|
156
|
-
*
|
|
157
|
-
* @param deps - Required dependencies
|
|
158
|
-
* @returns Promise resolving to user and tokens if valid, null otherwise
|
|
159
|
-
*/
|
|
160
|
-
const initializeFromStorage = async (deps) => {
|
|
161
|
-
try {
|
|
162
|
-
// Try to retrieve stored tokens
|
|
163
|
-
const tokens = await deps.tokenStorage.retrieveTokens();
|
|
164
|
-
if (!tokens) {
|
|
165
|
-
return null;
|
|
166
|
-
}
|
|
167
|
-
// Verify tokens are still valid by fetching user profile
|
|
168
|
-
const user = await (0, exports.getUserProfile)(deps.apiClient);
|
|
169
|
-
return { user, tokens };
|
|
170
|
-
}
|
|
171
|
-
catch (error) {
|
|
172
|
-
// If verification fails, clear invalid tokens
|
|
173
|
-
await deps.tokenStorage.clearTokens();
|
|
174
|
-
return null;
|
|
175
|
-
}
|
|
176
|
-
};
|
|
177
|
-
exports.initializeFromStorage = initializeFromStorage;
|
|
178
|
-
// Utility functions
|
|
179
|
-
/**
|
|
180
|
-
* Extract error message from various error formats
|
|
181
|
-
*/
|
|
182
|
-
const extractErrorMessage = (error) => {
|
|
183
|
-
if (typeof error === 'string') {
|
|
184
|
-
return error;
|
|
185
|
-
}
|
|
186
|
-
if (error && typeof error === 'object') {
|
|
187
|
-
const errorObj = error;
|
|
188
|
-
// API error format
|
|
189
|
-
if (errorObj.message) {
|
|
190
|
-
return errorObj.message;
|
|
191
|
-
}
|
|
192
|
-
// Django REST framework format
|
|
193
|
-
if (errorObj.detail) {
|
|
194
|
-
return errorObj.detail;
|
|
195
|
-
}
|
|
196
|
-
// Standard Error object
|
|
197
|
-
if (errorObj.message) {
|
|
198
|
-
return errorObj.message;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
return 'An unexpected error occurred';
|
|
202
|
-
};
|
|
203
|
-
/**
|
|
204
|
-
* Validate signin credentials
|
|
205
|
-
*/
|
|
206
|
-
const validateSigninCredentials = (credentials) => {
|
|
207
|
-
const errors = [];
|
|
208
|
-
if (!credentials.email || !credentials.email.trim()) {
|
|
209
|
-
errors.push('Email is required');
|
|
210
|
-
}
|
|
211
|
-
else if (!isValidEmail(credentials.email)) {
|
|
212
|
-
errors.push('Invalid email format');
|
|
213
|
-
}
|
|
214
|
-
if (!credentials.password || credentials.password.length < 1) {
|
|
215
|
-
errors.push('Password is required');
|
|
216
|
-
}
|
|
217
|
-
return errors;
|
|
218
|
-
};
|
|
219
|
-
exports.validateSigninCredentials = validateSigninCredentials;
|
|
220
|
-
/**
|
|
221
|
-
* Validate signup data
|
|
222
|
-
*/
|
|
223
|
-
const validateSignupData = (userData) => {
|
|
224
|
-
const errors = [];
|
|
225
|
-
if (!userData.email || !userData.email.trim()) {
|
|
226
|
-
errors.push('Email is required');
|
|
227
|
-
}
|
|
228
|
-
else if (!isValidEmail(userData.email)) {
|
|
229
|
-
errors.push('Invalid email format');
|
|
230
|
-
}
|
|
231
|
-
if (!userData.password || userData.password.length < 8) {
|
|
232
|
-
errors.push('Password must be at least 8 characters');
|
|
233
|
-
}
|
|
234
|
-
if (userData.password !== userData.passwordConfirm) {
|
|
235
|
-
errors.push('Passwords do not match');
|
|
236
|
-
}
|
|
237
|
-
return errors;
|
|
238
|
-
};
|
|
239
|
-
exports.validateSignupData = validateSignupData;
|
|
240
|
-
/**
|
|
241
|
-
* Simple email validation
|
|
242
|
-
*/
|
|
243
|
-
const isValidEmail = (email) => {
|
|
244
|
-
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
245
|
-
return emailRegex.test(email);
|
|
246
|
-
};
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Pure Authentication State Management
|
|
3
|
-
*
|
|
4
|
-
* Immutable state structures and pure functions for managing authentication state.
|
|
5
|
-
* All state transitions are explicit and predictable.
|
|
6
|
-
*/
|
|
7
|
-
export interface TokenPair {
|
|
8
|
-
readonly access: string;
|
|
9
|
-
readonly refresh: string;
|
|
10
|
-
}
|
|
11
|
-
export interface UserProfile {
|
|
12
|
-
readonly id: string;
|
|
13
|
-
readonly email: string;
|
|
14
|
-
readonly firstName: string;
|
|
15
|
-
readonly lastName: string;
|
|
16
|
-
readonly isVerified: boolean;
|
|
17
|
-
readonly isSignupCompleted: boolean;
|
|
18
|
-
}
|
|
19
|
-
export interface AuthState {
|
|
20
|
-
readonly isAuthenticated: boolean;
|
|
21
|
-
readonly isLoading: boolean;
|
|
22
|
-
readonly user: UserProfile | null;
|
|
23
|
-
readonly tokens: TokenPair | null;
|
|
24
|
-
readonly error: string | null;
|
|
25
|
-
}
|
|
26
|
-
export declare const createInitialAuthState: () => AuthState;
|
|
27
|
-
export declare const createLoadingAuthState: (currentState: AuthState) => AuthState;
|
|
28
|
-
export declare const createAuthenticatedState: (user: UserProfile, tokens: TokenPair) => AuthState;
|
|
29
|
-
export declare const createUnauthenticatedState: () => AuthState;
|
|
30
|
-
export declare const createErrorState: (currentState: AuthState, error: string) => AuthState;
|
|
31
|
-
export declare const updateTokens: (currentState: AuthState, tokens: TokenPair) => AuthState;
|
|
32
|
-
export declare const clearError: (currentState: AuthState) => AuthState;
|
|
33
|
-
export declare const isValidAuthState: (state: AuthState) => boolean;
|
|
34
|
-
export declare const hasValidTokens: (state: AuthState) => boolean;
|
|
35
|
-
export declare const getDisplayName: (user: UserProfile | null) => string;
|
|
36
|
-
export declare const requiresVerification: (user: UserProfile | null) => boolean;
|
|
37
|
-
export declare const requiresSignupCompletion: (user: UserProfile | null) => boolean;
|
|
38
|
-
//# sourceMappingURL=auth-state.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"auth-state.d.ts","sourceRoot":"","sources":["../../../src/auth/functional/auth-state.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B;AAED,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;IAC7B,QAAQ,CAAC,iBAAiB,EAAE,OAAO,CAAC;CACrC;AAED,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC,eAAe,EAAE,OAAO,CAAC;IAClC,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAC5B,QAAQ,CAAC,IAAI,EAAE,WAAW,GAAG,IAAI,CAAC;IAClC,QAAQ,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI,CAAC;IAClC,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CAC/B;AAGD,eAAO,MAAM,sBAAsB,QAAO,SAMxC,CAAC;AAEH,eAAO,MAAM,sBAAsB,GAAI,cAAc,SAAS,KAAG,SAI/D,CAAC;AAEH,eAAO,MAAM,wBAAwB,GACnC,MAAM,WAAW,EACjB,QAAQ,SAAS,KAChB,SAMD,CAAC;AAEH,eAAO,MAAM,0BAA0B,QAAO,SAM5C,CAAC;AAEH,eAAO,MAAM,gBAAgB,GAC3B,cAAc,SAAS,EACvB,OAAO,MAAM,KACZ,SAID,CAAC;AAEH,eAAO,MAAM,YAAY,GACvB,cAAc,SAAS,EACvB,QAAQ,SAAS,KAChB,SAGD,CAAC;AAEH,eAAO,MAAM,UAAU,GAAI,cAAc,SAAS,KAAG,SAGnD,CAAC;AAGH,eAAO,MAAM,gBAAgB,GAAI,OAAO,SAAS,KAAG,OAQnD,CAAC;AAEF,eAAO,MAAM,cAAc,GAAI,OAAO,SAAS,KAAG,OAIjD,CAAC;AAGF,eAAO,MAAM,cAAc,GAAI,MAAM,WAAW,GAAG,IAAI,KAAG,MAGzD,CAAC;AAEF,eAAO,MAAM,oBAAoB,GAAI,MAAM,WAAW,GAAG,IAAI,KAAG,OAE/D,CAAC;AAEF,eAAO,MAAM,wBAAwB,GAAI,MAAM,WAAW,GAAG,IAAI,KAAG,OAEnE,CAAC"}
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Pure Authentication State Management
|
|
4
|
-
*
|
|
5
|
-
* Immutable state structures and pure functions for managing authentication state.
|
|
6
|
-
* All state transitions are explicit and predictable.
|
|
7
|
-
*/
|
|
8
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.requiresSignupCompletion = exports.requiresVerification = exports.getDisplayName = exports.hasValidTokens = exports.isValidAuthState = exports.clearError = exports.updateTokens = exports.createErrorState = exports.createUnauthenticatedState = exports.createAuthenticatedState = exports.createLoadingAuthState = exports.createInitialAuthState = void 0;
|
|
10
|
-
// State factory functions - all pure functions that return new state
|
|
11
|
-
const createInitialAuthState = () => ({
|
|
12
|
-
isAuthenticated: false,
|
|
13
|
-
isLoading: false,
|
|
14
|
-
user: null,
|
|
15
|
-
tokens: null,
|
|
16
|
-
error: null,
|
|
17
|
-
});
|
|
18
|
-
exports.createInitialAuthState = createInitialAuthState;
|
|
19
|
-
const createLoadingAuthState = (currentState) => ({
|
|
20
|
-
...currentState,
|
|
21
|
-
isLoading: true,
|
|
22
|
-
error: null,
|
|
23
|
-
});
|
|
24
|
-
exports.createLoadingAuthState = createLoadingAuthState;
|
|
25
|
-
const createAuthenticatedState = (user, tokens) => ({
|
|
26
|
-
isAuthenticated: true,
|
|
27
|
-
isLoading: false,
|
|
28
|
-
user,
|
|
29
|
-
tokens,
|
|
30
|
-
error: null,
|
|
31
|
-
});
|
|
32
|
-
exports.createAuthenticatedState = createAuthenticatedState;
|
|
33
|
-
const createUnauthenticatedState = () => ({
|
|
34
|
-
isAuthenticated: false,
|
|
35
|
-
isLoading: false,
|
|
36
|
-
user: null,
|
|
37
|
-
tokens: null,
|
|
38
|
-
error: null,
|
|
39
|
-
});
|
|
40
|
-
exports.createUnauthenticatedState = createUnauthenticatedState;
|
|
41
|
-
const createErrorState = (currentState, error) => ({
|
|
42
|
-
...currentState,
|
|
43
|
-
isLoading: false,
|
|
44
|
-
error,
|
|
45
|
-
});
|
|
46
|
-
exports.createErrorState = createErrorState;
|
|
47
|
-
const updateTokens = (currentState, tokens) => ({
|
|
48
|
-
...currentState,
|
|
49
|
-
tokens,
|
|
50
|
-
});
|
|
51
|
-
exports.updateTokens = updateTokens;
|
|
52
|
-
const clearError = (currentState) => ({
|
|
53
|
-
...currentState,
|
|
54
|
-
error: null,
|
|
55
|
-
});
|
|
56
|
-
exports.clearError = clearError;
|
|
57
|
-
// State validation functions
|
|
58
|
-
const isValidAuthState = (state) => {
|
|
59
|
-
// If authenticated, must have user and tokens
|
|
60
|
-
if (state.isAuthenticated) {
|
|
61
|
-
return state.user !== null && state.tokens !== null;
|
|
62
|
-
}
|
|
63
|
-
// If not authenticated, should not have user or tokens
|
|
64
|
-
return state.user === null && state.tokens === null;
|
|
65
|
-
};
|
|
66
|
-
exports.isValidAuthState = isValidAuthState;
|
|
67
|
-
const hasValidTokens = (state) => {
|
|
68
|
-
return state.tokens !== null &&
|
|
69
|
-
state.tokens.access.length > 0 &&
|
|
70
|
-
state.tokens.refresh.length > 0;
|
|
71
|
-
};
|
|
72
|
-
exports.hasValidTokens = hasValidTokens;
|
|
73
|
-
// Utility functions for working with auth state
|
|
74
|
-
const getDisplayName = (user) => {
|
|
75
|
-
if (!user)
|
|
76
|
-
return '';
|
|
77
|
-
return `${user.firstName} ${user.lastName}`.trim() || user.email;
|
|
78
|
-
};
|
|
79
|
-
exports.getDisplayName = getDisplayName;
|
|
80
|
-
const requiresVerification = (user) => {
|
|
81
|
-
return user !== null && !user.isVerified;
|
|
82
|
-
};
|
|
83
|
-
exports.requiresVerification = requiresVerification;
|
|
84
|
-
const requiresSignupCompletion = (user) => {
|
|
85
|
-
return user !== null && !user.isSignupCompleted;
|
|
86
|
-
};
|
|
87
|
-
exports.requiresSignupCompletion = requiresSignupCompletion;
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Token Storage Implementations
|
|
3
|
-
*
|
|
4
|
-
* Platform-specific token storage implementations that follow the simple TokenStorage interface.
|
|
5
|
-
* Pure functions with explicit error handling.
|
|
6
|
-
*/
|
|
7
|
-
import type { TokenStorage } from './auth-operations';
|
|
8
|
-
export interface SecureStoreInterface {
|
|
9
|
-
setItemAsync(key: string, value: string): Promise<void>;
|
|
10
|
-
getItemAsync(key: string): Promise<string | null>;
|
|
11
|
-
deleteItemAsync(key: string): Promise<void>;
|
|
12
|
-
isAvailableAsync?(): Promise<boolean>;
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Create web token storage using localStorage
|
|
16
|
-
*
|
|
17
|
-
* @returns TokenStorage implementation for web browsers
|
|
18
|
-
*/
|
|
19
|
-
export declare const createWebTokenStorage: () => TokenStorage;
|
|
20
|
-
/**
|
|
21
|
-
* Create mobile token storage using secure storage
|
|
22
|
-
*
|
|
23
|
-
* @param secureStore - Platform-specific secure storage implementation
|
|
24
|
-
* @returns TokenStorage implementation for mobile apps
|
|
25
|
-
*/
|
|
26
|
-
export declare const createMobileTokenStorage: (secureStore: SecureStoreInterface) => TokenStorage;
|
|
27
|
-
/**
|
|
28
|
-
* Create in-memory token storage (for testing)
|
|
29
|
-
*
|
|
30
|
-
* @returns TokenStorage implementation that stores tokens in memory
|
|
31
|
-
*/
|
|
32
|
-
export declare const createMemoryTokenStorage: () => TokenStorage;
|
|
33
|
-
/**
|
|
34
|
-
* Utility function to check if secure storage is available (for mobile)
|
|
35
|
-
*/
|
|
36
|
-
export declare const isSecureStorageAvailable: (secureStore: SecureStoreInterface) => Promise<boolean>;
|
|
37
|
-
/**
|
|
38
|
-
* Migrate tokens from old storage format (if needed)
|
|
39
|
-
*
|
|
40
|
-
* This function can be used to migrate from the old AuthManager token storage
|
|
41
|
-
* to the new simplified format.
|
|
42
|
-
*/
|
|
43
|
-
export declare const migrateTokensFromLegacyStorage: (newStorage: TokenStorage) => Promise<boolean>;
|
|
44
|
-
//# sourceMappingURL=token-storage.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"token-storage.d.ts","sourceRoot":"","sources":["../../../src/auth/functional/token-storage.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAa,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAGjE,MAAM,WAAW,oBAAoB;IACnC,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACxD,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IAClD,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5C,gBAAgB,CAAC,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;CACvC;AAKD;;;;GAIG;AACH,eAAO,MAAM,qBAAqB,QAAO,YAwCvC,CAAC;AAEH;;;;;GAKG;AACH,eAAO,MAAM,wBAAwB,GAAI,aAAa,oBAAoB,KAAG,YA2C3E,CAAC;AAEH;;;;GAIG;AACH,eAAO,MAAM,wBAAwB,QAAO,YAgB3C,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,wBAAwB,GAAU,aAAa,oBAAoB,KAAG,OAAO,CAAC,OAAO,CAkBjG,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,8BAA8B,GACzC,YAAY,YAAY,KACvB,OAAO,CAAC,OAAO,CA2BjB,CAAC"}
|
|
@@ -1,178 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Token Storage Implementations
|
|
4
|
-
*
|
|
5
|
-
* Platform-specific token storage implementations that follow the simple TokenStorage interface.
|
|
6
|
-
* Pure functions with explicit error handling.
|
|
7
|
-
*/
|
|
8
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.migrateTokensFromLegacyStorage = exports.isSecureStorageAvailable = exports.createMemoryTokenStorage = exports.createMobileTokenStorage = exports.createWebTokenStorage = void 0;
|
|
10
|
-
// Storage keys
|
|
11
|
-
const TOKENS_STORAGE_KEY = 'auth_tokens';
|
|
12
|
-
/**
|
|
13
|
-
* Create web token storage using localStorage
|
|
14
|
-
*
|
|
15
|
-
* @returns TokenStorage implementation for web browsers
|
|
16
|
-
*/
|
|
17
|
-
const createWebTokenStorage = () => ({
|
|
18
|
-
async storeTokens(tokens) {
|
|
19
|
-
try {
|
|
20
|
-
const tokenData = JSON.stringify(tokens);
|
|
21
|
-
localStorage.setItem(TOKENS_STORAGE_KEY, tokenData);
|
|
22
|
-
}
|
|
23
|
-
catch (error) {
|
|
24
|
-
throw new Error(`Failed to store tokens: ${error}`);
|
|
25
|
-
}
|
|
26
|
-
},
|
|
27
|
-
async retrieveTokens() {
|
|
28
|
-
try {
|
|
29
|
-
const stored = localStorage.getItem(TOKENS_STORAGE_KEY);
|
|
30
|
-
if (!stored) {
|
|
31
|
-
return null;
|
|
32
|
-
}
|
|
33
|
-
const tokens = JSON.parse(stored);
|
|
34
|
-
// Validate token structure
|
|
35
|
-
if (!tokens.access || !tokens.refresh) {
|
|
36
|
-
throw new Error('Invalid token structure');
|
|
37
|
-
}
|
|
38
|
-
return tokens;
|
|
39
|
-
}
|
|
40
|
-
catch (error) {
|
|
41
|
-
// If parsing fails, clear corrupted data
|
|
42
|
-
localStorage.removeItem(TOKENS_STORAGE_KEY);
|
|
43
|
-
return null;
|
|
44
|
-
}
|
|
45
|
-
},
|
|
46
|
-
async clearTokens() {
|
|
47
|
-
try {
|
|
48
|
-
localStorage.removeItem(TOKENS_STORAGE_KEY);
|
|
49
|
-
}
|
|
50
|
-
catch (error) {
|
|
51
|
-
// localStorage.removeItem doesn't typically throw, but handle just in case
|
|
52
|
-
console.warn('Failed to clear tokens from localStorage:', error);
|
|
53
|
-
}
|
|
54
|
-
},
|
|
55
|
-
});
|
|
56
|
-
exports.createWebTokenStorage = createWebTokenStorage;
|
|
57
|
-
/**
|
|
58
|
-
* Create mobile token storage using secure storage
|
|
59
|
-
*
|
|
60
|
-
* @param secureStore - Platform-specific secure storage implementation
|
|
61
|
-
* @returns TokenStorage implementation for mobile apps
|
|
62
|
-
*/
|
|
63
|
-
const createMobileTokenStorage = (secureStore) => ({
|
|
64
|
-
async storeTokens(tokens) {
|
|
65
|
-
try {
|
|
66
|
-
const tokenData = JSON.stringify(tokens);
|
|
67
|
-
await secureStore.setItemAsync(TOKENS_STORAGE_KEY, tokenData);
|
|
68
|
-
}
|
|
69
|
-
catch (error) {
|
|
70
|
-
throw new Error(`Failed to store tokens in secure storage: ${error}`);
|
|
71
|
-
}
|
|
72
|
-
},
|
|
73
|
-
async retrieveTokens() {
|
|
74
|
-
try {
|
|
75
|
-
const stored = await secureStore.getItemAsync(TOKENS_STORAGE_KEY);
|
|
76
|
-
if (!stored) {
|
|
77
|
-
return null;
|
|
78
|
-
}
|
|
79
|
-
const tokens = JSON.parse(stored);
|
|
80
|
-
// Validate token structure
|
|
81
|
-
if (!tokens.access || !tokens.refresh) {
|
|
82
|
-
throw new Error('Invalid token structure');
|
|
83
|
-
}
|
|
84
|
-
return tokens;
|
|
85
|
-
}
|
|
86
|
-
catch (error) {
|
|
87
|
-
// If parsing fails, clear corrupted data
|
|
88
|
-
try {
|
|
89
|
-
await secureStore.deleteItemAsync(TOKENS_STORAGE_KEY);
|
|
90
|
-
}
|
|
91
|
-
catch (clearError) {
|
|
92
|
-
console.warn('Failed to clear corrupted tokens:', clearError);
|
|
93
|
-
}
|
|
94
|
-
return null;
|
|
95
|
-
}
|
|
96
|
-
},
|
|
97
|
-
async clearTokens() {
|
|
98
|
-
try {
|
|
99
|
-
await secureStore.deleteItemAsync(TOKENS_STORAGE_KEY);
|
|
100
|
-
}
|
|
101
|
-
catch (error) {
|
|
102
|
-
console.warn('Failed to clear tokens from secure storage:', error);
|
|
103
|
-
}
|
|
104
|
-
},
|
|
105
|
-
});
|
|
106
|
-
exports.createMobileTokenStorage = createMobileTokenStorage;
|
|
107
|
-
/**
|
|
108
|
-
* Create in-memory token storage (for testing)
|
|
109
|
-
*
|
|
110
|
-
* @returns TokenStorage implementation that stores tokens in memory
|
|
111
|
-
*/
|
|
112
|
-
const createMemoryTokenStorage = () => {
|
|
113
|
-
let storedTokens = null;
|
|
114
|
-
return {
|
|
115
|
-
async storeTokens(tokens) {
|
|
116
|
-
storedTokens = { ...tokens };
|
|
117
|
-
},
|
|
118
|
-
async retrieveTokens() {
|
|
119
|
-
return storedTokens ? { ...storedTokens } : null;
|
|
120
|
-
},
|
|
121
|
-
async clearTokens() {
|
|
122
|
-
storedTokens = null;
|
|
123
|
-
},
|
|
124
|
-
};
|
|
125
|
-
};
|
|
126
|
-
exports.createMemoryTokenStorage = createMemoryTokenStorage;
|
|
127
|
-
/**
|
|
128
|
-
* Utility function to check if secure storage is available (for mobile)
|
|
129
|
-
*/
|
|
130
|
-
const isSecureStorageAvailable = async (secureStore) => {
|
|
131
|
-
try {
|
|
132
|
-
if (secureStore.isAvailableAsync) {
|
|
133
|
-
return await secureStore.isAvailableAsync();
|
|
134
|
-
}
|
|
135
|
-
// Test availability by trying to set/get a test value
|
|
136
|
-
const testKey = '_secure_storage_test';
|
|
137
|
-
const testValue = 'test';
|
|
138
|
-
await secureStore.setItemAsync(testKey, testValue);
|
|
139
|
-
const retrieved = await secureStore.getItemAsync(testKey);
|
|
140
|
-
await secureStore.deleteItemAsync(testKey);
|
|
141
|
-
return retrieved === testValue;
|
|
142
|
-
}
|
|
143
|
-
catch (error) {
|
|
144
|
-
return false;
|
|
145
|
-
}
|
|
146
|
-
};
|
|
147
|
-
exports.isSecureStorageAvailable = isSecureStorageAvailable;
|
|
148
|
-
/**
|
|
149
|
-
* Migrate tokens from old storage format (if needed)
|
|
150
|
-
*
|
|
151
|
-
* This function can be used to migrate from the old AuthManager token storage
|
|
152
|
-
* to the new simplified format.
|
|
153
|
-
*/
|
|
154
|
-
const migrateTokensFromLegacyStorage = async (newStorage) => {
|
|
155
|
-
try {
|
|
156
|
-
// Try to find tokens in old locations
|
|
157
|
-
const legacyAccessToken = localStorage.getItem('access_token');
|
|
158
|
-
const legacyRefreshToken = localStorage.getItem('refresh_token');
|
|
159
|
-
if (legacyAccessToken && legacyRefreshToken) {
|
|
160
|
-
const tokens = {
|
|
161
|
-
access: legacyAccessToken,
|
|
162
|
-
refresh: legacyRefreshToken,
|
|
163
|
-
};
|
|
164
|
-
// Store in new format
|
|
165
|
-
await newStorage.storeTokens(tokens);
|
|
166
|
-
// Clean up old storage
|
|
167
|
-
localStorage.removeItem('access_token');
|
|
168
|
-
localStorage.removeItem('refresh_token');
|
|
169
|
-
return true;
|
|
170
|
-
}
|
|
171
|
-
return false;
|
|
172
|
-
}
|
|
173
|
-
catch (error) {
|
|
174
|
-
console.warn('Failed to migrate legacy tokens:', error);
|
|
175
|
-
return false;
|
|
176
|
-
}
|
|
177
|
-
};
|
|
178
|
-
exports.migrateTokensFromLegacyStorage = migrateTokensFromLegacyStorage;
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* React Authentication Provider
|
|
3
|
-
*
|
|
4
|
-
* Pure functional authentication with React state management.
|
|
5
|
-
* Uses the pure auth functions with React context for state management.
|
|
6
|
-
*/
|
|
7
|
-
import React from 'react';
|
|
8
|
-
import type { AuthState } from '../functional/auth-state';
|
|
9
|
-
import type { AuthDependencies, SigninPayload, SignupPayload } from '../functional/auth-operations';
|
|
10
|
-
interface AuthContextValue {
|
|
11
|
-
state: AuthState;
|
|
12
|
-
signin: (credentials: SigninPayload) => Promise<void>;
|
|
13
|
-
signout: () => Promise<void>;
|
|
14
|
-
signup: (userData: SignupPayload) => Promise<{
|
|
15
|
-
success: boolean;
|
|
16
|
-
message?: string;
|
|
17
|
-
}>;
|
|
18
|
-
refreshTokens: () => Promise<void>;
|
|
19
|
-
verifyEmail: (token: string) => Promise<{
|
|
20
|
-
success: boolean;
|
|
21
|
-
message?: string;
|
|
22
|
-
}>;
|
|
23
|
-
resendVerificationCode: () => Promise<{
|
|
24
|
-
success: boolean;
|
|
25
|
-
message?: string;
|
|
26
|
-
}>;
|
|
27
|
-
clearError: () => void;
|
|
28
|
-
validateSigninCredentials: (credentials: SigninPayload) => string[];
|
|
29
|
-
validateSignupData: (userData: SignupPayload) => string[];
|
|
30
|
-
}
|
|
31
|
-
interface AuthProviderProps {
|
|
32
|
-
children: React.ReactNode;
|
|
33
|
-
dependencies: AuthDependencies;
|
|
34
|
-
autoInitialize?: boolean;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Authentication Provider Component
|
|
38
|
-
*
|
|
39
|
-
* Manages authentication state using pure functions and React state.
|
|
40
|
-
* Provides a clean, predictable interface for authentication operations.
|
|
41
|
-
*/
|
|
42
|
-
export declare const AuthProvider: React.FC<AuthProviderProps>;
|
|
43
|
-
/**
|
|
44
|
-
* Hook to use authentication context
|
|
45
|
-
*
|
|
46
|
-
* @returns Authentication context value
|
|
47
|
-
* @throws Error if used outside AuthProvider
|
|
48
|
-
*/
|
|
49
|
-
export declare const useAuth: () => AuthContextValue;
|
|
50
|
-
/**
|
|
51
|
-
* Hook to get authentication state only (for components that only need to read state)
|
|
52
|
-
*/
|
|
53
|
-
export declare const useAuthState: () => AuthState;
|
|
54
|
-
/**
|
|
55
|
-
* Hook for authentication actions only (for components that only need actions)
|
|
56
|
-
*/
|
|
57
|
-
export declare const useAuthActions: () => {
|
|
58
|
-
signin: (credentials: SigninPayload) => Promise<void>;
|
|
59
|
-
signout: () => Promise<void>;
|
|
60
|
-
signup: (userData: SignupPayload) => Promise<{
|
|
61
|
-
success: boolean;
|
|
62
|
-
message?: string;
|
|
63
|
-
}>;
|
|
64
|
-
refreshTokens: () => Promise<void>;
|
|
65
|
-
verifyEmail: (token: string) => Promise<{
|
|
66
|
-
success: boolean;
|
|
67
|
-
message?: string;
|
|
68
|
-
}>;
|
|
69
|
-
resendVerificationCode: () => Promise<{
|
|
70
|
-
success: boolean;
|
|
71
|
-
message?: string;
|
|
72
|
-
}>;
|
|
73
|
-
clearError: () => void;
|
|
74
|
-
validateSigninCredentials: (credentials: SigninPayload) => string[];
|
|
75
|
-
validateSignupData: (userData: SignupPayload) => string[];
|
|
76
|
-
};
|
|
77
|
-
/**
|
|
78
|
-
* Higher-order component for components that require authentication
|
|
79
|
-
*/
|
|
80
|
-
export declare const withAuth: <P extends object>(Component: React.ComponentType<P>) => React.ComponentType<P>;
|
|
81
|
-
/**
|
|
82
|
-
* Component for conditional rendering based on authentication state
|
|
83
|
-
*/
|
|
84
|
-
interface AuthGuardProps {
|
|
85
|
-
children: React.ReactNode;
|
|
86
|
-
fallback?: React.ReactNode;
|
|
87
|
-
requireAuth?: boolean;
|
|
88
|
-
requireVerification?: boolean;
|
|
89
|
-
}
|
|
90
|
-
export declare const AuthGuard: React.FC<AuthGuardProps>;
|
|
91
|
-
export {};
|
|
92
|
-
//# sourceMappingURL=AuthProvider.d.ts.map
|