@@ -27,6 +27,18 @@ <h1>HELLO!</h1>
27
27
< input type ="number " id ="month " placeholder ="MM " min ="1 " max ="12 " required >
28
28
< input type ="number " id ="date " placeholder ="DD " min ="1 " max ="31 " required >
29
29
</ div >
30
+ < div class ="gender-options ">
31
+ < p > Select your gender:</ p >
32
+ < label >
33
+ < input type ="radio " name ="gender " value ="male " required > Male
34
+ </ label >
35
+ < label >
36
+ < input type ="radio " name ="gender " value ="female " required > Female
37
+ </ label >
38
+ < label >
39
+ < input type ="radio " name ="gender " value ="other " required > Other
40
+ </ label >
41
+ </ div >
30
42
< button id ="signup "> Next</ button >
31
43
</ form >
32
44
< p id ="messages " style ="color: red; font-weight: bold; "> </ p >
@@ -35,6 +47,7 @@ <h1>HELLO!</h1>
35
47
< script type ="module ">
36
48
import { initializeApp } from 'https://www.gstatic.com/firebasejs/11.0.2/firebase-app.js' ;
37
49
import { getAuth , createUserWithEmailAndPassword } from 'https://www.gstatic.com/firebasejs/11.0.2/firebase-auth.js' ;
50
+ import { getFirestore , doc , setDoc } from 'https://www.gstatic.com/firebasejs/11.0.2/firebase-firestore.js' ;
38
51
39
52
const firebaseConfig = {
40
53
apiKey : "AIzaSyC4guPinRRwZ3AlBdeiZMbn4XKq6_-d1Bo" ,
@@ -47,17 +60,24 @@ <h1>HELLO!</h1>
47
60
48
61
const app = initializeApp ( firebaseConfig ) ;
49
62
const auth = getAuth ( ) ;
63
+ const db = getFirestore ( ) ;
50
64
51
65
const signupForm = document . getElementById ( 'signup-form' ) ;
52
66
53
- signupForm . addEventListener ( 'submit' , ( event ) => {
67
+ signupForm . addEventListener ( 'submit' , async ( event ) => {
54
68
event . preventDefault ( ) ;
55
69
56
70
const email = document . getElementById ( 'email' ) . value ;
57
71
const password = document . getElementById ( 'password' ) . value ;
58
72
const year = parseInt ( document . getElementById ( 'year' ) . value , 10 ) ;
59
73
const month = parseInt ( document . getElementById ( 'month' ) . value , 10 ) ;
60
74
const date = parseInt ( document . getElementById ( 'date' ) . value , 10 ) ;
75
+ const gender = document . querySelector ( 'input[name="gender"]:checked' ) ;
76
+
77
+ if ( ! gender ) {
78
+ document . getElementById ( 'messages' ) . textContent = "Please select your gender." ;
79
+ return ;
80
+ }
61
81
62
82
const age = getAgeFromInputs ( year , month , date ) ;
63
83
@@ -71,13 +91,23 @@ <h1>HELLO!</h1>
71
91
return ;
72
92
}
73
93
74
- createUserWithEmailAndPassword ( auth , email , password )
75
- . then ( ( userCredential ) => {
76
- document . getElementById ( 'messages' ) . textContent = 'Account successfully created!' ;
77
- } )
78
- . catch ( ( error ) => {
79
- document . getElementById ( 'messages' ) . textContent = `Error: ${ error . code } / ${ error . message } ` ;
94
+ try {
95
+ const userCredential = await createUserWithEmailAndPassword ( auth , email , password ) ;
96
+ const user = userCredential . user ;
97
+
98
+ // Save additional data to Firestore
99
+ const userDoc = doc ( db , "users" , user . uid ) ;
100
+ await setDoc ( userDoc , {
101
+ email : email ,
102
+ barthday : year + "/" + month + "/" + date ,
103
+ gender : gender . value ,
104
+ signupDate : new Date ( ) . toISOString ( )
80
105
} ) ;
106
+
107
+ document . getElementById ( 'messages' ) . textContent = 'Account successfully created!' ;
108
+ } catch ( error ) {
109
+ document . getElementById ( 'messages' ) . textContent = `Error: ${ error . code } / ${ error . message } ` ;
110
+ }
81
111
} ) ;
82
112
83
113
function getAgeFromInputs ( year , month , date ) {
0 commit comments