-
Notifications
You must be signed in to change notification settings - Fork 1
/
Signup.js
117 lines (105 loc) · 3.18 KB
/
Signup.js
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
import React, { useState } from "react";
import './Signup.css';
import { auth } from "./firebase-config.js"; // Import the initialized `auth` instance
import { RecaptchaVerifier, signInWithPhoneNumber } from "firebase/auth";
const Signup = () => {
const [username, setUsername] = useState("");
const [phoneNumber, setPhoneNumber] = useState("");
const [otp, setOtp] = useState("");
const [language, setLanguage] = useState("en");
const [showOtpField, setShowOtpField] = useState(false);
const [confirmationResult, setConfirmationResult] = useState(null);
const setupRecaptcha = () => {
if (!window.recaptchaVerifier) {
window.recaptchaVerifier = new RecaptchaVerifier(
"recaptcha-container",
{
size: "invisible",
callback: () => {
console.log("Recaptcha verified");
},
},
auth
);
}
};
const handleGetOtp = async (e) => {
e.preventDefault();
setupRecaptcha();
try {
const appVerifier = window.recaptchaVerifier;
const confirmation = await signInWithPhoneNumber(auth, phoneNumber, appVerifier);
setConfirmationResult(confirmation);
setShowOtpField(true);
} catch (error) {
console.error("Error sending OTP:", error);
alert("Failed to send OTP. Please try again.");
}
};
const handleVerifyOtp = async (e) => {
e.preventDefault();
if (confirmationResult) {
try {
await confirmationResult.confirm(otp);
alert("Phone number verified successfully!");
} catch (error) {
alert("Invalid OTP! " + error.message);
}
} else {
alert("Please request a new OTP.");
}
};
return (
<div className="signup-container">
<header>
<h1>Environmental Monitoring and Reporting System</h1>
</header>
<form onSubmit={showOtpField ? handleVerifyOtp : handleGetOtp}>
<div id="recaptcha-container"></div>
<div className="input-field">
<label>Username</label>
<input
type="text"
value={username}
onChange={(e) => setUsername(e.target.value)}
required
/>
</div>
<div className="input-field">
<label>Phone Number</label>
<input
type="tel"
value={phoneNumber}
onChange={(e) => setPhoneNumber(e.target.value)}
required
/>
</div>
<div className="language-selection">
<label>Select Language:</label>
<select
value={language}
onChange={(e) => setLanguage(e.target.value)}
>
<option value="en">English</option>
<option value="hi">Hindi</option>
</select>
</div>
{showOtpField && (
<div className="input-field">
<label>OTP</label>
<input
type="text"
value={otp}
onChange={(e) => setOtp(e.target.value)}
required
/>
</div>
)}
<button type="submit">
{showOtpField ? "Verify OTP" : "Get OTP"}
</button>
</form>
</div>
);
};
export default Signup;