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

User in Middleware Has Only an Email #9951

Closed
AmphibianDev opened this issue Feb 7, 2024 · 2 comments
Closed

User in Middleware Has Only an Email #9951

AmphibianDev opened this issue Feb 7, 2024 · 2 comments
Labels
bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

Comments

@AmphibianDev
Copy link

AmphibianDev commented Feb 7, 2024

Environment

System:
OS: Windows 11 10.0.22631
CPU: (24) x64 AMD Ryzen 9 3900X 12-Core Processor
Memory: 29.06 GB / 47.93 GB
Binaries:
Node: 20.11.0 - C:\Program Files\nodejs\node.EXE
npm: 10.2.4 - C:\Program Files\nodejs\npm.CMD
Browsers:
Edge: Chromium (121.0.2277.106)
Internet Explorer: 11.0.22621.1
npmPackages:
@auth/prisma-adapter: ^1.2.1 => 1.2.1
next: 14.1.0 => 14.1.0
next-auth: ^5.0.0-beta.5 => 5.0.0-beta.5
react: ^18 => 18.2.0

Reproduction URL

https://github.com/AmphibianDev/todo-app

Describe the issue

When I access the user req.auth?.user in the middleware, I get intellisense for the user values, but every one of them is undefined except the email.

How to reproduce

middleware.ts

import NextAuth from "next-auth";
import authConfig from "@/auth.config";
const { auth } = NextAuth(authConfig);

export default auth((req) => {
  console.log(req.auth?.user); // => { email: 'myemail@gmail.com' }
});

export const config = {
  matcher: ["/((?!.+\\.[\\w]+$|_next).*)", "/", "/(api|trpc)(.*)"],
};

auth.ts

import NextAuth, { type DefaultSession } from "next-auth";
import authConfig from "@/auth.config";
import prisma from "@/lib/prisma";
import { PrismaAdapter } from "@auth/prisma-adapter";
import { User as PrismaUser, UserRole } from "@prisma/client";
import { JWT } from "next-auth/jwt";

declare module "next-auth/jwt" {
  interface JWT {
    idToken?: string;
    role: UserRole;
  }
}

declare module "next-auth" {
  interface User extends PrismaUser {}
}

export const {
  handlers: { GET, POST },
  auth,
  signIn,
  signOut,
} = NextAuth({
  pages: {
    signIn: "/auth/login",
    error: "/auth/error",
  },
  callbacks: {
    async signIn({ user }) {
      if (!user.loginVerified || user.loginVerified < new Date()) return false;

      await prisma.user.update({
        where: { id: user.id },
        data: { loginVerified: null, verifyRequestId: null },
      });

      return true;
    },
    async session({ session, token }) {
      if (token.sub && session.user) {
        session.user.id = token.sub;
        session.user.role = token.role;
      }

      return session;
    },
    async jwt({ token }) {
      if (!token.sub) return token;

      const user = await prisma.user.findUnique({
        where: { id: token.sub },
      });

      if (!user) return token;

      token.role = user.role;
      return token;
    },
  },
  adapter: PrismaAdapter(prisma),
  ...authConfig,
});

Expected behavior

I should get all the values for the user, not only the email.

@AmphibianDev AmphibianDev added bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. labels Feb 7, 2024
@ConnorC18
Copy link

@AmphibianDev

Give this a go #9836 (comment)

@AmphibianDev
Copy link
Author

Moved to #9836.

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

2 participants