-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.tsx
More file actions
174 lines (169 loc) · 6.7 KB
/
page.tsx
File metadata and controls
174 lines (169 loc) · 6.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
'use client';
import Link from 'next/link';
import { useRouter, useSearchParams } from 'next/navigation';
import { Suspense, useState } from 'react';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Input } from '@/components/ui/input';
import { useRegisterForm } from '@/features/public/auth/register/hooks';
import { appendCallbackUrl, normalizeCallbackUrl } from '@/lib/auth/callback-url';
export default function RegisterPage() {
return (
<Suspense fallback={null}>
<RegisterPageContent />
</Suspense>
);
}
function RegisterPageContent() {
const router = useRouter();
const searchParams = useSearchParams();
const rawCallbackUrl = searchParams.get('callbackUrl');
const callbackUrl = normalizeCallbackUrl(rawCallbackUrl);
const hasCallbackUrl = rawCallbackUrl !== null;
const loginHref = hasCallbackUrl ? appendCallbackUrl('/auth/login', callbackUrl) : '/auth/login';
const [registeredEmail, setRegisteredEmail] = useState<string | null>(null);
const { form, error, validators } = useRegisterForm(
(email) => {
setRegisteredEmail(email);
},
hasCallbackUrl ? callbackUrl : undefined,
);
if (registeredEmail) {
return (
<div className='container mx-auto px-4 py-16 flex justify-center'>
<Card className='w-full max-w-md'>
<CardHeader>
<CardTitle>確認メールを送信しました</CardTitle>
<CardDescription>{registeredEmail} に確認リンクを送信しました</CardDescription>
</CardHeader>
<CardContent className='space-y-4'>
<p className='text-sm text-muted-foreground'>
メール内のリンクを開くとアカウント作成が完了します。
</p>
<Button type='button' className='w-full' onClick={() => router.push(loginHref)}>
ログインへ
</Button>
</CardContent>
</Card>
</div>
);
}
return (
<div className='container mx-auto px-4 py-16 flex justify-center'>
<Card className='w-full max-w-md'>
<CardHeader>
<CardTitle>アカウント作成</CardTitle>
<CardDescription>新しいアカウントを作成します</CardDescription>
</CardHeader>
<CardContent>
<form
onSubmit={(e) => {
e.preventDefault();
form.handleSubmit();
}}
className='space-y-4'
>
<form.Field name='name' validators={{ onChange: validators.name }}>
{(field) => (
<div className='space-y-1'>
<label htmlFor={field.name} className='text-sm font-medium'>
お名前
</label>
<Input
id={field.name}
placeholder='山田 太郎'
value={field.state.value}
onBlur={field.handleBlur}
onChange={(e) => field.handleChange(e.target.value)}
/>
{field.state.meta.errors[0] && (
<p className='text-sm text-destructive'>{field.state.meta.errors[0].message}</p>
)}
</div>
)}
</form.Field>
<form.Field name='email' validators={{ onChange: validators.email }}>
{(field) => (
<div className='space-y-1'>
<label htmlFor={field.name} className='text-sm font-medium'>
メールアドレス
</label>
<Input
id={field.name}
type='email'
placeholder='you@example.com'
value={field.state.value}
onBlur={field.handleBlur}
onChange={(e) => field.handleChange(e.target.value)}
/>
{field.state.meta.errors[0] && (
<p className='text-sm text-destructive'>{field.state.meta.errors[0].message}</p>
)}
</div>
)}
</form.Field>
<form.Field name='password' validators={{ onChange: validators.password }}>
{(field) => (
<div className='space-y-1'>
<label htmlFor={field.name} className='text-sm font-medium'>
パスワード
</label>
<Input
id={field.name}
type='password'
value={field.state.value}
onBlur={field.handleBlur}
onChange={(e) => field.handleChange(e.target.value)}
/>
{field.state.meta.errors[0] && (
<p className='text-sm text-destructive'>{field.state.meta.errors[0].message}</p>
)}
</div>
)}
</form.Field>
<form.Field
name='confirmPassword'
validators={{
onChangeListenTo: ['password'],
onChange: ({ value, fieldApi }) => {
return validators.confirmPassword({
value,
password: fieldApi.form.getFieldValue('password'),
});
},
}}
>
{(field) => (
<div className='space-y-1'>
<label htmlFor={field.name} className='text-sm font-medium'>
パスワード(確認)
</label>
<Input
id={field.name}
type='password'
value={field.state.value}
onBlur={field.handleBlur}
onChange={(e) => field.handleChange(e.target.value)}
/>
{field.state.meta.errors[0] && (
<p className='text-sm text-destructive'>{field.state.meta.errors[0].message}</p>
)}
</div>
)}
</form.Field>
{error && <p className='text-sm text-destructive'>{error}</p>}
<Button type='submit' className='w-full' disabled={form.state.isSubmitting}>
{form.state.isSubmitting ? '作成中...' : 'アカウントを作成'}
</Button>
</form>
<p className='text-sm text-center text-muted-foreground mt-4'>
すでにアカウントをお持ちの方は{' '}
<Link href={loginHref} className='text-primary hover:underline'>
ログイン
</Link>
</p>
</CardContent>
</Card>
</div>
);
}