@thetechfossil/auth2 1.2.12 → 1.2.13
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.next.server.d.ts +272 -0
- package/package.json +3 -3
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import { UpfilesClient } from '@thetechfossil/upfiles';
|
|
2
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
3
|
+
|
|
4
|
+
interface LinkedAccount {
|
|
5
|
+
provider: 'google' | 'github';
|
|
6
|
+
providerId: string;
|
|
7
|
+
email: string;
|
|
8
|
+
avatar?: string;
|
|
9
|
+
}
|
|
10
|
+
interface User {
|
|
11
|
+
id: string;
|
|
12
|
+
_id?: string;
|
|
13
|
+
name: string;
|
|
14
|
+
email: string;
|
|
15
|
+
phoneNumber?: string;
|
|
16
|
+
avatar?: string;
|
|
17
|
+
role: string;
|
|
18
|
+
linkedAccounts?: LinkedAccount[];
|
|
19
|
+
createdAt: string;
|
|
20
|
+
updatedAt: string;
|
|
21
|
+
}
|
|
22
|
+
interface AuthResponse {
|
|
23
|
+
success: boolean;
|
|
24
|
+
message: string;
|
|
25
|
+
user?: User;
|
|
26
|
+
token?: string;
|
|
27
|
+
csrfToken?: string;
|
|
28
|
+
}
|
|
29
|
+
interface LoginData {
|
|
30
|
+
email?: string;
|
|
31
|
+
phoneNumber?: string;
|
|
32
|
+
password?: string;
|
|
33
|
+
otp?: string;
|
|
34
|
+
}
|
|
35
|
+
interface VerifyData {
|
|
36
|
+
email?: string;
|
|
37
|
+
phoneNumber?: string;
|
|
38
|
+
otp: string;
|
|
39
|
+
}
|
|
40
|
+
interface RegisterData {
|
|
41
|
+
name: string;
|
|
42
|
+
email?: string;
|
|
43
|
+
phoneNumber?: string;
|
|
44
|
+
password: string;
|
|
45
|
+
}
|
|
46
|
+
interface UpdateUserData {
|
|
47
|
+
name?: string;
|
|
48
|
+
avatar?: string;
|
|
49
|
+
email?: string;
|
|
50
|
+
username?: string;
|
|
51
|
+
phoneNumber?: string;
|
|
52
|
+
}
|
|
53
|
+
type OAuthProvider = 'google' | 'github';
|
|
54
|
+
interface OAuthConfig {
|
|
55
|
+
provider: OAuthProvider;
|
|
56
|
+
redirectUri?: string;
|
|
57
|
+
}
|
|
58
|
+
interface CsrfTokenResponse {
|
|
59
|
+
success: boolean;
|
|
60
|
+
csrfToken: string;
|
|
61
|
+
}
|
|
62
|
+
interface AuthConfig {
|
|
63
|
+
baseUrl: string;
|
|
64
|
+
localStorageKey?: string;
|
|
65
|
+
token?: string;
|
|
66
|
+
csrfEnabled?: boolean;
|
|
67
|
+
upfilesConfig?: UpfilesConfig;
|
|
68
|
+
}
|
|
69
|
+
interface UpfilesConfig {
|
|
70
|
+
baseUrl: string;
|
|
71
|
+
apiKey?: string;
|
|
72
|
+
apiKeyHeader?: 'authorization' | 'x-api-key' | 'x-up-api-key';
|
|
73
|
+
presignUrl?: string;
|
|
74
|
+
presignPath?: string;
|
|
75
|
+
folderPath?: string;
|
|
76
|
+
}
|
|
77
|
+
interface Session {
|
|
78
|
+
id: string;
|
|
79
|
+
userId?: string;
|
|
80
|
+
token?: string;
|
|
81
|
+
userAgent?: string;
|
|
82
|
+
ipAddress?: string;
|
|
83
|
+
expiresAt: string;
|
|
84
|
+
createdAt: string;
|
|
85
|
+
updatedAt?: string;
|
|
86
|
+
}
|
|
87
|
+
interface MFASetup {
|
|
88
|
+
success: boolean;
|
|
89
|
+
qrCode?: string;
|
|
90
|
+
secret?: string;
|
|
91
|
+
message: string;
|
|
92
|
+
}
|
|
93
|
+
interface AuditLog {
|
|
94
|
+
id: string;
|
|
95
|
+
userId: string;
|
|
96
|
+
action: string;
|
|
97
|
+
details?: any;
|
|
98
|
+
ipAddress?: string;
|
|
99
|
+
timestamp: string;
|
|
100
|
+
}
|
|
101
|
+
interface UseAuthReturn {
|
|
102
|
+
user: User | null;
|
|
103
|
+
isAuthenticated: boolean;
|
|
104
|
+
loading: boolean;
|
|
105
|
+
register: (data: RegisterData) => Promise<AuthResponse>;
|
|
106
|
+
login: (data: LoginData) => Promise<AuthResponse>;
|
|
107
|
+
loginWithOAuth: (provider: OAuthProvider) => void;
|
|
108
|
+
verify: (data: VerifyData) => Promise<AuthResponse>;
|
|
109
|
+
verifyEmailToken: (token: string) => Promise<AuthResponse>;
|
|
110
|
+
logout: () => Promise<void>;
|
|
111
|
+
updateProfile: (data: UpdateUserData) => Promise<AuthResponse>;
|
|
112
|
+
getProfile: () => Promise<User>;
|
|
113
|
+
getAllUsers: () => Promise<User[]>;
|
|
114
|
+
getUserById: (id: string) => Promise<User>;
|
|
115
|
+
linkOAuthProvider: (provider: OAuthProvider) => void;
|
|
116
|
+
unlinkOAuthProvider: (provider: OAuthProvider) => Promise<AuthResponse>;
|
|
117
|
+
csrfToken: string | null;
|
|
118
|
+
refreshCsrfToken: () => Promise<void>;
|
|
119
|
+
changePassword: (oldPassword: string, newPassword: string) => Promise<AuthResponse>;
|
|
120
|
+
updateAvatar: (avatar: string) => Promise<AuthResponse>;
|
|
121
|
+
uploadAndUpdateAvatar: (file: File) => Promise<AuthResponse>;
|
|
122
|
+
requestEmailChange: (newEmail: string) => Promise<AuthResponse>;
|
|
123
|
+
verifyEmailChange: (token: string) => Promise<AuthResponse>;
|
|
124
|
+
generate2FA: () => Promise<MFASetup>;
|
|
125
|
+
enable2FA: (token: string) => Promise<AuthResponse>;
|
|
126
|
+
disable2FA: (token: string) => Promise<AuthResponse>;
|
|
127
|
+
validate2FA: (token: string) => Promise<AuthResponse>;
|
|
128
|
+
getSessions: () => Promise<{
|
|
129
|
+
success: boolean;
|
|
130
|
+
sessions: Session[];
|
|
131
|
+
}>;
|
|
132
|
+
revokeSession: (sessionId: string) => Promise<AuthResponse>;
|
|
133
|
+
revokeAllSessions: () => Promise<AuthResponse>;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
declare class AuthService {
|
|
137
|
+
private httpClient;
|
|
138
|
+
private config;
|
|
139
|
+
private token;
|
|
140
|
+
private upfilesClient;
|
|
141
|
+
constructor(config: AuthConfig);
|
|
142
|
+
private loadTokenFromStorage;
|
|
143
|
+
private saveTokenToStorage;
|
|
144
|
+
private removeTokenFromStorage;
|
|
145
|
+
isAuthenticated(): boolean;
|
|
146
|
+
getToken(): string | null;
|
|
147
|
+
getCurrentUser(): User | null;
|
|
148
|
+
refreshCsrfToken(): Promise<void>;
|
|
149
|
+
getCsrfToken(): string | null;
|
|
150
|
+
loginWithOAuth(provider: OAuthProvider): void;
|
|
151
|
+
linkOAuthProvider(provider: OAuthProvider): void;
|
|
152
|
+
unlinkOAuthProvider(provider: OAuthProvider): Promise<AuthResponse>;
|
|
153
|
+
login(data: LoginData): Promise<AuthResponse>;
|
|
154
|
+
register(data: RegisterData): Promise<AuthResponse>;
|
|
155
|
+
verify(data: VerifyData): Promise<AuthResponse>;
|
|
156
|
+
verifyEmailToken(token: string): Promise<AuthResponse>;
|
|
157
|
+
logout(): Promise<void>;
|
|
158
|
+
getProfile(): Promise<User>;
|
|
159
|
+
updateProfile(data: UpdateUserData): Promise<AuthResponse>;
|
|
160
|
+
getAllUsers(): Promise<User[]>;
|
|
161
|
+
getUserById(id: string): Promise<User>;
|
|
162
|
+
forgotPassword(email: string): Promise<AuthResponse>;
|
|
163
|
+
resetPassword(token: string, password: string): Promise<AuthResponse>;
|
|
164
|
+
changePassword(oldPassword: string, newPassword: string): Promise<AuthResponse>;
|
|
165
|
+
updateAvatar(avatar: string): Promise<AuthResponse>;
|
|
166
|
+
uploadAndUpdateAvatar(file: File): Promise<AuthResponse>;
|
|
167
|
+
getUpfilesClient(): UpfilesClient | null;
|
|
168
|
+
requestEmailChange(newEmail: string): Promise<AuthResponse>;
|
|
169
|
+
verifyEmailChange(token: string): Promise<AuthResponse>;
|
|
170
|
+
generate2FA(): Promise<{
|
|
171
|
+
success: boolean;
|
|
172
|
+
qrCode?: string;
|
|
173
|
+
secret?: string;
|
|
174
|
+
message: string;
|
|
175
|
+
}>;
|
|
176
|
+
enable2FA(token: string): Promise<AuthResponse>;
|
|
177
|
+
disable2FA(token: string): Promise<AuthResponse>;
|
|
178
|
+
validate2FA(token: string): Promise<AuthResponse>;
|
|
179
|
+
getSessions(): Promise<{
|
|
180
|
+
success: boolean;
|
|
181
|
+
sessions: any[];
|
|
182
|
+
}>;
|
|
183
|
+
revokeSession(sessionId: string): Promise<AuthResponse>;
|
|
184
|
+
revokeAllSessions(): Promise<AuthResponse>;
|
|
185
|
+
getAuditLogs(filters?: any): Promise<{
|
|
186
|
+
success: boolean;
|
|
187
|
+
logs: any[];
|
|
188
|
+
}>;
|
|
189
|
+
adminVerifyUser(userId: string): Promise<AuthResponse>;
|
|
190
|
+
adminForcePasswordReset(userId: string): Promise<AuthResponse>;
|
|
191
|
+
adminSuspendUser(userId: string): Promise<AuthResponse>;
|
|
192
|
+
adminActivateUser(userId: string): Promise<AuthResponse>;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
declare class HttpClient {
|
|
196
|
+
private axiosInstance;
|
|
197
|
+
private csrfToken;
|
|
198
|
+
private frontendBaseUrl;
|
|
199
|
+
private baseUrl;
|
|
200
|
+
constructor(baseUrl: string, defaultHeaders?: Record<string, string>);
|
|
201
|
+
get<T>(endpoint: string, headers?: Record<string, string>): Promise<T>;
|
|
202
|
+
post<T>(endpoint: string, data?: any, headers?: Record<string, string>): Promise<T>;
|
|
203
|
+
put<T>(endpoint: string, data?: any, headers?: Record<string, string>): Promise<T>;
|
|
204
|
+
delete<T>(endpoint: string, headers?: Record<string, string>): Promise<T>;
|
|
205
|
+
setAuthToken(token: string): void;
|
|
206
|
+
removeAuthToken(): void;
|
|
207
|
+
setCsrfToken(token: string): void;
|
|
208
|
+
getCsrfToken(): string | null;
|
|
209
|
+
removeCsrfToken(): void;
|
|
210
|
+
setFrontendBaseUrl(url: string): void;
|
|
211
|
+
getFrontendBaseUrl(): string | null;
|
|
212
|
+
removeFrontendBaseUrl(): void;
|
|
213
|
+
private refreshCsrfToken;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
declare class AuthClient extends AuthService {
|
|
217
|
+
constructor(config: AuthConfig);
|
|
218
|
+
register(data: RegisterData): Promise<AuthResponse>;
|
|
219
|
+
login(data: LoginData): Promise<AuthResponse>;
|
|
220
|
+
verify(data: VerifyData): Promise<AuthResponse>;
|
|
221
|
+
logout(): Promise<void>;
|
|
222
|
+
getProfile(): Promise<User>;
|
|
223
|
+
getUserById(id: string): Promise<User>;
|
|
224
|
+
updateProfile(data: UpdateUserData): Promise<AuthResponse>;
|
|
225
|
+
getAllUsers(): Promise<User[]>;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
declare class NextServerAuth extends AuthClient {
|
|
229
|
+
constructor(config: AuthConfig);
|
|
230
|
+
static parseTokenFromHeaders(headers: Headers): string | null;
|
|
231
|
+
static parseTokenFromCookies(cookies: string): string | null;
|
|
232
|
+
static parseTokenFromRequest(req: any): string | null;
|
|
233
|
+
verifyToken(token: string): Promise<User | null>;
|
|
234
|
+
static createAuthenticatedClient(config: AuthConfig, token: string): NextServerAuth;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
interface AuthServerConfig {
|
|
238
|
+
authApiUrl?: string;
|
|
239
|
+
tokenCookieName?: string;
|
|
240
|
+
}
|
|
241
|
+
declare class AuthServer {
|
|
242
|
+
private config;
|
|
243
|
+
constructor(config?: AuthServerConfig);
|
|
244
|
+
getToken(): Promise<string | null>;
|
|
245
|
+
getCurrentUser(): Promise<User | null>;
|
|
246
|
+
isAuthenticated(): Promise<boolean>;
|
|
247
|
+
requireAuth(redirectTo?: string): Promise<User>;
|
|
248
|
+
redirectIfAuthenticated(redirectTo?: string): Promise<void>;
|
|
249
|
+
getProfile(): Promise<User | null>;
|
|
250
|
+
}
|
|
251
|
+
declare function getAuthServer(config?: AuthServerConfig): AuthServer;
|
|
252
|
+
declare function currentUser(): Promise<User | null>;
|
|
253
|
+
declare function auth(): Promise<{
|
|
254
|
+
user: User | null;
|
|
255
|
+
userId: string | null;
|
|
256
|
+
isAuthenticated: boolean;
|
|
257
|
+
token: string | null;
|
|
258
|
+
}>;
|
|
259
|
+
declare function requireAuth(redirectTo?: string): Promise<User>;
|
|
260
|
+
declare function redirectIfAuthenticated(redirectTo?: string): Promise<void>;
|
|
261
|
+
|
|
262
|
+
interface AuthMiddlewareConfig {
|
|
263
|
+
publicRoutes?: string[];
|
|
264
|
+
protectedRoutes?: string[];
|
|
265
|
+
loginUrl?: string;
|
|
266
|
+
afterLoginUrl?: string;
|
|
267
|
+
tokenCookieName?: string;
|
|
268
|
+
}
|
|
269
|
+
declare function authMiddleware(config?: AuthMiddlewareConfig): (request: NextRequest) => NextResponse<unknown>;
|
|
270
|
+
declare function createAuthMiddleware(config?: AuthMiddlewareConfig): (request: NextRequest) => NextResponse<unknown>;
|
|
271
|
+
|
|
272
|
+
export { AuditLog, AuthClient, AuthConfig, AuthResponse, AuthServer, AuthService, CsrfTokenResponse, HttpClient, LinkedAccount, LoginData, MFASetup, NextServerAuth, OAuthConfig, OAuthProvider, RegisterData, Session, UpdateUserData, UpfilesConfig, UseAuthReturn, User, VerifyData, auth, authMiddleware, createAuthMiddleware, currentUser, getAuthServer, redirectIfAuthenticated, requireAuth };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thetechfossil/auth2",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.13",
|
|
4
4
|
"description": "Authentication SDK for easy integration with Auth backend",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -68,8 +68,8 @@
|
|
|
68
68
|
"devDependencies": {
|
|
69
69
|
"@types/jest": "^29.0.0",
|
|
70
70
|
"@types/node": "^18.0.0",
|
|
71
|
-
"@types/react": "^
|
|
72
|
-
"@types/react-dom": "^
|
|
71
|
+
"@types/react": "^18.3.0",
|
|
72
|
+
"@types/react-dom": "^18.3.0",
|
|
73
73
|
"jest": "^29.0.0",
|
|
74
74
|
"next": "^14.0.0",
|
|
75
75
|
"react": "^18.0.0",
|