diff --git a/app/api/event1/getTeamCode/route.js b/app/api/event1/getTeamCode/route.js index f0eb994..74c666a 100644 --- a/app/api/event1/getTeamCode/route.js +++ b/app/api/event1/getTeamCode/route.js @@ -13,7 +13,7 @@ export async function GET(req, { params }) { const token = await getToken({req}) const auth = token ? token.accessTokenFromBackend : null let userId = await getTokenDetails(auth); - + console.log("rrrrrrfrrrrrr",userId); // console.log(userId); const team = await Event1.findOne({ teamLeaderId: userId }); if (!team) { diff --git a/app/api/event1/getTeamData/route.js b/app/api/event1/getTeamData/route.js index 8e20047..9a901e7 100644 --- a/app/api/event1/getTeamData/route.js +++ b/app/api/event1/getTeamData/route.js @@ -27,7 +27,7 @@ export async function GET(req) { if (!team) { return NextResponse.json({ message: 'Team is not there ' }); } - + return NextResponse.json({ message: 'Team Details sent. ', status: 200, diff --git a/app/api/event1/getTeamViaToken/route.js b/app/api/event1/getTeamViaToken/route.js index bf9bbd3..65747c8 100644 --- a/app/api/event1/getTeamViaToken/route.js +++ b/app/api/event1/getTeamViaToken/route.js @@ -7,14 +7,13 @@ import { NextResponse } from "next/server"; export async function POST(req) { try { await connectMongoDB(); - const { code } = await req.json(); - console.log(code); - const team = await event1TeamToken.findOne({ teamCode: code }); - const teamDetails = await Event1.findById(team.teamId); - + const { teamCode } = await req.json(); + console.log(teamCode); + const team = await event1TeamToken.findOne({ token: teamCode }); if (!team) { return NextResponse.json({ error: "Team not found" }); } + const teamDetails = await Event1.findById(team.teamId); return NextResponse.json({ message: "Team Details sent. ", diff --git a/app/api/event1/joinTeam/route.js b/app/api/event1/joinTeam/route.js index c4351a6..0f8c955 100644 --- a/app/api/event1/joinTeam/route.js +++ b/app/api/event1/joinTeam/route.js @@ -8,13 +8,15 @@ import { NextResponse } from 'next/server'; export async function POST(req, { params }) { try { + console.log("++++++++++++++++++++++++++++++++++++++++11111122334"); await connectMongoDB(); const token = await getToken({ req }); + console.log("@@@@@@@@@@@",token) const auth = token ? token.accessTokenFromBackend : null; let userId = await getTokenDetails(auth); - console.log(userId); + const user = await Users.findById({ _id: userId }); console.log(user); @@ -24,9 +26,12 @@ export async function POST(req, { params }) { }); } - const { code } = await req.json(); - console.log(code); - const team = await Event1.findOne({ teamCode: code }); + console.log("$$$$$$$$$$$$$$$$$$") + + const { teamCode } = await req.json(); + console.log('==========',teamCode); + const team = await Event1.findOne({ teamCode: teamCode }); + console.log("+++++",team); //check if user is not a part of any team if (!team) { return NextResponse.json({ error: 'Team not found' }); @@ -36,31 +41,35 @@ export async function POST(req, { params }) { } // console.log(team) const Event1TeamToken = await event1TeamToken.findOne({ teamId: team._id }); - + console.log("&&&&&&&",Event1TeamToken) if (!Event1TeamToken) { return res.status(404).json({ error: 'Token not found' }); } - const currentTime = new Date(); - const tokenCreationTime = token.createdAt; + console.log("*******") + // const currentTime = new Date(); + // const tokenCreationTime = token.createdAt; - const timeDifference = - (currentTime - tokenCreationTime) / (1000 * 60); // Difference in minutes + // const timeDifference = + // (currentTime - tokenCreationTime) / (1000 * 60); // Difference in minutes //have to change this - if (timeDifference > 1000000000) { - // Token expired, prompt for a new token - return NextResponse.json({ - error: 'Token expired. Ask leader to generate a new token.', - }); - } - if (code !== token.token) { + + // if (timeDifference > 1000000000) { + // // Token expired, prompt for a new token + // return NextResponse.json({ + // error: 'Token expired. Ask leader to generate a new token.', + // }); + // } + if (teamCode !== Event1TeamToken.token) { return NextResponse.json({ error: 'Incorrect token' }); } + console.log("workss") await Users.findOneAndUpdate( { _id: userId }, { $set: { event1TeamId: team.id, event1TeamRole: 1 } } ); + console.log("---------",team._id) await Event1.findOneAndUpdate( { diff --git a/app/api/event1/leaveTeam/route.js b/app/api/event1/leaveTeam/route.js index 75fdc98..68cc44f 100644 --- a/app/api/event1/leaveTeam/route.js +++ b/app/api/event1/leaveTeam/route.js @@ -1,57 +1,58 @@ -import { connectMongoDB } from "@/lib/mongodb"; -import { TeamModel } from "@/models/TeamDetails"; -import { Users } from "@/models/user.model"; -import { NextResponse } from "next/server"; +import { connectMongoDB } from '@/lib/mongodb'; +import { Event1 } from '@/models/event1.model'; +import { Users } from '@/models/user.model'; +import { NextResponse } from 'next/server'; -import { getToken } from "next-auth/jwt"; -import { getTokenDetails } from "../../../../utils/authuser"; +import { getToken } from 'next-auth/jwt'; +import { getTokenDetails } from '../../../../utils/authuser'; - -export async function POST(req){ - try{ +export async function POST(req) { + try { await connectMongoDB(); - const token = await getToken({req}) - const auth = token ? token.accessTokenFromBackend : null + const token = await getToken({ req }); + const auth = token ? token.accessTokenFromBackend : null; let userId = await getTokenDetails(auth); console.log(userId); const user = await Users.findById(userId); - if (user.event1TeamRole != "1") { + if (user.event1TeamRole != 1) { return NextResponse.json({ - message: "Leader cant leave the team", + message: 'Leader cant leave the team', }); } if (!user.event1TeamId) { return NextResponse.json({ - message: "User is not part of any team", + message: 'User is not part of any team', }); } - const team = await TeamModel.findById(user.event1TeamId); + const team = await Event1.findById(user.event1TeamId); if (!team) { return NextResponse.json({ - message: "Team not found", + message: 'Team not found', }); } team.members.pull(userId); await team.save(); - await Users.findByIdAndUpdate(userId, { $set: { teamId: null, teamRole:-1 } }); - - - - - return NextResponse.json({ message: "User has left the team successfully ", status: 200, teamDetails: team }); - - - - -}catch(error) { - console.error("An error occurred:", error); - return NextResponse.json({ message: "Error occurred ", status: 500 }); + await Users.findByIdAndUpdate(userId, { + $set: { event1TeamId: null, event1TeamRole: -1 }, + }); + + return NextResponse.json({ + message: 'User has left the team successfully ', + status: 200, + teamDetails: team, + }); + } catch (error) { + console.error('An error occurred:', error); + return NextResponse.json({ + message: 'Error occurred ', + status: 500, + }); + } } -} \ No newline at end of file diff --git a/app/api/login/route.js b/app/api/login/route.js new file mode 100644 index 0000000..d75c46e --- /dev/null +++ b/app/api/login/route.js @@ -0,0 +1,42 @@ +import { connectMongoDB } from "@/lib/mongodb"; +import { Users } from "@/models/user.js"; +import { getTokenDetails } from "@/utils/authuser.js"; +import { getToken } from "next-auth/jwt"; +import { headers } from 'next/headers'; +import { NextResponse } from "next/server"; + + +export async function POST(req){ + try{ + await connectMongoDB(); + const headersList = headers() + // const authorization = headersList.get('authorization') + const token = await getToken({req}) + //console.log('ff', token) + console.log(token.accessTokenFromBackend); + // const auth = req.headers.get("authorization").split(' ')[1]; + + let userId = await getTokenDetails(token.accessTokenFromBackend); + console.log(userId); + const user=await Users.findById(userId); + console.log(user); + + const {regNo,mobno}=await req.json(); + + await Users.findByIdAndUpdate(userId,{$set:{regNo:regNo,mobno:mobno}}) + // console.log(newUserDetail); + // const { accessToken, refreshToken } = await generateTokens(newUserDetail); + // console.log(accessToken); + // console.log(refreshToken); + + //console.log(accessToken); + + return NextResponse.json({ message: "User Details entered ", status: 200 }); + + + }catch(error) { + console.error("An error occurred:", error); + return NextResponse.json({ message: "Error occurred ", status: 500 }); + } + +} \ No newline at end of file diff --git a/app/events/event1/joinTeam/page.jsx b/app/events/event1/joinTeam/page.jsx index 193f97e..cd2f774 100644 --- a/app/events/event1/joinTeam/page.jsx +++ b/app/events/event1/joinTeam/page.jsx @@ -1,6 +1,5 @@ 'use client' -import Navbar from '@/Components/Navbar'; import { useSession } from 'next-auth/react'; import { useRouter } from 'next/navigation'; import { useEffect, useState } from 'react'; @@ -49,7 +48,7 @@ const JoinTeam = ({ teamCode: propTeamCode }) => { if (user.hasFilledDetails == false) { router.push('/'); } else { - if (user.teamId !== null) { + if (user.event1TeamId !== null) { const redirect = user.teamRole=='1' ? '/events/event1/memberDash' : '/events/event1/leaderDash'; router.push(redirect); } @@ -158,7 +157,6 @@ const JoinTeam = ({ teamCode: propTeamCode }) => { return ( // tailwind
-

diff --git a/app/events/event1/leaderDash/page.jsx b/app/events/event1/leaderDash/page.jsx index 29530b2..46aec57 100644 --- a/app/events/event1/leaderDash/page.jsx +++ b/app/events/event1/leaderDash/page.jsx @@ -213,7 +213,7 @@ export default function LeaderDashboard() { name={el.name} Role={el.event1TeamRole === 0 ? 'Leader' : 'Member'} regNo={el.regNo} - phone={el.mobno} + phone={el.mobNo} leader={true} removeMember={() => { removeMember(el._id); diff --git a/app/events/event1/memberDash/page.jsx b/app/events/event1/memberDash/page.jsx index 3310118..5d85180 100644 --- a/app/events/event1/memberDash/page.jsx +++ b/app/events/event1/memberDash/page.jsx @@ -26,14 +26,12 @@ const TeamPage = () => { const router = useRouter(); useEffect(() => { - if (router.isReady) { - - if (status === "unauthenticated") { - router.push("/") - } else if(status === "authenticated"){ - getData() - fetchDataFromBackend(); - } + if (status === "unauthenticated") { + router.push("/") + } else if(status === "authenticated"){ + console.log('asdf') + getData() + fetchDataFromBackend(); } }, [status, router]) @@ -52,9 +50,10 @@ const TeamPage = () => { .then((data) => { const user = data.user; + console.log('user;;', user) if (user.hasFilledDetails == true) { if (user.event1TeamId !== null) { - // router.push("/"); + router.push("/"); if (user.event1TeamRole === '0') { router.push('/leaderDashboard') } else { @@ -72,7 +71,7 @@ const TeamPage = () => { const fetchDataFromBackend = () => { setIsLoading(true); - fetch('/team/getTeamData', { + fetch('/api/event1/getTeamData', { content: "application/json", method: "GET", headers: { @@ -98,7 +97,7 @@ const TeamPage = () => { const leaveTeam = () => { setIsLoading(true); - fetch( '/user/leaveTeam/'+teamId, { + fetch( '/api/event1/leaveTeam/', { content: "application/json", method: "POST", headers: { @@ -108,6 +107,7 @@ const TeamPage = () => { }, }).then(data=>data.json()) .then(data=>{ + console.log('data', data) if(data.error == false) { setIsLoading(true); router.push('/makeTeam') @@ -138,7 +138,7 @@ const TeamPage = () => {
{ teamMembersData.map(el=>{ - return + return }) }
diff --git a/components/Navbar.jsx b/components/Navbar.jsx index 8f10f8c..f3e7aee 100644 --- a/components/Navbar.jsx +++ b/components/Navbar.jsx @@ -1,29 +1,129 @@ "use client"; -import Link from "next/link"; +// import Link from "next/link"; import { signIn, signOut, useSession } from "next-auth/react"; -export default function Navbar() { - const { status } = useSession(); +// export default function Navbar() { +// const { status } = useSession(); +// return ( +//
+//
Nav
+// {status === "authenticated" ? ( +// +// ) : ( +// +// )} +//
+// ); +// } + +// import { useSession } from 'next-auth/react'; +import Image from "next/image"; +import Link from "next/link"; +import { useRouter } from "next/navigation"; +import logo from "@/public/assets/logos/FP LOGO.svg"; +import { useEffect, useState } from "react"; +import SignInBtn from "./SignInBtn"; +// import LoginButton from './Landing Page/Loginbutton'; + +const Navbar = () => { + const router = useRouter(); + const [isOpen, setIsOpen] = useState(false); + const [dashboardLink, setDashboardLink] = useState("/"); + const { data: session, status } = useSession(); + + const toggleMenu = () => { + setIsOpen(!isOpen); + }; + useEffect(() => { + if (router.isReady) { + if (status === "unauthenticated") { + router.push("/"); + } else if (status === "authenticated") { + getData(); + } + } + }, [status, router]); + + const getData = () => { + fetch(`${process.env.NEXT_PUBLIC_SERVER}/user/userDetails`, { + content: "application/json", + method: "GET", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${session.accessTokenBackend}`, + "Access-Control-Allow-Origin": "*", + }, + }) + .then((res) => res.json()) + .then((data) => { + const user = data.user; + if (user.teamRole === "0") { + setDashboardLink("/leaderDashboard"); + } else { + setDashboardLink("/memberDashboard"); + } + }) + .catch((error) => {}); + }; + return ( -
-
Nav
- {status === "authenticated" ? ( - <> - {window.location.pathname === "/events/event1/teamCode" && ( - - )} - - - ) : ( - - )} -
+ ); -} \ No newline at end of file +}; + +export default Navbar; diff --git a/components/SignInBtn.jsx b/components/SignInBtn.jsx index 1b1f844..63eec2d 100644 --- a/components/SignInBtn.jsx +++ b/components/SignInBtn.jsx @@ -1,18 +1,22 @@ "use client"; import Image from "next/image"; -import { signIn } from "next-auth/react"; +import { signIn,signOut,useSession } from "next-auth/react"; export default function SignInBtn() { + const { status } = useSession(); return ( - - ); +
+ {status === "authenticated" ? ( + + ) : ( + + )} +
) } \ No newline at end of file diff --git a/components/userDetails.jsx b/components/userDetails.jsx new file mode 100644 index 0000000..b81a9fe --- /dev/null +++ b/components/userDetails.jsx @@ -0,0 +1,103 @@ +"use client" +import { useState } from 'react'; +import { useSession } from 'next-auth/react'; +import { useRouter } from 'next/navigation'; + +export default function UserDetails() { + const { data: session, status } = useSession(); + + // use session chl rha h baaki frondend me data kese lena h aap log dekh lo + //console.log("fsdbhbsbvdfwad",data); + // console.log("fgfcgcgfdfdsds",session); + //console.log(data); + + const [firstName, setFirstName] = useState(''); + const [lastName, setLastName] = useState(''); + const [regNo, setRegNo] = useState(''); + const [mobno, setMobno] = useState(''); + // const router = useRouter(); + + const handleSubmit = async () => { + + console.log('button ') + + try { + const response = await fetch('http://localhost:3001/api/login', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + Authorization:`Bearer ${session.accessTokenBackend}`, + 'Access-Control-Allow-Origin':'*', + body: JSON.stringify({ + firstName, + lastName, + regNo, + mobno + }) + }); + + if (response.ok) { + console.log('Data saved successfully'); + + setFirstName(''); + setLastName(''); + setRegNo(''); + setMobno(''); + + // router.push('/') + + } else { + console.error('Failed to save data:', response.statusText); + } + } catch (error) { + console.error('Error saving data:', error); + } + }; + + return ( +
+
+ + setFirstName(e.target.value)} + style={{ color: 'black' }} + /> +
+
+ + setLastName(e.target.value)} + style={{ color: 'black' }} + /> +
+
+ + setRegNo(e.target.value)} + style={{ color: 'black' }} + /> +
+
+ + setMobno(e.target.value)} + style={{ color: 'black' }} + /> +
+ +
+ ); +} diff --git a/utils/authuser.js b/utils/authuser.js index 3da6957..ed80514 100644 --- a/utils/authuser.js +++ b/utils/authuser.js @@ -7,13 +7,15 @@ export async function getTokenDetails(token) { connectMongoDB(); console.log("fsdaghjgdshbfzhjdbvsjhbdfhjcbghjgfd") const tokenDetails = await jwtVerify( + token, new TextEncoder().encode(process.env.ACCESS_TOKEN_SECRET) ); + console.log("yha aa rha h") const userId = tokenDetails.payload._id; - // console.log(userId) + console.log("+++++++",userId) const user = await Users.findById(userId);