-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignup.jsx
More file actions
95 lines (85 loc) · 2.87 KB
/
Copy pathsignup.jsx
File metadata and controls
95 lines (85 loc) · 2.87 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
import React, { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { getAuth, createUserWithEmailAndPassword } from 'firebase/auth';
import { getFirestore, doc, setDoc } from 'firebase/firestore';
import './signup.css'
function Signup() {
const [name, setName] = useState('');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [confirmPassword, setConfirmPassword] = useState('');
const [error, setError] = useState(null);
const auth = getAuth();
const db = getFirestore();
const navigate = useNavigate(); // Initialize the navigate function
const handleSignup = async (e) => {
e.preventDefault();
if (password !== confirmPassword) {
setError('Passwords do not match');
return;
}
try {
const userCredential = await createUserWithEmailAndPassword(auth, email, password);
// User successfully signed up, you can access userCredential.user to get the user information
const user = userCredential.user;
// Store user data in Firestore
const userData = {
name,
email,
password: password, // You can choose to store the password or not (it's recommended not to store passwords)
};
const userDocRef = doc(db, 'users', user.uid);
await setDoc(userDocRef, userData);
// After successful signup, navigate to the login page
navigate('/login');
} catch (error) {
setError(error.message);
}
};
return (
<div className="container">
<p>Create a DEV@Deakin Account</p>
<form onSubmit={handleSignup}>
<label htmlFor="name">Name:</label>
<input
type="text"
id="name"
name="name"
value={name}
onChange={(e) => setName(e.target.value)}
required
/><br /><br />
<label htmlFor="email">Email:</label>
<input
type="email"
id="email"
name="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
required
/><br /><br />
<label htmlFor="password">Password:</label>
<input
type="password"
id="password"
name="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
required
/><br /><br />
<label htmlFor="confirmPassword">Confirm Password:</label>
<input
type="password"
id="confirmPassword"
name="confirmPassword"
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.target.value)}
required
/><br /><br />
<button type="submit">Create</button>
</form>
{error && <p className="error">{error}</p>}
</div>
);
}
export default Signup;