@beyondcorp/beyond-ui 1.0.28 → 1.0.30
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/contexts/AuthContext.d.ts +12 -0
- package/dist/contexts/AuthContext.js +153 -0
- package/dist/contexts/AuthContext.js.map +1 -0
- package/dist/services/authService.d.ts +36 -0
- package/dist/services/authService.js +143 -0
- package/dist/services/authService.js.map +1 -0
- package/package.json +3 -1
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { AuthContextType } from '../types/auth';
|
|
3
|
+
/**
|
|
4
|
+
* AuthProvider component that manages authentication state
|
|
5
|
+
*/
|
|
6
|
+
export declare const AuthProvider: React.FC<{
|
|
7
|
+
children: React.ReactNode;
|
|
8
|
+
}>;
|
|
9
|
+
/**
|
|
10
|
+
* Hook to use authentication context
|
|
11
|
+
*/
|
|
12
|
+
export declare const useAuth: () => AuthContextType;
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { jsx } from 'react/jsx-runtime';
|
|
2
|
+
import { createContext, useReducer, useEffect, useCallback, useContext } from 'react';
|
|
3
|
+
import { authService } from '../services/authService.js';
|
|
4
|
+
|
|
5
|
+
const authReducer = (state, action) => {
|
|
6
|
+
switch (action.type) {
|
|
7
|
+
case 'AUTH_START':
|
|
8
|
+
return { ...state, isLoading: true, error: null };
|
|
9
|
+
case 'AUTH_SUCCESS':
|
|
10
|
+
return {
|
|
11
|
+
...state,
|
|
12
|
+
user: action.payload,
|
|
13
|
+
isAuthenticated: true,
|
|
14
|
+
isLoading: false,
|
|
15
|
+
error: null,
|
|
16
|
+
};
|
|
17
|
+
case 'AUTH_ERROR':
|
|
18
|
+
return {
|
|
19
|
+
...state,
|
|
20
|
+
user: null,
|
|
21
|
+
isAuthenticated: false,
|
|
22
|
+
isLoading: false,
|
|
23
|
+
error: action.payload,
|
|
24
|
+
};
|
|
25
|
+
case 'AUTH_LOGOUT':
|
|
26
|
+
return {
|
|
27
|
+
...state,
|
|
28
|
+
user: null,
|
|
29
|
+
isAuthenticated: false,
|
|
30
|
+
isLoading: false,
|
|
31
|
+
error: null,
|
|
32
|
+
};
|
|
33
|
+
case 'CLEAR_ERROR':
|
|
34
|
+
return { ...state, error: null };
|
|
35
|
+
case 'SET_LOADING':
|
|
36
|
+
return { ...state, isLoading: action.payload };
|
|
37
|
+
default:
|
|
38
|
+
return state;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
const initialState = {
|
|
42
|
+
user: null,
|
|
43
|
+
isAuthenticated: false,
|
|
44
|
+
isLoading: true,
|
|
45
|
+
error: null,
|
|
46
|
+
};
|
|
47
|
+
const AuthContext = createContext(undefined);
|
|
48
|
+
/**
|
|
49
|
+
* AuthProvider component that manages authentication state
|
|
50
|
+
*/
|
|
51
|
+
const AuthProvider = ({ children }) => {
|
|
52
|
+
const [state, dispatch] = useReducer(authReducer, initialState);
|
|
53
|
+
// Initialize auth state on mount
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
const initializeAuth = () => {
|
|
56
|
+
try {
|
|
57
|
+
const user = authService.getCurrentUser();
|
|
58
|
+
const isAuthenticated = authService.isAuthenticated();
|
|
59
|
+
if (user && isAuthenticated) {
|
|
60
|
+
dispatch({ type: 'AUTH_SUCCESS', payload: user });
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
dispatch({ type: 'SET_LOADING', payload: false });
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
console.error('Failed to initialize auth:', error);
|
|
68
|
+
dispatch({ type: 'SET_LOADING', payload: false });
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
initializeAuth();
|
|
72
|
+
}, []);
|
|
73
|
+
/**
|
|
74
|
+
* Login user with credentials
|
|
75
|
+
*/
|
|
76
|
+
const login = useCallback(async (credentials) => {
|
|
77
|
+
dispatch({ type: 'AUTH_START' });
|
|
78
|
+
try {
|
|
79
|
+
const { user } = await authService.login(credentials);
|
|
80
|
+
dispatch({ type: 'AUTH_SUCCESS', payload: user });
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
const message = error instanceof Error ? error.message : 'Login failed';
|
|
84
|
+
dispatch({ type: 'AUTH_ERROR', payload: message });
|
|
85
|
+
throw error;
|
|
86
|
+
}
|
|
87
|
+
}, []);
|
|
88
|
+
/**
|
|
89
|
+
* Register new user
|
|
90
|
+
*/
|
|
91
|
+
const signup = useCallback(async (data) => {
|
|
92
|
+
dispatch({ type: 'AUTH_START' });
|
|
93
|
+
try {
|
|
94
|
+
const { user } = await authService.signup(data);
|
|
95
|
+
dispatch({ type: 'AUTH_SUCCESS', payload: user });
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
const message = error instanceof Error ? error.message : 'Signup failed';
|
|
99
|
+
dispatch({ type: 'AUTH_ERROR', payload: message });
|
|
100
|
+
throw error;
|
|
101
|
+
}
|
|
102
|
+
}, []);
|
|
103
|
+
/**
|
|
104
|
+
* Logout user
|
|
105
|
+
*/
|
|
106
|
+
const logout = useCallback(() => {
|
|
107
|
+
authService.logout();
|
|
108
|
+
dispatch({ type: 'AUTH_LOGOUT' });
|
|
109
|
+
}, []);
|
|
110
|
+
/**
|
|
111
|
+
* Clear authentication error
|
|
112
|
+
*/
|
|
113
|
+
const clearError = useCallback(() => {
|
|
114
|
+
dispatch({ type: 'CLEAR_ERROR' });
|
|
115
|
+
}, []);
|
|
116
|
+
/**
|
|
117
|
+
* Refresh authentication token
|
|
118
|
+
*/
|
|
119
|
+
const refreshToken = useCallback(async () => {
|
|
120
|
+
try {
|
|
121
|
+
await authService.refreshToken();
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
console.error('Token refresh failed:', error);
|
|
125
|
+
logout();
|
|
126
|
+
}
|
|
127
|
+
}, [logout]);
|
|
128
|
+
const value = {
|
|
129
|
+
user: state.user,
|
|
130
|
+
isAuthenticated: state.isAuthenticated,
|
|
131
|
+
isLoading: state.isLoading,
|
|
132
|
+
error: state.error,
|
|
133
|
+
login,
|
|
134
|
+
signup,
|
|
135
|
+
logout,
|
|
136
|
+
clearError,
|
|
137
|
+
refreshToken,
|
|
138
|
+
};
|
|
139
|
+
return jsx(AuthContext.Provider, { value: value, children: children });
|
|
140
|
+
};
|
|
141
|
+
/**
|
|
142
|
+
* Hook to use authentication context
|
|
143
|
+
*/
|
|
144
|
+
const useAuth = () => {
|
|
145
|
+
const context = useContext(AuthContext);
|
|
146
|
+
if (context === undefined) {
|
|
147
|
+
throw new Error('useAuth must be used within an AuthProvider');
|
|
148
|
+
}
|
|
149
|
+
return context;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
export { AuthProvider, useAuth };
|
|
153
|
+
//# sourceMappingURL=AuthContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AuthContext.js","sources":["../../src/contexts/AuthContext.tsx"],"sourcesContent":["import React, { createContext, useContext, useReducer, useEffect, useCallback } from 'react';\nimport type { AuthState, AuthContextType, LoginCredentials, SignupData, User } from '../types/auth';\nimport { authService } from '../services/authService';\n\n// Auth reducer\ntype AuthAction =\n | { type: 'AUTH_START' }\n | { type: 'AUTH_SUCCESS'; payload: User }\n | { type: 'AUTH_ERROR'; payload: string }\n | { type: 'AUTH_LOGOUT' }\n | { type: 'CLEAR_ERROR' }\n | { type: 'SET_LOADING'; payload: boolean };\n\nconst authReducer = (state: AuthState, action: AuthAction): AuthState => {\n switch (action.type) {\n case 'AUTH_START':\n return { ...state, isLoading: true, error: null };\n case 'AUTH_SUCCESS':\n return {\n ...state,\n user: action.payload,\n isAuthenticated: true,\n isLoading: false,\n error: null,\n };\n case 'AUTH_ERROR':\n return {\n ...state,\n user: null,\n isAuthenticated: false,\n isLoading: false,\n error: action.payload,\n };\n case 'AUTH_LOGOUT':\n return {\n ...state,\n user: null,\n isAuthenticated: false,\n isLoading: false,\n error: null,\n };\n case 'CLEAR_ERROR':\n return { ...state, error: null };\n case 'SET_LOADING':\n return { ...state, isLoading: action.payload };\n default:\n return state;\n }\n};\n\nconst initialState: AuthState = {\n user: null,\n isAuthenticated: false,\n isLoading: true,\n error: null,\n};\n\nconst AuthContext = createContext<AuthContextType | undefined>(undefined);\n\n/**\n * AuthProvider component that manages authentication state\n */\nexport const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {\n const [state, dispatch] = useReducer(authReducer, initialState);\n\n // Initialize auth state on mount\n useEffect(() => {\n const initializeAuth = () => {\n try {\n const user = authService.getCurrentUser();\n const isAuthenticated = authService.isAuthenticated();\n\n if (user && isAuthenticated) {\n dispatch({ type: 'AUTH_SUCCESS', payload: user });\n } else {\n dispatch({ type: 'SET_LOADING', payload: false });\n }\n } catch (error) {\n console.error('Failed to initialize auth:', error);\n dispatch({ type: 'SET_LOADING', payload: false });\n }\n };\n\n initializeAuth();\n }, []);\n\n /**\n * Login user with credentials\n */\n const login = useCallback(async (credentials: LoginCredentials) => {\n dispatch({ type: 'AUTH_START' });\n \n try {\n const { user } = await authService.login(credentials);\n dispatch({ type: 'AUTH_SUCCESS', payload: user });\n } catch (error) {\n const message = error instanceof Error ? error.message : 'Login failed';\n dispatch({ type: 'AUTH_ERROR', payload: message });\n throw error;\n }\n }, []);\n\n /**\n * Register new user\n */\n const signup = useCallback(async (data: SignupData) => {\n dispatch({ type: 'AUTH_START' });\n \n try {\n const { user } = await authService.signup(data);\n dispatch({ type: 'AUTH_SUCCESS', payload: user });\n } catch (error) {\n const message = error instanceof Error ? error.message : 'Signup failed';\n dispatch({ type: 'AUTH_ERROR', payload: message });\n throw error;\n }\n }, []);\n\n /**\n * Logout user\n */\n const logout = useCallback(() => {\n authService.logout();\n dispatch({ type: 'AUTH_LOGOUT' });\n }, []);\n\n /**\n * Clear authentication error\n */\n const clearError = useCallback(() => {\n dispatch({ type: 'CLEAR_ERROR' });\n }, []);\n\n /**\n * Refresh authentication token\n */\n const refreshToken = useCallback(async () => {\n try {\n await authService.refreshToken();\n } catch (error) {\n console.error('Token refresh failed:', error);\n logout();\n }\n }, [logout]);\n\n const value: AuthContextType = {\n user: state.user,\n isAuthenticated: state.isAuthenticated,\n isLoading: state.isLoading,\n error: state.error,\n login,\n signup,\n logout,\n clearError,\n refreshToken,\n };\n\n return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;\n};\n\n/**\n * Hook to use authentication context\n */\nexport const useAuth = (): AuthContextType => {\n const context = useContext(AuthContext);\n if (context === undefined) {\n throw new Error('useAuth must be used within an AuthProvider');\n }\n return context;\n};"],"names":["_jsx"],"mappings":";;;;AAaA,MAAM,WAAW,GAAG,CAAC,KAAgB,EAAE,MAAkB,KAAe;AACtE,IAAA,QAAQ,MAAM,CAAC,IAAI;AACjB,QAAA,KAAK,YAAY;AACf,YAAA,OAAO,EAAE,GAAG,KAAK,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE;AACnD,QAAA,KAAK,cAAc;YACjB,OAAO;AACL,gBAAA,GAAG,KAAK;gBACR,IAAI,EAAE,MAAM,CAAC,OAAO;AACpB,gBAAA,eAAe,EAAE,IAAI;AACrB,gBAAA,SAAS,EAAE,KAAK;AAChB,gBAAA,KAAK,EAAE,IAAI;aACZ;AACH,QAAA,KAAK,YAAY;YACf,OAAO;AACL,gBAAA,GAAG,KAAK;AACR,gBAAA,IAAI,EAAE,IAAI;AACV,gBAAA,eAAe,EAAE,KAAK;AACtB,gBAAA,SAAS,EAAE,KAAK;gBAChB,KAAK,EAAE,MAAM,CAAC,OAAO;aACtB;AACH,QAAA,KAAK,aAAa;YAChB,OAAO;AACL,gBAAA,GAAG,KAAK;AACR,gBAAA,IAAI,EAAE,IAAI;AACV,gBAAA,eAAe,EAAE,KAAK;AACtB,gBAAA,SAAS,EAAE,KAAK;AAChB,gBAAA,KAAK,EAAE,IAAI;aACZ;AACH,QAAA,KAAK,aAAa;YAChB,OAAO,EAAE,GAAG,KAAK,EAAE,KAAK,EAAE,IA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AAEG;AACH,IAAA,MAAM,UAAU,GAAG,WAAW,CAAC,MAAK;AAClC,QAAA,QAAQ,CAAC,EAAE,IAAI,EAAE,aAAa,EAAE,CAAC;IACnC,CAAC,EAAE,EAAE,CAAC;AAEN;;AAEG;AACH,IAAA,MAAM,YAAY,GAAG,WAAW,CAAC,YAAW;AAC1C,QAAA,IAAI;AACF,YAAA,MAAM,WAAW,CAAC,YAAY,EAAE;QAClC;QAAE,OAAO,KAAK,EAAE;AACd,YAAA,OAAO,CAAC,KAAK,CAAC,uBAAuB,EAAE,KAAK,CAAC;AAC7C,YAAA,MAAM,EAAE;QACV;AACF,IAAA,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC;AAEZ,IAAA,MAAM,KAAK,GAAoB;QAC7B,IAAI,EAAE,KAAK,CAAC,IAAI;QAChB,eAAe,EAAE,KAAK,CAAC,eAAe;QACtC,SAAS,EAAE,KAAK,CAAC,SAAS;QAC1B,KAAK,EAAE,KAAK,CAAC,KAAK;QAClB,KAAK;QACL,MAAM;QACN,MAAM;QACN,UAAU;QACV,YAAY;KACb;IAED,OAAOA,GAAA,CAAC,WAAW,CAAC,QAAQ,EAAA,EAAC,KAAK,EAAE,KAAK,EAAA,QAAA,EAAG,QAAQ,EAAA,CAAwB;AAC9E;AAEA;;AAEG;AACI,MAAM,OAAO,GAAG,MAAsB;AAC3C,IAAA,MAAM,OAAO,GAAG,UAAU,CAAC,WAAW,CAAC;AACvC,IAAA,IAAI,OAAO,KAAK,SAAS,EAAE;AACzB,QAAA,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC;IAChE;AACA,IAAA,OAAO,OAAO;AAChB;;;;"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { LoginCredentials, SignupData, AuthResponse, User } from '../types/auth';
|
|
2
|
+
declare class AuthService {
|
|
3
|
+
private readonly TOKEN_KEY;
|
|
4
|
+
private readonly REFRESH_TOKEN_KEY;
|
|
5
|
+
private readonly USER_KEY;
|
|
6
|
+
/**
|
|
7
|
+
* Simulate API login
|
|
8
|
+
*/
|
|
9
|
+
login(credentials: LoginCredentials): Promise<AuthResponse>;
|
|
10
|
+
/**
|
|
11
|
+
* Simulate API signup
|
|
12
|
+
*/
|
|
13
|
+
signup(data: SignupData): Promise<AuthResponse>;
|
|
14
|
+
/**
|
|
15
|
+
* Logout user and clear tokens
|
|
16
|
+
*/
|
|
17
|
+
logout(): void;
|
|
18
|
+
/**
|
|
19
|
+
* Get current user from storage
|
|
20
|
+
*/
|
|
21
|
+
getCurrentUser(): User | null;
|
|
22
|
+
/**
|
|
23
|
+
* Get current token
|
|
24
|
+
*/
|
|
25
|
+
getToken(): string | null;
|
|
26
|
+
/**
|
|
27
|
+
* Refresh authentication token
|
|
28
|
+
*/
|
|
29
|
+
refreshToken(): Promise<string>;
|
|
30
|
+
/**
|
|
31
|
+
* Check if user is authenticated
|
|
32
|
+
*/
|
|
33
|
+
isAuthenticated(): boolean;
|
|
34
|
+
}
|
|
35
|
+
export declare const authService: AuthService;
|
|
36
|
+
export {};
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import api from '../node_modules/js-cookie/dist/js.cookie.js';
|
|
2
|
+
|
|
3
|
+
// Mock API service - replace with actual API calls
|
|
4
|
+
class AuthService {
|
|
5
|
+
constructor() {
|
|
6
|
+
Object.defineProperty(this, "TOKEN_KEY", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
configurable: true,
|
|
9
|
+
writable: true,
|
|
10
|
+
value: 'auth_token'
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(this, "REFRESH_TOKEN_KEY", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
configurable: true,
|
|
15
|
+
writable: true,
|
|
16
|
+
value: 'refresh_token'
|
|
17
|
+
});
|
|
18
|
+
Object.defineProperty(this, "USER_KEY", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
configurable: true,
|
|
21
|
+
writable: true,
|
|
22
|
+
value: 'auth_user'
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Simulate API login
|
|
27
|
+
*/
|
|
28
|
+
async login(credentials) {
|
|
29
|
+
// Simulate API delay
|
|
30
|
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
31
|
+
// Mock validation - replace with actual API call
|
|
32
|
+
if (credentials.email === 'admin@example.com' && credentials.password === 'Password123!') {
|
|
33
|
+
const user = {
|
|
34
|
+
id: '1',
|
|
35
|
+
email: credentials.email,
|
|
36
|
+
name: 'John Doe',
|
|
37
|
+
role: 'admin',
|
|
38
|
+
avatar: 'https://images.pexels.com/photos/774909/pexels-photo-774909.jpeg?auto=compress&cs=tinysrgb&w=64',
|
|
39
|
+
createdAt: new Date().toISOString(),
|
|
40
|
+
lastLogin: new Date().toISOString(),
|
|
41
|
+
};
|
|
42
|
+
const token = 'mock_jwt_token_' + Date.now();
|
|
43
|
+
const refreshToken = 'mock_refresh_token_' + Date.now();
|
|
44
|
+
// Store tokens
|
|
45
|
+
const cookieOptions = {
|
|
46
|
+
expires: credentials.rememberMe ? 30 : undefined, // 30 days if remember me
|
|
47
|
+
secure: process.env.NODE_ENV === 'production',
|
|
48
|
+
sameSite: 'strict',
|
|
49
|
+
};
|
|
50
|
+
api.set(this.TOKEN_KEY, token, cookieOptions);
|
|
51
|
+
api.set(this.REFRESH_TOKEN_KEY, refreshToken, cookieOptions);
|
|
52
|
+
localStorage.setItem(this.USER_KEY, JSON.stringify(user));
|
|
53
|
+
return { user, token, refreshToken };
|
|
54
|
+
}
|
|
55
|
+
throw new Error('Invalid email or password');
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Simulate API signup
|
|
59
|
+
*/
|
|
60
|
+
async signup(data) {
|
|
61
|
+
// Simulate API delay
|
|
62
|
+
await new Promise(resolve => setTimeout(resolve, 1500));
|
|
63
|
+
// Mock email validation - replace with actual API call
|
|
64
|
+
if (data.email === 'existing@example.com') {
|
|
65
|
+
throw new Error('An account with this email already exists');
|
|
66
|
+
}
|
|
67
|
+
const user = {
|
|
68
|
+
id: Date.now().toString(),
|
|
69
|
+
email: data.email,
|
|
70
|
+
name: data.name,
|
|
71
|
+
role: 'user',
|
|
72
|
+
createdAt: new Date().toISOString(),
|
|
73
|
+
};
|
|
74
|
+
const token = 'mock_jwt_token_' + Date.now();
|
|
75
|
+
const refreshToken = 'mock_refresh_token_' + Date.now();
|
|
76
|
+
// Store tokens
|
|
77
|
+
const cookieOptions = {
|
|
78
|
+
expires: 7, // 7 days for new users
|
|
79
|
+
secure: process.env.NODE_ENV === 'production',
|
|
80
|
+
sameSite: 'strict',
|
|
81
|
+
};
|
|
82
|
+
api.set(this.TOKEN_KEY, token, cookieOptions);
|
|
83
|
+
api.set(this.REFRESH_TOKEN_KEY, refreshToken, cookieOptions);
|
|
84
|
+
localStorage.setItem(this.USER_KEY, JSON.stringify(user));
|
|
85
|
+
return { user, token, refreshToken };
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Logout user and clear tokens
|
|
89
|
+
*/
|
|
90
|
+
logout() {
|
|
91
|
+
api.remove(this.TOKEN_KEY);
|
|
92
|
+
api.remove(this.REFRESH_TOKEN_KEY);
|
|
93
|
+
localStorage.removeItem(this.USER_KEY);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Get current user from storage
|
|
97
|
+
*/
|
|
98
|
+
getCurrentUser() {
|
|
99
|
+
try {
|
|
100
|
+
const userStr = localStorage.getItem(this.USER_KEY);
|
|
101
|
+
return userStr ? JSON.parse(userStr) : null;
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Get current token
|
|
109
|
+
*/
|
|
110
|
+
getToken() {
|
|
111
|
+
return api.get(this.TOKEN_KEY) || null;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Refresh authentication token
|
|
115
|
+
*/
|
|
116
|
+
async refreshToken() {
|
|
117
|
+
const refreshToken = api.get(this.REFRESH_TOKEN_KEY);
|
|
118
|
+
if (!refreshToken) {
|
|
119
|
+
throw new Error('No refresh token available');
|
|
120
|
+
}
|
|
121
|
+
// Simulate API call
|
|
122
|
+
await new Promise(resolve => setTimeout(resolve, 500));
|
|
123
|
+
const newToken = 'mock_jwt_token_refreshed_' + Date.now();
|
|
124
|
+
api.set(this.TOKEN_KEY, newToken, {
|
|
125
|
+
expires: 1, // 1 day
|
|
126
|
+
secure: process.env.NODE_ENV === 'production',
|
|
127
|
+
sameSite: 'strict',
|
|
128
|
+
});
|
|
129
|
+
return newToken;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Check if user is authenticated
|
|
133
|
+
*/
|
|
134
|
+
isAuthenticated() {
|
|
135
|
+
const token = this.getToken();
|
|
136
|
+
const user = this.getCurrentUser();
|
|
137
|
+
return !!(token && user);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
const authService = new AuthService();
|
|
141
|
+
|
|
142
|
+
export { authService };
|
|
143
|
+
//# sourceMappingURL=authService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"authService.js","sources":["../../src/services/authService.ts"],"sourcesContent":["import Cookies from 'js-cookie';\nimport type { LoginCredentials, SignupData, AuthResponse, User } from '../types/auth';\n\n// Mock API service - replace with actual API calls\nclass AuthService {\n private readonly TOKEN_KEY = 'auth_token';\n private readonly REFRESH_TOKEN_KEY = 'refresh_token';\n private readonly USER_KEY = 'auth_user';\n\n /**\n * Simulate API login\n */\n async login(credentials: LoginCredentials): Promise<AuthResponse> {\n // Simulate API delay\n await new Promise(resolve => setTimeout(resolve, 1000));\n\n // Mock validation - replace with actual API call\n if (credentials.email === 'admin@example.com' && credentials.password === 'Password123!') {\n const user: User = {\n id: '1',\n email: credentials.email,\n name: 'John Doe',\n role: 'admin',\n avatar: 'https://images.pexels.com/photos/774909/pexels-photo-774909.jpeg?auto=compress&cs=tinysrgb&w=64',\n createdAt: new Date().toISOString(),\n lastLogin: new Date().toISOString(),\n };\n\n const token = 'mock_jwt_token_' + Date.now();\n const refreshToken = 'mock_refresh_token_' + Date.now();\n\n // Store tokens\n const cookieOptions = {\n expires: credentials.rememberMe ? 30 : undefined, // 30 days if remember me\n secure: process.env.NODE_ENV === 'production',\n sameSite: 'strict' as const,\n };\n\n Cookies.set(this.TOKEN_KEY, token, cookieOptions);\n Cookies.set(this.REFRESH_TOKEN_KEY, refreshToken, cookieOptions);\n localStorage.setItem(this.USER_KEY, JSON.stringify(user));\n\n return { user, token, refreshToken };\n }\n\n throw new Error('Invalid email or password');\n }\n\n /**\n * Simulate API signup\n */\n async signup(data: SignupData): Promise<AuthResponse> {\n // Simulate API delay\n await new Promise(resolve => setTimeout(resolve, 1500));\n\n // Mock email validation - replace with actual API call\n if (data.email === 'existing@example.com') {\n throw new Error('An account with this email already exists');\n }\n\n const user: User = {\n id: Date.now().toString(),\n email: data.email,\n name: data.name,\n role: 'user',\n createdAt: new Date().toISOString(),\n };\n\n const token = 'mock_jwt_token_' + Date.now();\n const refreshToken = 'mock_refresh_token_' + Date.now();\n\n // Store tokens\n const cookieOptions = {\n expires: 7, // 7 days for new users\n secure: process.env.NODE_ENV === 'production',\n sameSite: 'strict' as const,\n };\n\n Cookies.set(this.TOKEN_KEY, token, cookieOptions);\n Cookies.set(this.REFRESH_TOKEN_KEY, refreshToken, cookieOptions);\n localStorage.setItem(this.USER_KEY, JSON.stringify(user));\n\n return { user, token, refreshToken };\n }\n\n /**\n * Logout user and clear tokens\n */\n logout(): void {\n Cookies.remove(this.TOKEN_KEY);\n Cookies.remove(this.REFRESH_TOKEN_KEY);\n localStorage.removeItem(this.USER_KEY);\n }\n\n /**\n * Get current user from storage\n */\n getCurrentUser(): User | null {\n try {\n const userStr = localStorage.getItem(this.USER_KEY);\n return userStr ? JSON.parse(userStr) : null;\n } catch {\n return null;\n }\n }\n\n /**\n * Get current token\n */\n getToken(): string | null {\n return Cookies.get(this.TOKEN_KEY) || null;\n }\n\n /**\n * Refresh authentication token\n */\n async refreshToken(): Promise<string> {\n const refreshToken = Cookies.get(this.REFRESH_TOKEN_KEY);\n \n if (!refreshToken) {\n throw new Error('No refresh token available');\n }\n\n // Simulate API call\n await new Promise(resolve => setTimeout(resolve, 500));\n\n const newToken = 'mock_jwt_token_refreshed_' + Date.now();\n \n Cookies.set(this.TOKEN_KEY, newToken, {\n expires: 1, // 1 day\n secure: process.env.NODE_ENV === 'production',\n sameSite: 'strict',\n });\n\n return newToken;\n }\n\n /**\n * Check if user is authenticated\n */\n isAuthenticated(): boolean {\n const token = this.getToken();\n const user = this.getCurrentUser();\n return !!(token && user);\n }\n}\n\nexport const authService = new AuthService();"],"names":["Cookies"],"mappings":";;AAGA;AACA,MAAM,WAAW,CAAA;AAAjB,IAAA,WAAA,GAAA;AACmB,QAAA,MAAA,CAAA,cAAA,CAAA,IAAA,EAAA,WAAA,EAAA;;;;mBAAY;AAAa,SAAA,CAAA;AACzB,QAAA,MAAA,CAAA,cAAA,CAAA,IAAA,EAAA,mBAAA,EAAA;;;;mBAAoB;AAAgB,SAAA,CAAA;AACpC,QAAA,MAAA,CAAA,cAAA,CAAA,IAAA,EAAA,UAAA,EAAA;;;;mBAAW;AAAY,SAAA,CAAA;IA0I1C;AAxIE;;AAEG;IACH,MAAM,KAAK,CAAC,WAA6B,EAAA;;AAEvC,QAAA,MAAM,IAAI,OAAO,CAAC,OAAO,IAAI,UAAU,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;;AAGvD,QAAA,IAAI,WAAW,CAAC,KAAK,KAAK,mBAAmB,IAAI,WAAW,CAAC,QAAQ,KAAK,cAAc,EAAE;AACxF,YAAA,MAAM,IAAI,GAAS;AACjB,gBAAA,EAAE,EAAE,GAAG;gBACP,KAAK,EAAE,WAAW,CAAC,KAAK;AACxB,gBAAA,IAAI,EAAE,UAAU;AAChB,gBAAA,IAAI,EAAE,OAAO;AACb,gBAAA,MAAM,EAAE,iGAAiG;AACzG,gBA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IACF;AAEA;;AAEG;IACH,QAAQ,GAAA;QACN,OAAOA,GAAO,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,IAAI;IAC5C;AAEA;;AAEG;AACH,IAAA,MAAM,YAAY,GAAA;QAChB,MAAM,YAAY,GAAGA,GAAO,CAAC,GAAG,CAAC,IAAI,CAAC,iBAAiB,CAAC;QAExD,IAAI,CAAC,YAAY,EAAE;AACjB,YAAA,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC;QAC/C;;AAGA,QAAA,MAAM,IAAI,OAAO,CAAC,OAAO,IAAI,UAAU,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;QAEtD,MAAM,QAAQ,GAAG,2BAA2B,GAAG,IAAI,CAAC,GAAG,EAAE;QAEzDA,GAAO,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,EAAE,QAAQ,EAAE;YACpC,OAAO,EAAE,CAAC;AACV,YAAA,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY;AAC7C,YAAA,QAAQ,EAAE,QAAQ;AACnB,SAAA,CAAC;AAEF,QAAA,OAAO,QAAQ;IACjB;AAEA;;AAEG;IACH,eAAe,GAAA;AACb,QAAA,MAAM,KAAK,GAAG,IAAI,CAAC,QAAQ,EAAE;AAC7B,QAAA,MAAM,IAAI,GAAG,IAAI,CAAC,cAAc,EAAE;AAClC,QAAA,OAAO,CAAC,EAAE,KAAK,IAAI,IAAI,CAAC;IAC1B;AACD;AAEM,MAAM,WAAW,GAAG,IAAI,WAAW;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@beyondcorp/beyond-ui",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.30",
|
|
4
4
|
"description": "A comprehensive React UI component library built with TypeScript, TailwindCSS, and CVA",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
"dist/index.d.ts",
|
|
12
12
|
"dist/styles.css",
|
|
13
13
|
"dist/components/**/*",
|
|
14
|
+
"dist/contexts/**/*",
|
|
15
|
+
"dist/services/**/*",
|
|
14
16
|
"dist/hooks/**/*",
|
|
15
17
|
"dist/utils/**/*",
|
|
16
18
|
"dist/theme/**/*"
|