diff --git a/app/(auth)/sign-in/page.jsx b/app/(auth)/sign-in/page.jsx index 8aa1b69..2c8d972 100644 --- a/app/(auth)/sign-in/page.jsx +++ b/app/(auth)/sign-in/page.jsx @@ -2,7 +2,8 @@ import { zodResolver } from "@hookform/resolvers/zod"; import { useForm } from "react-hook-form"; -import { signIn } from "next-auth/react"; +import * as z from "zod"; +import { signIn, useSession } from "next-auth/react"; import { Form, FormField, @@ -15,7 +16,7 @@ import { Input } from "../../../@/components/ui/input"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { signInSchema } from "../../../model/Schema/signInSchema"; -import React, { useState } from "react"; +import React, { useState, useEffect } from "react"; import { Card, CardContent, @@ -52,9 +53,12 @@ export default function SignInForm() { redirect: false, }); console.log(response); - if (!response.ok) throw new Error(); - window.location.href = response?.url || "/tournaments"; - toast.success("Successfull Signup"); + if (response?.error) { + toast.error(response.error); + } else { + router.push("/dashboard"); + toast.success("Successfull Signup"); + } } catch (error) { toast.error("Username / Password mismatched"); } finally { @@ -63,12 +67,19 @@ export default function SignInForm() { }; const handleGoogleSignIn = async () => { - await signIn("google", { callbackUrl: "/" }); + await signIn("google", { callbackUrl: "/dashboard" }); }; const handleDiscordSignIn = async () => { - await signIn("discord", { callbackUrl: "/" }); + await signIn("discord", { callbackUrl: "/dashboard" }); }; + const { data: session, status } = useSession(); + + useEffect(() => { + if (status === "authenticated") { + router.push("/dashboard"); + } + }, [status, router]); return (
diff --git a/app/api/dashboard/route.js b/app/api/dashboard/route.js new file mode 100644 index 0000000..fa06505 --- /dev/null +++ b/app/api/dashboard/route.js @@ -0,0 +1,112 @@ +import { getServerSession } from "next-auth/next"; +import { authOptions } from "../auth/[...nextauth]/route"; +import { NextResponse } from "next/server"; +import dbConnect from "../../../lib/dbConnect"; +import Tournament from "../../../model/Tournament"; +import Team from "../../../model/Team"; +import Bracket from "../../../model/Bracket"; + +export async function GET(request) { + try { + const session = await getServerSession(authOptions); + + if (!session) { + return NextResponse.json({ + participatedTournaments: [], + upcomingTournaments: [], + userTeams: [], + userBrackets: [], + }); + } + + await dbConnect(); + + const testData = { + participatedTournaments: [ + { + _id: "T1", + tournamentName: "Tournament 1", + game: "BGMI", + startDate: new Date(), + status: "ongoing", + }, + { + _id: "T2", + tournamentName: "Valo Tournament", + game: "Valorent", + startDate: new Date(), + status: "completed", + }, + ], + upcomingTournaments: [ + { + _id: "UT1", + tournamentName: "Tournament 3", + game: "Valorent", + registrationEndDate: new Date(Date.now()), + }, + { + _id: "UT2", + tournamentName: "Tournament 4", + game: "Apex Legends", + registrationEndDate: new Date(Date.now() + 7 * 1000 * 86400), + }, + ], + userTeams: [ + { + _id: "team 1", + teamName: "Team 1", + members: ["pro", "exp", "a32"], + }, + { + _id: "team 2", + teamName: "Team 2", + members: ["dragon", "vince", "strange"], + }, + ], + userBrackets: [ + { + _id: "B1", + name: "bgmi bracket", + rounds: 3, + teams: 9, + }, + { + _id: "B2", + name: "valorent bracket", + rounds: 4, + teams: 15, + }, + ], + }; + return NextResponse.json(testData); + + /* + const [participatedTournaments, upcomingTournaments, userTeams, userBrackets] = await Promise.all([ + Tournament.find({ "teamsRegistered.members": session.user.email }).lean(), + Tournament.find({ registrationEndDate: { $gt: new Date() } }).limit(5).lean(), + Team.find({ members: session.user.email }).lean(), + Bracket.find({ createdBy: session.user.email }).lean() + ]); + + return NextResponse.json({ + participatedTournaments, + upcomingTournaments, + userTeams, + userBrackets + }); + */ + } catch (error) { + console.log("API Error:", error); + return NextResponse.json( + /*{ + participatedTournaments: [], + upcomingTournaments: [], + userTeams: [], + userBrackets: [] + }*/ + { error: "Internal Server Error" }, + { status: 500 }, + ); + } +} diff --git a/app/dashboard/page.jsx b/app/dashboard/page.jsx new file mode 100644 index 0000000..2592b87 --- /dev/null +++ b/app/dashboard/page.jsx @@ -0,0 +1,215 @@ +"use client"; +import { useState, useEffect } from "react"; +import { useSession } from "next-auth/react"; +import { useRouter } from "next/navigation"; +import { PacmanLoader } from "react-spinners"; +import { + Card, + CardContent, + CardHeader, + CardTitle, +} from "../../@/components/ui/card"; +import Link from "next/link"; +import { CalendarDays, Users, Trophy, Brackets } from "lucide-react"; + +export default function Dashboard() { + const { data: session, status } = useSession(); + const router = useRouter(); + const [isLoading, setIsLoading] = useState(true); + const [userData, setUserData] = useState({ + participatedTournaments: [], + upcomingTournaments: [], + userTeams: [], + userBrackets: [], + }); + + useEffect(() => { + //console.log("Session:", session); + if (status === "unauthenticated") { + router.push("/sign-in"); + } + + if (status === "authenticated" && session?.user) { + fetchDashboardData(); + } + }, [status, session, router]); + + const fetchDashboardData = async () => { + try { + const response = await fetch("/api/dashboard", { + method: "GET", + headers: { + "Content-Type": "application/json", + }, + credentials: "same-origin", + }); + + const data = await response.json(); + + if (data.error) { + console.log("API Error:", data.error); + setUserData({ + participatedTournaments: [], + upcomingTournaments: [], + userTeams: [], + userBrackets: [], + }); + return; + } + + setUserData(data); + } catch (error) { + console.log("Fetch Error:", error); + setUserData({ + participatedTournaments: [], + upcomingTournaments: [], + userTeams: [], + userBrackets: [], + }); + } finally { + setIsLoading(false); + } + }; + + if (status === "loading" || isLoading) { + return ( +
+ +
+ ); + } + + if (!session) { + return null; + } + + return ( +
+
+

+ Welcome back, {session.user.username} +

+ +
+ {/* Participated Tournaments */} + + + + + Participated Tournaments + + + + {userData?.participatedTournaments.map((tournament) => ( +
+

+ {tournament.tournamentName} +

+

+ Game: {tournament.game} +

+ + View Details + +
+ ))} +
+
+ + {/* Upcoming Tournaments */} + + + + + Upcoming Tournaments + + + + {userData?.upcomingTournaments.map((tournament) => ( +
+

+ {tournament.tournamentName} +

+

+ Registration Ends:{" "} + {new Date( + tournament.registrationEndDate, + ).toLocaleDateString()} +

+ + Register Now + +
+ ))} +
+
+ + {/* User Teams */} + + + + My Teams + + + {userData?.userTeams.map((team) => ( +
+

{team.teamName}

+

+ Members: {team.members.length} +

+ + View Team + +
+ ))} +
+
+ + {/* Brackets */} + + + + + My Brackets + + + + {userData?.userBrackets.map((bracket) => ( +
+

{bracket.name}

+ + View Bracket + +
+ ))} +
+
+
+
+
+ ); +}