From 65b757a5991be086dc08b81188d0626e36cc9a44 Mon Sep 17 00:00:00 2001
From: razputshivanshu <89507255+razputshivanshu@users.noreply.github.com>
Date: Fri, 4 Oct 2024 15:13:49 +0530
Subject: [PATCH 1/4] I have implemented the validation for the new users
---
src/App.js | 44 ++++++------
src/components/Login.js | 40 ++++++++---
src/components/Navbar.js | 114 ++++++++++---------------------
src/components/ProtectedRoute.js | 36 ++++++++++
src/components/Register.js | 88 ++++++++----------------
src/context/AuthContext.js | 21 +++++-
src/firebase.js | 4 +-
7 files changed, 174 insertions(+), 173 deletions(-)
create mode 100644 src/components/ProtectedRoute.js
diff --git a/src/App.js b/src/App.js
index 4fede8f..043d34c 100644
--- a/src/App.js
+++ b/src/App.js
@@ -1,3 +1,4 @@
+// App.js
import React from 'react';
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
import Navbar from './components/Navbar';
@@ -11,36 +12,37 @@ import { AuthProvider } from './context/AuthContext';
import Support from './components/Support';
import Settings from './components/Setting';
import ImageQRCodeGenerator from './components/ImageQRCodeGenerator';
-import SocialMedia from './components/SocialMedia';
+import SocialMedia from './components/SocialMedia';
import BulkQRCode from './components/BulkQRCode';
import QRScanner from './components/QRScanner';
import PdfQRCodeGenerator from './components/PDFToQR';
import { ThemeProvider } from './context/ThemeContext';
-import { Toaster } from 'react-hot-toast';
+import ProtectedRoute from './components/ProtectedRoute';
export default function App() {
return (
-
-
-
- } />
- } />
- } />
- } />
- } />
- } />
- } />
- } />
- } />
- } />
- } />
- } />
- } />
-
-
-
+
+
+
+ } />
+ } />
+ } />
+ } />
+ } />
+ } />
+ } />
+ } />
+ } />
+ } />
+ } />
+ } />
+ } />
+ {/* Remove the line for Verify */}
+ {/* } /> */}
+
+
);
diff --git a/src/components/Login.js b/src/components/Login.js
index c3d7b78..4dabe72 100644
--- a/src/components/Login.js
+++ b/src/components/Login.js
@@ -1,36 +1,56 @@
+
+
import React, { useState } from 'react';
import { signInWithEmailAndPassword, signInWithPopup, GoogleAuthProvider } from "firebase/auth";
import { auth } from '../firebase';
import { useNavigate } from 'react-router-dom';
import { FcGoogle } from "react-icons/fc";
import { motion } from 'framer-motion';
-import toast from 'react-hot-toast';
+import { useAuth } from "../context/AuthContext";
export default function Login() {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const navigate = useNavigate();
+ const { setCurrentUser } = useAuth(); // Get setCurrentUser from Auth context
const handleSubmit = async (e) => {
e.preventDefault();
try {
- await signInWithEmailAndPassword(auth, email, password);
- navigate('/dashboard');
- toast.success("Login Successful");
+ const userCredential = await signInWithEmailAndPassword(auth, email, password);
+ const user = userCredential.user;
+
+ // Set currentUser from context
+ setCurrentUser(user);
+
+ // Navigate only if user is verified
+ if (user.emailVerified) {
+ navigate('/dashboard');
+ } else {
+ alert('Please verify your email before logging in.');
+ }
} catch (error) {
- toast.error('Something went wrong');
- console.error(error.message);
+ alert(error.message);
}
};
const handleGoogleLogin = async () => {
const provider = new GoogleAuthProvider();
try {
- await signInWithPopup(auth, provider);
- navigate('/dashboard');
+ const userCredential = await signInWithPopup(auth, provider);
+ const user = userCredential.user;
+
+ // Set currentUser from context
+ setCurrentUser(user);
+
+ // Navigate only if user is verified
+ if (user.emailVerified) {
+ navigate('/dashboard');
+ } else {
+ alert('Please verify your email before logging in.');
+ }
} catch (error) {
- toast.error('Something went wrong');
- console.error(error.message);
+ alert(error.message);
}
};
diff --git a/src/components/Navbar.js b/src/components/Navbar.js
index a10d93e..f8bcf1a 100644
--- a/src/components/Navbar.js
+++ b/src/components/Navbar.js
@@ -1,8 +1,8 @@
+
import React, { useState, useEffect, useRef, useContext } from "react";
import { useAuth } from "../context/AuthContext";
import { Link, useNavigate } from "react-router-dom";
import { ThemeContext } from "../context/ThemeContext";
-import toast from "react-hot-toast";
import {
FaUser,
@@ -22,69 +22,17 @@ export default function Navbar() {
const navigate = useNavigate();
const [dropdownOpen, setDropdownOpen] = useState(false);
const dropdownRef = useRef(null);
-
const { darkMode, setDarkMode } = useContext(ThemeContext);
const handleLogout = async () => {
- // Show a confirmation toast
- const confirmation = toast(
- (t) => (
-
-
Are you sure you want to log out?
-
-
-
-
-
- ),
- {
- duration: 0, // Keep the toast open until dismissed
- position: 'top-center', // Adjust position if needed
- }
- )
- };
-
- const handleToggleTheme = () => {
- setDarkMode((prevMode) => {
- const newMode = !prevMode;
-
- // Show toast notification when theme changes
- toast(`Theme changed to ${newMode ? 'Dark' : 'Light'} mode!`, {
- icon: `${newMode ? '🌙' :'☀️'}`,
- style: {
- borderRadius: '10px',
- background: `${newMode ? '#333' : '#fff'} `,
- color: `${newMode ? '#fff' : '#333'}`,
- },
- });
-
- return newMode;
- });
+ try {
+ await logout();
+ navigate("/"); // Redirect to the home page after logout
+ } catch (error) {
+ console.error("Failed to log out", error);
+ }
};
-
const toggleDropdown = () => {
setDropdownOpen(!dropdownOpen);
};
@@ -105,10 +53,11 @@ export default function Navbar() {
return (