workos / authkit-nextjs

The WorkOS library for Next.js provides convenient helpers for authentication and session management using WorkOS & AuthKit with Next.js.
MIT License
44 stars 9 forks source link

Unable to use `getUser` #6

Closed nathanburgess closed 2 months ago

nathanburgess commented 3 months ago
 ⨯ ../../node_modules/@workos-inc/nextjs/dist/cjs/impersonation.js
Module parse failed: 'import' and 'export' may appear only with 'sourceType: module' (1:106)
> /* __next_internal_action_entry_do_not_use__ {"664c45540e4ace7aaf888ba79a1284ededd41d72":"$$ACTION_1"} */ import { createActionProxy } from "private-next-rsc-action-proxy";
| import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption";
| "use strict";

Import trace for requested module:
../../node_modules/@workos-inc/nextjs/dist/cjs/impersonation.js
../../node_modules/@workos-inc/nextjs/dist/cjs/index.js
./src/app/page.tsx

I'm seeing this error when attempting to use getUser

import { getUser } from '@workos-inc/nextjs';

export default async function Page() {
  const { user } = await getUser();
  console.log(user);

  return (
    <div>test</div>
  );
}
benoitgrelard commented 3 months ago

Strange, which version of Next are you using?

nathanburgess commented 3 months ago

Strange, which version of Next are you using?

14.0.4

benoitgrelard commented 3 months ago

Are you able to to push a reproduction repo somewhere?

nathanburgess commented 3 months ago

https://github.com/nathanburgess/workos-bug-repo

This is using Next 14.0.4 in Turborepo

palmithor commented 3 months ago

This is affecting me as well.

benoitgrelard commented 2 months ago

@nathanburgess I haven't found a way to run your repository unfortunately.

I wonder, is it specific to Turborepo? or this version of next? etc.

It would help to isolate what the issue is about.

@palmithor Are you able to provide some info on your setup too to see if it matches some of these above?

Thanks! 🙏

nathanburgess commented 2 months ago

@benoitgrelard

  1. git clone https://github.com/nathanburgess/workos-bug-repo
  2. pnpm i
  3. Add the necessary WorkOS env vars to apps/web/.env
  4. pnpm dev

That'll get the repro running and you'll see the output below printed to the terminal:

web:dev:
web:dev:    ▲ Next.js 14.0.4
web:dev:    - Local:        http://localhost:3000
web:dev:    - Environments: .env
web:dev:
web:dev:  ✓ Ready in 1553ms
web:dev:  ✓ Compiled /not-found in 308ms (398 modules)
web:dev:  ⚠ Fast Refresh had to perform a full reload due to a runtime error.
web:dev:  ✓ Compiled /src/middleware in 128ms (331 modules)
web:dev:  ○ Compiling / ...
web:dev:  ⨯ ../../node_modules/@workos-inc/nextjs/dist/cjs/impersonation.js
web:dev: Module parse failed: 'import' and 'export' may appear only with 'sourceType: module' (1:106)
web:dev: > /* __next_internal_action_entry_do_not_use__ {"51389d01f9c684912006828677ea5047beae752a":"$$ACTION_1"} */ import { createActionProxy } from "private-next-rsc-action-proxy";
web:dev: | import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption";
web:dev: | "use strict";
web:dev:
web:dev: Import trace for requested module:
web:dev: ../../node_modules/@workos-inc/nextjs/dist/cjs/impersonation.js
web:dev: ../../node_modules/@workos-inc/nextjs/dist/cjs/index.js
web:dev: ./src/app/page.tsx
web:dev:  ⨯ ../../node_modules/@workos-inc/nextjs/dist/cjs/impersonation.js
web:dev: Module parse failed: 'import' and 'export' may appear only with 'sourceType: module' (1:106)
web:dev: > /* __next_internal_action_entry_do_not_use__ {"51389d01f9c684912006828677ea5047beae752a":"$$ACTION_1"} */ import { createActionProxy } from "private-next-rsc-action-proxy";
web:dev: | import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption";
web:dev: | "use strict";
web:dev:
web:dev: Import trace for requested module:
web:dev: ../../node_modules/@workos-inc/nextjs/dist/cjs/impersonation.js
web:dev: ../../node_modules/@workos-inc/nextjs/dist/cjs/index.js
web:dev: ./src/app/page.tsx
web:dev:  ⚠ Fast Refresh had to perform a full reload due to a runtime error.
benoitgrelard commented 2 months ago

@nathanburgess thanks, I knew what to do, I just had trouble installing pnpm without busting my local env. I found a way eventually and was able to replicate the issue with the repo.

I tried figuring out what’s going on but I don’t think it’s something our side. If you look at the messages it points to something in Next.js, specifically starting from our Impersonation component which is a React server component using a server action, and you can see the issue is related to that. (Note the next import private-next-rsc-action-encryption). So it looks to me like some kind of misconfiguration in modules in this version of Next.js.

I was able to reliably get everything to work by updating Next.js in that repository to ^14.1.0.

So I would say the best course of action is to upgrade Next.js to a more recent version (also there isn’t any stable version between 14.0.4 and 14.1.0).

nathanburgess commented 2 months ago

Cool cool, I'll give that a try again. I swear I tried upgrading Next last time. ¯\_(ツ)_/¯