-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhooks.ts
More file actions
55 lines (49 loc) · 1.7 KB
/
hooks.ts
File metadata and controls
55 lines (49 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { useForm } from '@tanstack/react-form';
import { useState } from 'react';
import { z } from 'zod';
import { useInvalidateMe } from '@/contexts/AuthContext';
import { authClient } from '@/lib/auth/client';
export function useRegisterForm(
onSuccess: (email: string) => void,
verificationCallbackUrl?: string,
) {
const invalidateMe = useInvalidateMe();
const [error, setError] = useState<string | null>(null);
const form = useForm({
defaultValues: { name: '', email: '', password: '', confirmPassword: '' },
onSubmit: async ({ value }) => {
setError(null);
const result = await authClient.signUp.email({
name: value.name,
email: value.email,
password: value.password,
callbackURL: verificationCallbackUrl
? new URL(verificationCallbackUrl, window.location.origin).toString()
: `${window.location.origin}/auth/verify-email`,
});
if (result.error) {
setError(result.error.message ?? 'アカウント作成に失敗しました');
return;
}
if (result.data?.token) {
await invalidateMe();
}
onSuccess(value.email);
},
});
return {
form,
error,
validators: {
name: z.string().min(1, '名前を入力してください'),
email: z.string().email('有効なメールアドレスを入力してください'),
password: z.string().min(8, 'パスワードは8文字以上で入力してください'),
confirmPassword: ({ value, password }: { value: string; password: string }) => {
if (value !== password) {
return { message: 'パスワードが一致しません' };
}
return undefined;
},
},
};
}