so like, when i login the first time, it works perfectly, but when i login again with different gmail, the name email and image displayed at dashboard page is not updated,
i asked gpt,gemini etc, but they werent helpful at all, cookies is getting updating properly when logging out, i checked it multiple times
here is the video of the problem:
here are some useful codes and debugging that i tried myself
this is the output of api/users/dashboard
debuggin output:
userId: 66d027dc93164109b87574cb
user: {
_id: new ObjectId('66cf366a27efefd2c067d5fc'),
name: 'aman_5909',
email: '[email protected]',
image: 'https://www.shutterstock.com/image-vector/default-avatar-profile-vector-user-260nw-1705357234.jpg',
isVerified: true,
createdAt: 2024-08-28T14:38:34.491Z,
updatedAt: 2024-08-28T14:38:58.223Z,
__v: 0
}
userId is the right one
but user is the wrong one
import { dbconnect } from "@/utils/dbconnect";
import { NextResponse } from "next/server";
dbconnect();
export async function POST(req) {
try {
// Create the response indicating successful logout
const response = NextResponse.json({ message: "User logged out successfully" }, { status: 200 });
// Set the access_token cookie to an empty string and expire it immediately
response.cookies.set("access_token", "", {
httpOnly: true,
path: "/", // Ensure this path matches where the cookie was originally set
sameSite: "strict",
expires: new Date(0) // Expire the cookie immediately
});
console.log("User logged out successfully");
return response;
} catch (error) {
console.error("Error in logout/route.js: ", error);
return NextResponse.json({ error: "Something went wrong in logout/route.js" }, { status: 500 });
}
}
import { getDataFromToken } from "@/helpers/getDataFromToken";
import User from "@/models/User";
import { dbconnect } from "@/utils/dbconnect";
import { NextResponse } from "next/server";
dbconnect();
export async function POST(req) {
try {
const userId = await getDataFromToken(req);
console.log("userId:", userId);
const user = await User.findOne({userId}).select("-password");
console.log("user:", user);
return NextResponse.json({ message: "User fetched successfully" , user });
} catch (error) {
return NextResponse.json({ error: "Something went wrong in dashboard/route.js" }, { status: 500 });
}
}
import { dbconnect } from "@/utils/dbconnect";
import { NextResponse } from "next/server";
import User from "@/models/User";
import bcryptjs from "bcryptjs";
import jwt from "jsonwebtoken";
dbconnect();
export async function POST(req) {
try {
const body = await req.json(); // <-- Properly await the json() method
const { email, password } = body;
console.log("email:", email, "password:", password);
const existingUser = await User.findOne({ email });
if (!existingUser) {
console.log("User does not exist");
return NextResponse.json({ error: "Invalid email or password" }, { status: 400 });
}
const isPasswordCorrect = await bcryptjs.compare(password, existingUser.password);
if (!isPasswordCorrect) {
console.log("isPasswordCorrect:", isPasswordCorrect);
return NextResponse.json({ error: "Invalid email or password" }, { status: 400 });
}
const token = jwt.sign({ email: existingUser.email, id: existingUser._id, name: existingUser.name }, "test", { expiresIn: "1h" });
console.log("token:", token);
const response = NextResponse.json({ result: { email: existingUser.email } }, { status: 200 });
console.log("email:", existingUser.email);
response.cookies.set("access_token", token, {
httpOnly: true,
sameSite: "strict",
path: "/"
});
return response;
} catch (error) {
console.log("error in login/route.js: ", error);
return NextResponse.json({ error: "Something went wrong in login/route.js" }, { status: 500 });
}
}
import jwt from "jsonwebtoken";
export const getDataFromToken = (req) => {
try {
const token = req.cookies.get("access_token")?.value || "";
console.log("token:", token);
if (!token) {
throw new Error("Token not found");
}
const decodedToken = jwt.verify(token, "test");
console.log("decodedToken:", decodedToken.id);
return decodedToken.id;
} catch (error) {
console.error("An error occurred in getDataFromToken:", error.message);
return null; // Or throw an error, depending on how you want to handle it
}
}
I have mentioned what i tried and what i was expecting above