質問編集履歴

1

add signup

2022/10/30 12:51

投稿

busiRyu110
busiRyu110

スコア2

test CHANGED
File without changes
test CHANGED
@@ -51,3 +51,90 @@
51
51
  }
52
52
  ```
53
53
 
54
+ こちらがサインアップページです
55
+
56
+ ```tsx
57
+ import { useRef } from "react";
58
+ import { useForm } from "react-hook-form";
59
+ import { ErrorMessage } from "../../components/ErrorMessage";
60
+ import {
61
+ createUserWithEmailAndPassword,
62
+ getAuth,
63
+ sendEmailVerification,
64
+ } from 'firebase/auth'
65
+ import { FirebaseError } from '@firebase/util'
66
+ import { openNotificationWithIcon } from "../../components/OpenNotificationWithIcon";
67
+
68
+ export const SignUp = () => {
69
+ const { register, handleSubmit, reset, formState: { errors } } = useForm<any>();
70
+ const emailRef = useRef<string>('')
71
+ const passwordRef = useRef<string>('')
72
+ const onSubmit = async (data: any) => {
73
+ try {
74
+ const auth = getAuth()
75
+ const userCredential = await createUserWithEmailAndPassword(
76
+ auth,
77
+ emailRef.current,
78
+ passwordRef.current
79
+ )
80
+ await sendEmailVerification(userCredential.user)
81
+ reset()
82
+ } catch (e) {
83
+ if (e instanceof FirebaseError) {
84
+ openNotificationWithIcon({ type: "error", description: "サインアップに失敗しました" })
85
+ console.log(e)
86
+ }
87
+ }
88
+ }
89
+ return (
90
+ <div className="animate-pulse min-h-screen bg-slate-200 py-6 flex flex-col justify-center relative overflow-hidden sm:py-12">
91
+ <span className="border text-4xl text-yellow-800 px-6 pt-10 pb-8 bg-white w-1/2 max-w-md mx-auto rounded-t-md sm:px-10">サインアップ</span>
92
+ <div className="border relative px-4 pt-7 pb-8 bg-white shadow-xl w-1/2 max-w-md mx-auto sm:px-10 rounded-b-md">
93
+ <form action="" onSubmit={handleSubmit(onSubmit)}>
94
+ <label htmlFor="" className="block">Username or Email</label>
95
+ <input
96
+ {...register('email', {
97
+ required: true,
98
+ })}
99
+ type="Email"
100
+ id="email"
101
+ name="email"
102
+ className="border w-full h-10 px-3 mb-5 rounded-md"
103
+ placeholder="Username or Email"
104
+ onChange={(e) => emailRef.current = e.target.value}
105
+ />
106
+ <ErrorMessage children={errors.email?.type === "required" && "メールアドレスを入力してください"} />
107
+ <label htmlFor="" className="block">Password</label>
108
+ <input {...register('password', {
109
+ required: true,
110
+ })}
111
+ type="password"
112
+ id="password"
113
+ name="password"
114
+ className="border w-full h-10 px-3 mb-5 rounded-md"
115
+ placeholder="password"
116
+ onChange={(e) => passwordRef.current = e.target.value}
117
+ />
118
+ <ErrorMessage children={errors.password?.type === "required" && "パスワードを入力してください"} />
119
+ <div className="flex items-start">
120
+ <div className="flex items-start">
121
+ <div className="flex items-center">
122
+ <input required id="remember" aria-describedby="remember" type="checkbox" className="bg-gray-50 border border-gray-300 focus:ring-3 focus:ring-blue-300 h-4 w-4 rounded dark:bg-gray-700 dark:border-gray-600 dark:focus:ring-blue-600 dark:ring-offset-gray-800" />
123
+ </div>
124
+ <div className="text-sm ml-3">
125
+ <label htmlFor="remember" className="font-medium text-gray-900">Remember me</label>
126
+ </div>
127
+ </div>
128
+ <a href="#" className="text-sm text-blue-700 hover:underline ml-auto dark:text-blue-500">Lost
129
+ Password?</a>
130
+ </div>
131
+ <button className="mt-5 bg-green-500 hover:bg-blue-700 shadow-xl text-white uppercase text-sm font-semibold px-14 py-3 rounded">サインアップ</button>
132
+ </form>
133
+ </div>
134
+ </div>
135
+ )
136
+ }
137
+
138
+ export default SignUp
139
+ ```
140
+