Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session is not updated after user email verifcation #11058

Open
Ali-Raza764 opened this issue Jun 2, 2024 · 1 comment
Open

Session is not updated after user email verifcation #11058

Ali-Raza764 opened this issue Jun 2, 2024 · 1 comment
Labels
bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

Comments

@Ali-Raza764
Copy link

Environment

System: OS: Windows 10 10.0.19045 CPU: (4) x64 Intel(R) Core(TM) i5-4200U CPU @ 1.60GHz Memory: 2.39 GB / 7.92 GB Binaries: Node: 21.5.0 - C:\Program Files\nodejs\node.EXE npm: 10.2.5 - C:\Program Files\nodejs\npm.CMD Browsers: Edge: Chromium (123.0.2420.97) Internet Explorer: 11.0.19041.4355 npmPackages: next: 14.2.3 => 14.2.3 react: ^18 => 18.3.1

Reproduction URL

https://github.com/Ali-Raza764/next-auth-tookit

Describe the issue

I have tried using the unstable_update and to customly rewrite the session after the email verification at the route but it does not
seem to work
`import { auth, unstable_update } from "@/auth"; // Correctly import unstable_update
import User from "@/utils/db/models/user.model";
import { NextResponse } from "next/server";
import dbConnect from "@/utils/db/dbConnect";

export async function GET(request) {
try {
const { searchParams } = new URL(request.url);
const token = searchParams.get('token');
console.log("token", token);

const session = await auth();
if (!session) {
  return NextResponse.json({ message: "Unauthorized" }, { status: 401 });
}

await dbConnect();

const userId = session.user.id;
const user = await User.findById(userId);

if (!user) {
  return NextResponse.json({ message: "User not found" }, { status: 404 });
}

const currentTime = new Date().getTime();

if (token === user.verificationToken && currentTime < user.verificationTokenExpiry) {
  user.isVerified = true;
  await user.save();

  // Update the session and token with the new isVerified status
  await unstable_update({ ...session, user: { ...session.user, isVerified: true } });

  console.log("Route Session", session.user);

  return NextResponse.json({ message: "Verification successful" });
}

return NextResponse.json({ message: "Verification failed" }, { status: 400 });

} catch (error) {
console.error("Error verifying token:", error);
return NextResponse.json({ message: "Internal Server Error" }, { status: 500 });
}
}`

here is my protected route
`import { auth } from "@/auth";
import React from "react";
import SignOutButton from "../auth/components/SignOut";
import { redirect } from "next/navigation";

const Protected = async () => {
const session = await auth();
console.log(session);
if (!session.user.isVerified) {
redirect("/auth/verify");
}
return (


{JSON.stringify(session)}


);
};

export default Protected; `

How to reproduce

  1. Clone the repo
  2. Install dependencies npm install
  3. npm run dev

Expected behavior

The session should get updated and I should be able to access the protected route.

@Ali-Raza764 Ali-Raza764 added bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. labels Jun 2, 2024
@Ali-Raza764
Copy link
Author

await unstable_update({ user: { ...session.user, isVerified: true } });
Also tried this way but does not seem to work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
Projects
None yet
Development

No branches or pull requests

1 participant