From b852018f24e9c2f84f577e6147ca651dae62a065 Mon Sep 17 00:00:00 2001 From: Shanmuga Krishnan S M Date: Thu, 6 Aug 2026 13:39:08 +0530 Subject: [PATCH] feat(auth): protect Collab, Community Q&A, and Leetcode routes behind Google Sign-In --- src/App.tsx | 40 +++++++++- src/components/Auth/ProtectedRoute.tsx | 100 +++++++++++++++++++++++++ 2 files changed, 137 insertions(+), 3 deletions(-) create mode 100644 src/components/Auth/ProtectedRoute.tsx diff --git a/src/App.tsx b/src/App.tsx index c850ae9..19058ff 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -2,6 +2,7 @@ import { useEffect } from 'react'; import { BrowserRouter, Routes, Route, useLocation } from 'react-router-dom'; import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; import { AuthProvider } from '@/context/AuthContext'; +import ProtectedRoute from '@/components/Auth/ProtectedRoute'; import MainLayout from '@/layouts/MainLayout'; import Home from '@/pages/Home/Home'; import Notes from '@/pages/Notes/Notes'; @@ -48,9 +49,42 @@ function App() { } /> } /> } /> - } /> - } /> - } /> + + {/* Protected Routes — Require Google Sign-In */} + + + + } + /> + + + + } + /> + + + + } + /> diff --git a/src/components/Auth/ProtectedRoute.tsx b/src/components/Auth/ProtectedRoute.tsx new file mode 100644 index 0000000..6a61131 --- /dev/null +++ b/src/components/Auth/ProtectedRoute.tsx @@ -0,0 +1,100 @@ +import { type ReactNode } from 'react'; +import { motion } from 'framer-motion'; +import { ShieldAlert, LogIn, Sparkles, ShieldCheck } from 'lucide-react'; +import { useAuth } from '@/context/AuthContext'; + +interface ProtectedRouteProps { + children: ReactNode; + title: string; + description: string; + requireVerifiedStudent?: boolean; +} + +export default function ProtectedRoute({ + children, + title, + description, + requireVerifiedStudent = false, +}: ProtectedRouteProps) { + const { isAuthenticated, isVerifiedStudent, loginWithGoogle, user } = useAuth(); + + // If user is authenticated (and meets verified student criteria if required), render the actual page! + const isAllowed = isAuthenticated && (!requireVerifiedStudent || isVerifiedStudent); + + if (isAllowed) { + return <>{children}; + } + + return ( +
+ {/* Background ambient light */} +
+
+
+ + + {/* Lock / Shield Icon */} +
+ {requireVerifiedStudent ? ( + + ) : ( + + )} +
+ + {/* Feature Title */} +
+ Authentication Required +
+ +

+ {title} +

+ +

+ {!isAuthenticated ? ( + <> + {description} Sign in with your Google account to access this feature. + + ) : ( + <> + You are signed in as {user?.email}. This feature is restricted to verified students with an @ritchennai.edu.in college email. + + )} +

+ + {/* Action Button */} +
+ + + {!isAuthenticated ? 'Sign In with Google to Unlock' : 'Switch to @ritchennai.edu.in Account'} + + +

+ Sign-in is quick, secure, and preserves your student privacy. +

+
+
+
+ ); +}