stack-auth / stack

Open-source Auth0/Clerk alternative
https://stack-auth.com
Other
3.75k stars 244 forks source link

StackAssertionError (while trying to signin) #310

Open suryavirkapur opened 5 days ago

suryavirkapur commented 5 days ago

Using the dev branch on my Mac.

Node and PNPM Versions

suryavirkapur@Suryavirs-MacBook-Pro stack % pnpm -v
9.4.0
suryavirkapur@Suryavirs-MacBook-Pro stack % node -v
v22.2.0

Error:

StackAssertionError: Uncaught error in asynchronous function: KnownError<USER_EMAIL_ALREADY_EXISTS>: User already exists.

This is likely an error in Stack. Please make sure you are running the newest version and report it.
    at eval (promises.js:125:26)Caused by: KnownError<USER_EMAIL_ALREADY_EXISTS>: User already exists.
    at KnownError.fromJson (known-errors.js:42:24)
    at StackClientInterface._processResponse (clientInterface.js:279:38)
    at async StackClientInterface.sendClientRequestInner (clientInterface.js:228:30)
    at async Object.retry (results.js:122:21)
    at async StackClientInterface._networkRetry (clientInterface.js:72:31)
    at async StackClientInterface.sendClientRequest (clientInterface.js:135:16)
    at async StackClientInterface.sendClientRequestAndCatchKnownError (clientInterface.js:146:30)
    at async StackClientInterface.sendMagicLinkEmail (clientInterface.js:328:21)
    at async __StackClientAppImpl.sendMagicLinkEmail (stack-app.ts:1100:12)
    at async onSubmit (magic-link-sign-in.tsx:94:18)
    at async eval (index.esm.mjs:2256:17) Object  Error: This error was caught by DevErrorNotifier, and the original stacktrace is below.
    at console.<computed> (dev-error-notifier.tsx:18:25)
    at console.error (index.js:103:26)
    at eval (errors.js:36:13)
    at captureError (errors.js:44:9)
    at eval (promises.js:132:25)
fomalhautb commented 5 days ago

What is the code that produces this? How can I reproduce it?

suryavirkapur commented 5 days ago

Followed development intructions on README.md: clone repo, switch .env.development to .env.local, and then run the project.

http://localhost:8101/handler/sign-in

Login via Email Magic Link


stackframe/stack-backend:dev: [dev] [    ERR] [xfjenwzm9aqkbsvx] POST http://localhost:8102/api/v1/auth/otp/sign-in: Internal Server Error. The error message follows, but will be stripped in production. TypeError: Cannot read properties of undefined (reading '0')
@stackframe/stack-backend:dev: [dev]     at isInsideNodeModules (node:internal/util:511:17)
@stackframe/stack-backend:dev: [dev]     at showFlaggedDeprecation (node:buffer:178:8)
@stackframe/stack-backend:dev: [dev]     at new Buffer (node:buffer:266:3)
@stackframe/stack-backend:dev: [dev]     at BN.toArrayLike (webpack-internal:///(rsc)/../../node_modules/.pnpm/bn.js@4.12.0/node_modules/bn.js/lib/bn.js:546:15)
@stackframe/stack-backend:dev: [dev]     at BN.toBuffer (webpack-internal:///(rsc)/../../node_modules/.pnpm/bn.js@4.12.0/node_modules/bn.js/lib/bn.js:531:17)
@stackframe/stack-backend:dev: [dev]     at getPrivateJwk (webpack-internal:///(rsc)/../../packages/stack-shared/dist/utils/jwt.js:90:85)
@stackframe/stack-backend:dev: [dev]     at async signJWT (webpack-internal:///(rsc)/../../packages/stack-shared/dist/utils/jwt.js:59:74)
@stackframe/stack-backend:dev: [dev]     at async generateAccessToken (webpack-internal:///(rsc)/./src/lib/tokens.tsx:100:16)
@stackframe/stack-backend:dev: [dev]     at async createAuthTokens (webpack-internal:///(rsc)/./src/lib/tokens.tsx:113:25)```

Broswer Console Dump:

https://pastebin.com/6DpbapPY
fomalhautb commented 5 days ago

Followed development intructions on README.md: clone repo, switch .env.development to .env.local, and then run the project.

http://localhost:8101/handler/sign-in

Login via Email Magic Link

stackframe/stack-backend:dev: [dev] [    ERR] [xfjenwzm9aqkbsvx] POST http://localhost:8102/api/v1/auth/otp/sign-in: Internal Server Error. The error message follows, but will be stripped in production. TypeError: Cannot read properties of undefined (reading '0')
@stackframe/stack-backend:dev: [dev]     at isInsideNodeModules (node:internal/util:511:17)
@stackframe/stack-backend:dev: [dev]     at showFlaggedDeprecation (node:buffer:178:8)
@stackframe/stack-backend:dev: [dev]     at new Buffer (node:buffer:266:3)
@stackframe/stack-backend:dev: [dev]     at BN.toArrayLike (webpack-internal:///(rsc)/../../node_modules/.pnpm/bn.js@4.12.0/node_modules/bn.js/lib/bn.js:546:15)
@stackframe/stack-backend:dev: [dev]     at BN.toBuffer (webpack-internal:///(rsc)/../../node_modules/.pnpm/bn.js@4.12.0/node_modules/bn.js/lib/bn.js:531:17)
@stackframe/stack-backend:dev: [dev]     at getPrivateJwk (webpack-internal:///(rsc)/../../packages/stack-shared/dist/utils/jwt.js:90:85)
@stackframe/stack-backend:dev: [dev]     at async signJWT (webpack-internal:///(rsc)/../../packages/stack-shared/dist/utils/jwt.js:59:74)
@stackframe/stack-backend:dev: [dev]     at async generateAccessToken (webpack-internal:///(rsc)/./src/lib/tokens.tsx:100:16)
@stackframe/stack-backend:dev: [dev]     at async createAuthTokens (webpack-internal:///(rsc)/./src/lib/tokens.tsx:113:25)```

Broswer Console Dump:

https://pastebin.com/6DpbapPY

Are you sure that you didn't create a user with the same email before? I can't reproduce this problem

suryavirkapur commented 5 days ago

Just tried singning up with a new email. Same issue.

suryavirkapur commented 5 days ago

packages/stack-shared/src/utils/jwt.tsx at Line 35:

  issuer: string,
  audience: string,
  payload: any,
  expirationTime?: string,
}) {
  const secret = getPerAudienceSecret({ audience: options.audience, secret: STACK_SERVER_SECRET });
  const kid = getKid({ secret });
  const privateJwk = await jose.importJWK(await getPrivateJwk(secret));
  return await new jose.SignJWT(options.payload)
    .setProtectedHeader({ alg: "ES256", kid })
    .setIssuer(options.issuer)
    .setIssuedAt()
    .setAudience(options.audience)
    .setExpirationTime(options.expirationTime || "5m")
    .sign(privateJwk);
}

Is returning an await on a promise.

export async function generateAccessToken(options: {
  projectId: string,
  useLegacyGlobalJWT: boolean,
  userId: string,
}) {
  waitUntil(logEvent([SystemEventTypes.UserActivity], { projectId: options.projectId, userId: options.userId }));

  if (options.useLegacyGlobalJWT) {
    return await legacySignGlobalJWT(
      jwtIssuer,
      { projectId: options.projectId, sub: options.userId },
      getEnvVariable("STACK_ACCESS_TOKEN_EXPIRATION_TIME", "10min")
    );
  } else {
    return await signJWT({
      issuer: jwtIssuer,
      audience: options.projectId,
      payload: { sub: options.userId },
      expirationTime: getEnvVariable("STACK_ACCESS_TOKEN_EXPIRATION_TIME", "10min"),
    });
  }
}

And here there is an await on an await ...

apps/backend/src/lib/tokens.tsx at Line 73:

This is not a function on the hot path, but thought would let you know.