@beyondcorp/beyond-ui 1.0.28 → 1.0.29

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.
@@ -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,IAAI,EAAE;AAClC,QAAA,KAAK,aAAa;YAChB,OAAO,EAAE,GAAG,KAAK,EAAE,SAAS,EAAE,MAAM,CAAC,OAAO,EAAE;AAChD,QAAA;AACE,YAAA,OAAO,KAAK;;AAElB,CAAC;AAED,MAAM,YA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,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;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@beyondcorp/beyond-ui",
3
- "version": "1.0.28",
3
+ "version": "1.0.29",
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,7 @@
11
11
  "dist/index.d.ts",
12
12
  "dist/styles.css",
13
13
  "dist/components/**/*",
14
+ "dist/contexts/**/*",
14
15
  "dist/hooks/**/*",
15
16
  "dist/utils/**/*",
16
17
  "dist/theme/**/*"