Skip to content

Commit e8fdf0c

Browse files
committed
undo undoing the undo
1 parent 6735801 commit e8fdf0c

File tree

1 file changed

+334
-0
lines changed

1 file changed

+334
-0
lines changed
Lines changed: 334 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,334 @@
1+
'use client';
2+
3+
import { authClient, signIn, signOut, signUp } from './auth-client';
4+
5+
interface TwoFactorResponse {
6+
data?: {
7+
totpURI?: string;
8+
backupCodes?: string[];
9+
twoFactorEnabled: boolean;
10+
};
11+
error?: {
12+
message: string;
13+
};
14+
}
15+
16+
/**
17+
* Helper function to sign in with email and password with simplified redirect handling
18+
*/
19+
export async function loginWithEmail(
20+
email: string,
21+
password: string,
22+
options?: {
23+
redirectUrl?: string;
24+
router?: any; // Next.js router
25+
onError?: (error: any) => void;
26+
}
27+
) {
28+
try {
29+
const result = await signIn.email({
30+
email,
31+
password,
32+
fetchOptions: {
33+
onSuccess: () => {
34+
if (options?.router && options.redirectUrl) {
35+
options.router.push(options.redirectUrl);
36+
options.router.refresh();
37+
}
38+
},
39+
},
40+
});
41+
42+
return { success: !result?.error, data: result };
43+
} catch (error) {
44+
if (options?.onError) {
45+
options.onError(error);
46+
}
47+
return { success: false, error };
48+
}
49+
}
50+
51+
/**
52+
* Helper function to sign up with email and password with simplified redirect handling
53+
*/
54+
export async function registerWithEmail(
55+
email: string,
56+
password: string,
57+
name: string,
58+
options?: {
59+
redirectUrl?: string;
60+
router?: any; // Next.js router
61+
onError?: (error: any) => void;
62+
}
63+
) {
64+
try {
65+
const result = await signUp.email({
66+
email,
67+
password,
68+
name,
69+
fetchOptions: {
70+
onSuccess: () => {
71+
if (options?.router && options.redirectUrl) {
72+
options.router.push(options.redirectUrl);
73+
options.router.refresh();
74+
}
75+
},
76+
},
77+
});
78+
79+
return { success: !result?.error, data: result };
80+
} catch (error) {
81+
if (options?.onError) {
82+
options.onError(error);
83+
}
84+
return { success: false, error };
85+
}
86+
}
87+
88+
/**
89+
* Helper function to sign out with simplified redirect handling
90+
*/
91+
export async function logout(options?: {
92+
redirectUrl?: string;
93+
router?: any; // Next.js router
94+
onError?: (error: any) => void;
95+
}) {
96+
try {
97+
await signOut();
98+
99+
if (options?.router && options.redirectUrl) {
100+
options.router.push(options.redirectUrl);
101+
}
102+
103+
return { success: true };
104+
} catch (error) {
105+
if (options?.onError) {
106+
options.onError(error);
107+
}
108+
return { success: false, error };
109+
}
110+
}
111+
112+
/**
113+
* Helper function to sign in with Google with simplified redirect handling
114+
*/
115+
export function loginWithGoogle(options?: {
116+
redirectUrl?: string;
117+
router?: any; // Next.js router
118+
onError?: (error: any) => void;
119+
}) {
120+
try {
121+
return signIn.social({
122+
provider: 'google',
123+
fetchOptions: {
124+
onSuccess: () => {
125+
if (options?.router && options.redirectUrl) {
126+
options.router.push(options.redirectUrl);
127+
options.router.refresh();
128+
}
129+
},
130+
},
131+
});
132+
} catch (error) {
133+
if (options?.onError) {
134+
options.onError(error);
135+
}
136+
return { success: false, error };
137+
}
138+
}
139+
140+
/**
141+
* Helper function to sign in with GitHub with simplified redirect handling
142+
*/
143+
export function loginWithGithub(options?: {
144+
redirectUrl?: string;
145+
router?: any; // Next.js router
146+
onError?: (error: any) => void;
147+
}) {
148+
try {
149+
return signIn.social({
150+
provider: 'github',
151+
fetchOptions: {
152+
onSuccess: () => {
153+
if (options?.router && options.redirectUrl) {
154+
options.router.push(options.redirectUrl);
155+
options.router.refresh();
156+
}
157+
},
158+
},
159+
});
160+
} catch (error) {
161+
if (options?.onError) {
162+
options.onError(error);
163+
}
164+
return { success: false, error };
165+
}
166+
}
167+
168+
/**
169+
* Helper function to enable 2FA for a user
170+
*/
171+
export async function enableTwoFactor(
172+
password: string,
173+
options?: {
174+
onSuccess?: (data: TwoFactorResponse['data']) => void;
175+
onError?: (error: any) => void;
176+
}
177+
) {
178+
try {
179+
const result = await authClient.twoFactor.enable({
180+
password,
181+
});
182+
183+
if (result.data) {
184+
options?.onSuccess?.({
185+
...result.data,
186+
twoFactorEnabled: false, // Will be true after verification
187+
});
188+
} else if (result.error) {
189+
options?.onError?.(result.error);
190+
}
191+
192+
return { success: !!result.data, data: result };
193+
} catch (error) {
194+
options?.onError?.(error);
195+
return { success: false, error };
196+
}
197+
}
198+
199+
/**
200+
* Helper function to verify TOTP code
201+
*/
202+
export async function verifyTwoFactorCode(
203+
code: string,
204+
options?: {
205+
trustDevice?: boolean;
206+
onSuccess?: () => void;
207+
onError?: (error: any) => void;
208+
}
209+
) {
210+
try {
211+
const result = await authClient.twoFactor.verifyTotp({
212+
code,
213+
trustDevice: options?.trustDevice,
214+
});
215+
216+
if (result.data) {
217+
options?.onSuccess?.();
218+
} else if (result.error) {
219+
options?.onError?.(result.error);
220+
}
221+
222+
return { success: !!result.data, data: result };
223+
} catch (error) {
224+
options?.onError?.(error);
225+
return { success: false, error };
226+
}
227+
}
228+
229+
/**
230+
* Helper function to verify backup code
231+
*/
232+
export async function verifyBackupCode(
233+
code: string,
234+
options?: {
235+
onSuccess?: () => void;
236+
onError?: (error: any) => void;
237+
}
238+
) {
239+
try {
240+
const result = await authClient.twoFactor.verifyBackupCode({
241+
code,
242+
});
243+
244+
if (result.data) {
245+
options?.onSuccess?.();
246+
} else if (result.error) {
247+
options?.onError?.(result.error);
248+
}
249+
250+
return { success: !!result.data, data: result };
251+
} catch (error) {
252+
options?.onError?.(error);
253+
return { success: false, error };
254+
}
255+
}
256+
257+
/**
258+
* Helper function to send OTP
259+
*/
260+
export async function sendOTP(options?: {
261+
onSuccess?: () => void;
262+
onError?: (error: any) => void;
263+
}) {
264+
try {
265+
const result = await authClient.twoFactor.sendOtp();
266+
267+
if (result.data) {
268+
options?.onSuccess?.();
269+
} else if (result.error) {
270+
options?.onError?.(result.error);
271+
}
272+
273+
return { success: !!result.data, data: result };
274+
} catch (error) {
275+
options?.onError?.(error);
276+
return { success: false, error };
277+
}
278+
}
279+
280+
/**
281+
* Helper function to verify OTP
282+
*/
283+
export async function verifyOTP(
284+
code: string,
285+
options?: {
286+
onSuccess?: () => void;
287+
onError?: (error: any) => void;
288+
}
289+
) {
290+
try {
291+
const result = await authClient.twoFactor.verifyOtp({
292+
code,
293+
});
294+
295+
if (result.data) {
296+
options?.onSuccess?.();
297+
} else if (result.error) {
298+
options?.onError?.(result.error);
299+
}
300+
301+
return { success: !!result.data, data: result };
302+
} catch (error) {
303+
options?.onError?.(error);
304+
return { success: false, error };
305+
}
306+
}
307+
308+
/**
309+
* Helper function to generate backup codes
310+
*/
311+
export async function generateBackupCodes(
312+
password: string,
313+
options?: {
314+
onSuccess?: (backupCodes: string[]) => void;
315+
onError?: (error: any) => void;
316+
}
317+
) {
318+
try {
319+
const result = await authClient.twoFactor.generateBackupCodes({
320+
password,
321+
});
322+
323+
if (result.data?.backupCodes) {
324+
options?.onSuccess?.(result.data.backupCodes);
325+
} else if (result.error) {
326+
options?.onError?.(result.error);
327+
}
328+
329+
return { success: !!result.data, data: result };
330+
} catch (error) {
331+
options?.onError?.(error);
332+
return { success: false, error };
333+
}
334+
}

0 commit comments

Comments
 (0)