| <!DOCTYPE html>
|
| <html>
|
| <head>
|
| <meta charset="UTF-8">
|
| <title>OTP Login</title>
|
| <script src="https://www.gstatic.com/firebasejs/9.22.2/firebase-app-compat.js"></script>
|
| <script src="https://www.gstatic.com/firebasejs/9.22.2/firebase-auth-compat.js"></script>
|
| </head>
|
| <body>
|
| <h2>Login with Phone Number</h2>
|
|
|
| <input type="text" id="phoneNumber" placeholder="+91XXXXXXXXXX">
|
| <div id="recaptcha-container"></div>
|
| <button id="sendBtn" onclick="sendOTP()">Send OTP</button>
|
|
|
| <br><br>
|
|
|
| <input type="text" id="otp" placeholder="Enter OTP">
|
| <button onclick="verifyOTP()">Verify OTP</button>
|
|
|
| <script>
|
|
|
| const firebaseConfig = {
|
| apiKey: "AIzaSyALkU6ON3Zt7vD-USso6zCxcKYQXy-dZms",
|
| authDomain: "aahaar-pilot.firebaseapp.com",
|
| projectId: "aahaar-pilot",
|
| storageBucket: "aahaar-pilot.firebasestorage.app",
|
| messagingSenderId: "349939809630",
|
| appId: "1:349939809630:web:7dd54c88f426c872faa3cf"
|
| };
|
|
|
| firebase.initializeApp(firebaseConfig);
|
|
|
| // Setup reCAPTCHA (invisible)
|
| window.recaptchaVerifier = new firebase.auth.RecaptchaVerifier('recaptcha-container', {
|
| size: 'invisible',
|
| callback: function(response) {
|
| return response
|
| }
|
| });
|
|
|
| let confirmationResult;
|
|
|
| function sendOTP() {
|
| const phone = document.getElementById('phoneNumber').value;
|
|
|
| firebase.auth().signInWithPhoneNumber(phone, window.recaptchaVerifier)
|
| .then((result) => {
|
| confirmationResult = result;
|
| //alert("OTP Sent Successfully!");
|
| console.log("otp send successfully");
|
|
|
| cooldown();
|
| })
|
|
|
| .catch((error) => {
|
| //alert("Error: " + error.message);
|
| });
|
| }
|
|
|
| function cooldown() {
|
| const btn = document.getElementById("sendBtn");
|
| btn.disabled = true;
|
| let time = 30;
|
|
|
| const timer = setInterval(() => {
|
| btn.innerText = `resend in ${time--}s`;
|
| if (time < 0) {
|
| clearInterval(timer);
|
| btn.innerText = "Send OTP";
|
| btn.disabled = false;
|
| }
|
| }, 1000);
|
| }
|
| let fcmtoken = "";
|
|
|
| function verifyOTP() {
|
| const otp = document.getElementById('otp').value;
|
|
|
| confirmationResult.confirm(otp)
|
| .then((result) => {
|
| const user = result.user;
|
|
|
|
|
| user.getIdToken().then((token) => {
|
| //alert("Login Success!");
|
| console.log("Firebase Token:", token);
|
| console.log("login success")
|
|
|
|
|
|
|
| fetch("https://script.google.com/macros/s/AKfycbwSZV1AeFeEWU3QLUnoPN8bKqRwIlNssCNbukct0JdKUf7pCKFMRD5B5P3mGqmAvIHz/exec", {
|
| method: "POST",
|
| redirect:"follow",
|
| body: JSON.stringify({ idToken: token, fcmtoken:token
|
|
|
| }),
|
| headers: { "Content-Type": 'text/plain;charset=utf-8'}
|
| })
|
| .then(res => res.json())
|
| .then(data => {
|
| console.log("Backend Response:", data);
|
|
|
|
|
| if (data.message === "User already logged in")
|
| {
|
| alert("User already logged in!");
|
| }
|
| else (data.message === "Login success")
|
| {
|
| alert("Login successful!");
|
| }
|
|
|
| });
|
| });
|
| })
|
| .catch((error) => {
|
| alert("Invalid OTP. Please try again.");
|
| });
|
| }
|
| </script>
|
| </body>
|
| </html>
|