@startsimpli/auth 0.1.0 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-CDNZRZ7Q.mjs +767 -0
- package/dist/chunk-CDNZRZ7Q.mjs.map +1 -0
- package/dist/chunk-S6J5FYQY.mjs +134 -0
- package/dist/chunk-S6J5FYQY.mjs.map +1 -0
- package/dist/chunk-TA46ASDJ.mjs +37 -0
- package/dist/chunk-TA46ASDJ.mjs.map +1 -0
- package/dist/client/index.d.mts +175 -0
- package/dist/client/index.d.ts +175 -0
- package/dist/client/index.js +858 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/index.mjs +5 -0
- package/dist/client/index.mjs.map +1 -0
- package/dist/index.d.mts +68 -0
- package/dist/index.d.ts +68 -0
- package/dist/index.js +971 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +5 -0
- package/dist/index.mjs.map +1 -0
- package/dist/server/index.d.mts +83 -0
- package/dist/server/index.d.ts +83 -0
- package/dist/server/index.js +242 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/index.mjs +191 -0
- package/dist/server/index.mjs.map +1 -0
- package/dist/types/index.d.mts +209 -0
- package/dist/types/index.d.ts +209 -0
- package/dist/types/index.js +43 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/index.mjs +3 -0
- package/dist/types/index.mjs.map +1 -0
- package/package.json +50 -18
- package/src/__tests__/auth-client.test.ts +125 -0
- package/src/__tests__/auth-fetch.test.ts +128 -0
- package/src/__tests__/token-storage.test.ts +61 -0
- package/src/__tests__/validation.test.ts +60 -0
- package/src/client/auth-client.ts +11 -1
- package/src/client/functions.ts +83 -14
- package/src/types/index.ts +100 -0
- package/src/utils/validation.ts +190 -0
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { AuthConfig, Session, AuthUser as AuthUser$1, AuthState, CompanyRole } from '../types/index.js';
|
|
2
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
3
|
+
import { ReactNode } from 'react';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Client-side authentication manager
|
|
7
|
+
* Handles JWT tokens, login/logout, and token refresh
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
declare class AuthClient {
|
|
11
|
+
private config;
|
|
12
|
+
private session;
|
|
13
|
+
private refreshTimer;
|
|
14
|
+
private isRefreshing;
|
|
15
|
+
private refreshPromise;
|
|
16
|
+
constructor(config: AuthConfig);
|
|
17
|
+
/**
|
|
18
|
+
* Login with email and password
|
|
19
|
+
*/
|
|
20
|
+
login(email: string, password: string): Promise<Session>;
|
|
21
|
+
/**
|
|
22
|
+
* Logout and clear session
|
|
23
|
+
*/
|
|
24
|
+
logout(): Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* Refresh access token using refresh token cookie
|
|
27
|
+
*/
|
|
28
|
+
refreshToken(): Promise<string>;
|
|
29
|
+
private performTokenRefresh;
|
|
30
|
+
/**
|
|
31
|
+
* Get current user data from backend
|
|
32
|
+
*/
|
|
33
|
+
getCurrentUser(): Promise<AuthUser$1>;
|
|
34
|
+
/**
|
|
35
|
+
* Get current session
|
|
36
|
+
*/
|
|
37
|
+
getSession(): Session | null;
|
|
38
|
+
/**
|
|
39
|
+
* Set session (for SSR/hydration)
|
|
40
|
+
*/
|
|
41
|
+
setSession(session: Session): void;
|
|
42
|
+
/**
|
|
43
|
+
* Get auth headers for API requests
|
|
44
|
+
*/
|
|
45
|
+
getAuthHeaders(): Record<string, string>;
|
|
46
|
+
/**
|
|
47
|
+
* Get valid access token (refreshes if needed)
|
|
48
|
+
*/
|
|
49
|
+
getAccessToken(): Promise<string | null>;
|
|
50
|
+
/**
|
|
51
|
+
* Start automatic token refresh timer
|
|
52
|
+
*/
|
|
53
|
+
private startRefreshTimer;
|
|
54
|
+
/**
|
|
55
|
+
* Clear session and stop refresh timer
|
|
56
|
+
*/
|
|
57
|
+
private clearSession;
|
|
58
|
+
/**
|
|
59
|
+
* Cleanup resources
|
|
60
|
+
*/
|
|
61
|
+
destroy(): void;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
interface AuthContextValue extends AuthState {
|
|
65
|
+
login: (email: string, password: string) => Promise<void>;
|
|
66
|
+
logout: () => Promise<void>;
|
|
67
|
+
refreshUser: () => Promise<void>;
|
|
68
|
+
getAccessToken: () => Promise<string | null>;
|
|
69
|
+
}
|
|
70
|
+
interface AuthProviderProps {
|
|
71
|
+
children: ReactNode;
|
|
72
|
+
config: AuthConfig;
|
|
73
|
+
initialSession?: Session | null;
|
|
74
|
+
}
|
|
75
|
+
declare function AuthProvider({ children, config, initialSession, }: AuthProviderProps): react_jsx_runtime.JSX.Element;
|
|
76
|
+
/**
|
|
77
|
+
* Hook to access auth context
|
|
78
|
+
*/
|
|
79
|
+
declare function useAuthContext(): AuthContextValue;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* React hook for authentication
|
|
83
|
+
*/
|
|
84
|
+
|
|
85
|
+
interface UseAuthReturn {
|
|
86
|
+
user: AuthUser$1 | null;
|
|
87
|
+
session: Session | null;
|
|
88
|
+
isLoading: boolean;
|
|
89
|
+
isAuthenticated: boolean;
|
|
90
|
+
login: (email: string, password: string) => Promise<void>;
|
|
91
|
+
logout: () => Promise<void>;
|
|
92
|
+
refreshUser: () => Promise<void>;
|
|
93
|
+
getAccessToken: () => Promise<string | null>;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Hook to access authentication state and methods
|
|
97
|
+
*/
|
|
98
|
+
declare function useAuth(): UseAuthReturn;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* React hook for permission checks
|
|
102
|
+
*/
|
|
103
|
+
|
|
104
|
+
interface UsePermissionsReturn {
|
|
105
|
+
hasRole: (requiredRole: CompanyRole, companyId?: string) => boolean;
|
|
106
|
+
isOwner: (companyId?: string) => boolean;
|
|
107
|
+
isAdmin: (companyId?: string) => boolean;
|
|
108
|
+
canEdit: (companyId?: string) => boolean;
|
|
109
|
+
canView: (companyId?: string) => boolean;
|
|
110
|
+
currentRole: CompanyRole | null;
|
|
111
|
+
currentCompanyId: string | null;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Hook to check user permissions
|
|
115
|
+
*/
|
|
116
|
+
declare function usePermissions(): UsePermissionsReturn;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Functional auth API for Django backend
|
|
120
|
+
*
|
|
121
|
+
* Stateless functions for authentication flows: sign in, register, OAuth, token refresh, etc.
|
|
122
|
+
* Uses fetch (browser/Node) and getCsrfToken from shared utils.
|
|
123
|
+
* No Next.js dependency.
|
|
124
|
+
*/
|
|
125
|
+
interface AuthUser {
|
|
126
|
+
id: string;
|
|
127
|
+
email: string;
|
|
128
|
+
name?: string | null;
|
|
129
|
+
firstName?: string | null;
|
|
130
|
+
lastName?: string | null;
|
|
131
|
+
groups?: string[];
|
|
132
|
+
permissions?: string[];
|
|
133
|
+
isActive?: boolean;
|
|
134
|
+
isEmailVerified?: boolean;
|
|
135
|
+
}
|
|
136
|
+
declare function resolveAuthUrl(path: string): string;
|
|
137
|
+
declare function getAccessToken(): string | null;
|
|
138
|
+
declare function setAccessToken(token: string | null): void;
|
|
139
|
+
declare function signInWithCredentials(email: string, password: string): Promise<{
|
|
140
|
+
access?: string;
|
|
141
|
+
user?: AuthUser;
|
|
142
|
+
}>;
|
|
143
|
+
declare function registerAccount(payload: {
|
|
144
|
+
email: string;
|
|
145
|
+
password: string;
|
|
146
|
+
passwordConfirm: string;
|
|
147
|
+
name?: string;
|
|
148
|
+
firstName?: string;
|
|
149
|
+
lastName?: string;
|
|
150
|
+
}): Promise<{
|
|
151
|
+
access?: string;
|
|
152
|
+
user?: AuthUser;
|
|
153
|
+
}>;
|
|
154
|
+
declare function requestPasswordReset(email: string): Promise<void>;
|
|
155
|
+
declare function resetPassword(payload: {
|
|
156
|
+
token: string;
|
|
157
|
+
password: string;
|
|
158
|
+
passwordConfirm: string;
|
|
159
|
+
email?: string;
|
|
160
|
+
}): Promise<void>;
|
|
161
|
+
declare function verifyEmail(token: string): Promise<void>;
|
|
162
|
+
declare function resendVerification(access?: string | null): Promise<void>;
|
|
163
|
+
declare function initiateGoogleOAuth(redirectUri: string): Promise<any>;
|
|
164
|
+
declare function completeGoogleOAuth(code: string, state: string): Promise<{
|
|
165
|
+
access?: string;
|
|
166
|
+
user?: AuthUser;
|
|
167
|
+
}>;
|
|
168
|
+
declare function refreshAccessToken(): Promise<string | null>;
|
|
169
|
+
declare function getMe(): Promise<AuthUser | null>;
|
|
170
|
+
declare function signOut(): Promise<void>;
|
|
171
|
+
declare function authFetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response>;
|
|
172
|
+
declare function hasPermission(user: AuthUser | null | undefined, permission: string): boolean;
|
|
173
|
+
declare function hasGroup(user: AuthUser | null | undefined, group: string): boolean;
|
|
174
|
+
|
|
175
|
+
export { AuthClient, AuthProvider, type AuthUser, type UseAuthReturn, type UsePermissionsReturn, authFetch, completeGoogleOAuth, getAccessToken, getMe, hasGroup, hasPermission, initiateGoogleOAuth, refreshAccessToken, registerAccount, requestPasswordReset, resendVerification, resetPassword, resolveAuthUrl, setAccessToken, signInWithCredentials, signOut, useAuth, useAuthContext, usePermissions, verifyEmail };
|