{"record":{"id":"6a9fc9ff3a15d48f","repo":"RocketChat/Rocket.Chat","slug":"error-token-does-not-exists","errorCode":"error-token-does-not-exists","errorMessage":"Token does not exist","messagePattern":"Token does not exist","errorType":"exception","errorClass":"Meteor.Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/imports/personal-access-tokens/server/api/methods/regenerateToken.ts","lineNumber":29,"sourceCode":"\t// eslint-disable-next-line @typescript-eslint/naming-convention\n\tinterface ServerMethods {\n\t\t'personalAccessTokens:regenerateToken'(params: { tokenName: string }): Promise<string>;\n\t}\n}\n\nexport const regeneratePersonalAccessTokenOfUser = async (tokenName: string, userId: string): Promise<string> => {\n\tif (!(await hasPermissionAsync(userId, 'create-personal-access-tokens'))) {\n\t\tthrow new Meteor.Error('not-authorized', 'Not Authorized', {\n\t\t\tmethod: 'personalAccessTokens:regenerateToken',\n\t\t});\n\t}\n\n\tconst tokenExist = await Users.findPersonalAccessTokenByTokenNameAndUserId({\n\t\tuserId,\n\t\ttokenName,\n\t});\n\tif (!tokenExist) {\n\t\tthrow new Meteor.Error('error-token-does-not-exists', 'Token does not exist', {\n\t\t\tmethod: 'personalAccessTokens:regenerateToken',\n\t\t});\n\t}\n\n\tawait removePersonalAccessTokenOfUser(tokenName, userId);\n\n\tconst tokenObject = tokenExist.services?.resume?.loginTokens?.find((token) => isPersonalAccessToken(token) && token.name === tokenName);\n\n\treturn generatePersonalAccessTokenOfUser({\n\t\ttokenName,\n\t\tuserId,\n\t\tbypassTwoFactor: (tokenObject && isPersonalAccessToken(tokenObject) && tokenObject.bypassTwoFactor) || false,\n\t});\n};\n\nMeteor.methods<ServerMethods>({\n\t'personalAccessTokens:regenerateToken': twoFactorRequired(async function ({ tokenName }: { tokenName: string }) {\n\t\tconst uid = Meteor.userId();","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/f9d3ec372bb580fa8d036f94cf03925a478ef768/apps/meteor/imports/personal-access-tokens/server/api/methods/regenerateToken.ts#L11-L47","documentation":"Thrown by regeneratePersonalAccessTokenOfUser when Users.findPersonalAccessTokenByTokenNameAndUserId returns null — there is no personal access token with that name for the user. Regeneration requires an existing token to replace, so a missing token is an error rather than a create.","triggerScenarios":"Calling regenerateToken with a tokenName the user never created, that was already removed, or whose name does not exactly match.","commonSituations":"Token was deleted by another admin or in another tab; typo in tokenName; UI showed a stale token list after deletion; case mismatch in the name.","solutions":["If you want a brand-new token, call generateToken instead of regenerateToken.","Re-fetch the user's token list and use the exact existing name.","Confirm the token still exists before offering a 'regenerate' action in the UI.","Handle the error by prompting the user to create a new token."],"exampleFix":"// before\nawait regeneratePersonalAccessTokenOfUser('typo-name', userId);\n\n// after\nconst exists = await Users.findPersonalAccessTokenByTokenNameAndUserId({ userId, tokenName });\nif (!exists) {\n  await generatePersonalAccessTokenOfUser({ userId, tokenName, bypassTwoFactor: false });\n} else {\n  await regeneratePersonalAccessTokenOfUser(tokenName, userId);\n}","handlingStrategy":"validation","validationCode":"async function tokenExists(userId: string, tokenName: string): Promise<boolean> {\n  const t = await Users.findPersonalAccessTokenByTokenNameAndUserId({ userId, tokenName });\n  return Boolean(t);\n}","typeGuard":null,"tryCatchPattern":"try {\n  await regeneratePersonalAccessTokenOfUser(tokenName, userId);\n} catch (e) {\n  if (e.error === 'error-token-does-not-exists') {\n    // fall back to generateToken\n    await generatePersonalAccessTokenOfUser({ userId, tokenName, bypassTwoFactor: false });\n  } else throw e;\n}","preventionTips":["Only offer 'regenerate' for tokens that currently exist (live list).","Use exact tokenName spelling/case.","Fall back to generate when the named token is gone."],"tags":["personal-access-tokens","not-found","validation"],"backgroundTag":null,"analyzedSha":"f9d3ec372bb580fa8d036f94cf03925a478ef768","analyzedAt":"2026-08-12T19:07:17.372Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}