Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/dashboard/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use client";

import withAdmin from "@/lib/withAdmin";

function DashboardPage() {
return <p>Welcome to the admin dashboard!</p>;
}

export default withAdmin(DashboardPage);
3 changes: 2 additions & 1 deletion app/db/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { getFirestore } from "firebase/firestore";
import { getAuth } from "firebase/auth";
import fireapp from "@/app/firebase.config";

// Get Firestore instance
const db = getFirestore(fireapp);

export const auth = getAuth(fireapp);
export { db };
1 change: 1 addition & 0 deletions app/firebase.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ const firebaseConfig = {
const fireapp = initializeApp(firebaseConfig);

export default fireapp;

21 changes: 0 additions & 21 deletions app/globals.css
Original file line number Diff line number Diff line change
@@ -1,21 +0,0 @@
@tailwind base;
@tailwind components;
@tailwind utilities;

:root {
--background: #ffffff;
--foreground: #171717;
}

@media (prefers-color-scheme: dark) {
:root {
--background: #0a0a0a;
--foreground: #ededed;
}
}

body {
color: var(--foreground);
background: var(--background);
font-family: Arial, Helvetica, sans-serif;
}
30 changes: 7 additions & 23 deletions app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,17 @@
import type { Metadata } from "next";
import { Geist, Geist_Mono } from "next/font/google";
"use client";
import React from 'react'
import { SessionProvider } from "next-auth/react";
import "./globals.css";

const geistSans = Geist({
variable: "--font-geist-sans",
subsets: ["latin"],
});

const geistMono = Geist_Mono({
variable: "--font-geist-mono",
subsets: ["latin"],
});

export const metadata: Metadata = {
title: "Create Next App",
description: "Generated by create next app",
};

export default function RootLayout({
children,
}: Readonly<{
}: {
children: React.ReactNode;
}>) {
}) {
return (
<html lang="en">
<body
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
>
{children}
<body>
<SessionProvider>{children}</SessionProvider>
</body>
</html>
);
Expand Down
29 changes: 29 additions & 0 deletions app/login/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use client";

import { signIn, signOut, useSession } from "next-auth/react";
import { useRouter } from "next/navigation";

export default function LoginPage() {
const { data: session, status } = useSession();
console.log(session);
console.log(status);
const router = useRouter();

if (status === "authenticated") {
router.push("/dashboard");
return null;
}

return (
<div>
{status === "loading" ? (
<p>Loading...=</p>
) : (
<>
<p>Please log in:</p>
<button onClick={() => signIn("google")}>Sign in with Google</button>
</>
)}
</div>
);
}
101 changes: 0 additions & 101 deletions app/page.tsx

This file was deleted.

57 changes: 57 additions & 0 deletions lib/withAdmin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"use client";

import { useEffect, useState } from "react";
import { db } from "@/app/db";
import { collection, query, where, doc, getDoc } from "firebase/firestore";
import { useSession } from "next-auth/react";
import { useRouter } from "next/navigation";

export default function withAdmin(Component: React.FC) {

return function AdminProtectedPage(props: any) {
const { data: session, status } = useSession();
const [isAdmin, setIsAdmin] = useState(false);
const [loading, setLoading] = useState(true);
const router = useRouter();

useEffect(() => {
const checkAdmin = async () => {
if (status === "authenticated" && session!==null) {
const userEmail = session.user?.email;

if (userEmail) {
try {
const adminQuery =doc(db,"admin",userEmail);

const querySnapshot = await getDoc(adminQuery);
if (querySnapshot.exists()) {
setIsAdmin(true);
} else {
console.log(querySnapshot);
alert("You do not have admin access.");
router.push("/login");
}
} catch (error) {
console.error("Error checking admin access:", error);
router.push("/login");
}
} else {
router.push("/login");
}
} else if (status === "unauthenticated") {
router.push("/login");
}

setLoading(false);
};

checkAdmin();
}, [session, status, router]);

if (loading) {
return <p>Loading...</p>;
}

return isAdmin ? <Component {...props} /> : null;
};
}
Loading