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