Skip to content

Unable to persist to database within beforeSessionSaved #2049

Open
@dextermb

Description

@dextermb

Checklist

Description

Hi all;

During the beforeSessionSaved callback I want to persist some user information to my database using the Drizzle ORM. Unfortunately it seems like Auth0 is overwriting or modifying the URL global object as when I try to execute the code below I get an error saying “TypeError: URL is not a constructor”, however executing database queries anywhere else in the app seems to work fine.

export const auth0 = new Auth0Client({
  beforeSessionSaved: async (session: SessionData) => {
    const { sub, username, email, name, picture } = session.user
    const attributes = {
      id: sub,
      avatar: picture,
      username,
      email,
      name
    }

    await db.insert(users)
      .values(attributes)
      .onDuplicateKeyUpdate({
        set: omit(attributes, ['id'])
      })

    return session
  },
  authorizationParameters: {
    scope: "openid profile email",
  },
})

What’s the recommended approach here so that I don’t clash with the mutated URL object?

Thanks in advance

Reproduction

  1. Create a basic MySQL Drizzle set up
  2. Attempt to insert into a table during beforeSessionSaved
  3. See NextJS error pointing to Drizzle for a URL object error

Additional context

No response

nextjs-auth0 version

4.4.1

Next.js version

15.2.2

Node.js version

18.18.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions