{"record":{"id":"4141154b59a7cb69","repo":"calcom/cal.diy","slug":"apiauthstrategy-next-auth-user-associated-with","errorCode":null,"errorMessage":"ApiAuthStrategy - next auth - User associated with the authentication token email not found.","messagePattern":"ApiAuthStrategy - next auth - User associated with the authentication token email not found\\.","errorType":"http","errorClass":"UnauthorizedException","httpStatus":401,"severity":"error","filePath":"apps/api/v2/src/modules/auth/strategies/api-auth/api-auth.strategy.ts","lineNumber":310,"sourceCode":"      );\n    }\n\n    const organizationId = this.usersService.getUserMainOrgId(user) as number;\n    request.organizationId = organizationId;\n\n    return user;\n  }\n\n  async nextAuthStrategy(token: { email?: string | null }, request: ApiAuthGuardRequest) {\n    if (!token.email) {\n      throw new UnauthorizedException(\n        \"ApiAuthStrategy - next auth - Email not found in the authentication token.\"\n      );\n    }\n\n    const user = await this.userRepository.findByEmailWithProfile(token.email);\n    if (!user) {\n      throw new UnauthorizedException(\n        \"ApiAuthStrategy - next auth - User associated with the authentication token email not found.\"\n      );\n    }\n    const organizationId = this.usersService.getUserMainOrgId(user) as number;\n    request.organizationId = organizationId;\n\n    return user;\n  }\n\n  async validateThirdPartyAccessToken(\n    token: string,\n    request: ApiAuthGuardRequest\n  ): Promise<{ success: true; data: UserWithProfile } | { success: false }> {\n    const decodedToken = this.tokensService.getDecodedThirdPartyAccessToken(token);\n    if (!decodedToken) {\n      return { success: false };\n    }\n","sourceCodeStart":292,"sourceCodeEnd":328,"githubUrl":"https://github.com/calcom/cal.diy/blob/176037d0afbe572f870a3c702985e7cd83fe6c0c/apps/api/v2/src/modules/auth/strategies/api-auth/api-auth.strategy.ts#L292-L328","documentation":"Thrown by nextAuthStrategy when userRepository.findByEmailWithProfile(token.email) returns null. The decoded NextAuth token had a valid email claim, but no User row matches that email in the database the API v2 is connected to.","triggerScenarios":"A NextAuth session cookie for a user that does not exist in this Cal.com instance (e.g., a token minted by another deployment, or the user was deleted after the session was created).","commonSituations":"Carrying a session cookie across environments (dev cookie against prod); user account was deleted; email address changed/case-difference between the token and the DB.","solutions":["Re-authenticate via the web app for this environment so a fresh session cookie tied to an existing user is issued.","Confirm the user exists with `SELECT id,email FROM users WHERE email = '<token.email>'` against the same DB.","Prefer platform OAuth access tokens or API keys for service-to-service calls instead of session cookies."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"const payload = decodeJwt(sessionJwt);\nconst exists = await db.user.findFirst({ where: { email: payload.email } });\nif (!exists) throw new Error('No user for this session email; re-authenticate in this environment');","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Re-authenticate in the target environment rather than reusing session cookies elsewhere.","Normalize email casing on session creation and user lookup.","Cascade-revoke sessions when a user is deleted."],"tags":["auth","next-auth","user","platform"],"backgroundTag":null,"analyzedSha":"176037d0afbe572f870a3c702985e7cd83fe6c0c","analyzedAt":"2026-08-12T19:12:41.464Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}