{"version":3,"file":"clear-token.mjs","names":["constants"],"sources":["../../../../src/services/auth/refresh-token/clear-token.ts"],"sourcesContent":["import { deleteCookie, getCookie } from \"hono/cookie\";\nimport constants from \"../../../constants/constants.js\";\nimport createServiceContext from \"../../../libs/http/utils/create-service-context.js\";\nimport cacheKeys from \"../../../libs/kv/cache-keys.js\";\nimport { UserTokensRepository } from \"../../../libs/repositories/index.js\";\nimport type { LucidHonoContext } from \"../../../types/hono.js\";\nimport hashUserToken from \"../../../utils/helpers/hash-user-token.js\";\nimport type { ServiceResponse } from \"../../../utils/services/types.js\";\n\ninterface ClearTokenConfig {\n\trevokeReason?: string;\n\tconsume?: boolean;\n}\n\nconst clearToken = async (\n\tc: LucidHonoContext,\n\toptions?: ClearTokenConfig,\n): ServiceResponse<undefined> => {\n\tconst _refresh = getCookie(c, constants.cookies.refreshToken);\n\tdeleteCookie(c, constants.cookies.refreshToken, { path: \"/\" });\n\n\tif (!_refresh) {\n\t\treturn {\n\t\t\terror: undefined,\n\t\t\tdata: undefined,\n\t\t};\n\t}\n\n\tconst config = c.get(\"config\");\n\tconst now = new Date().toISOString();\n\tconst hashedRefreshToken = hashUserToken(_refresh);\n\n\tconst UserTokens = new UserTokensRepository(\n\t\tc.get(\"database\").client,\n\t\tconfig.db,\n\t);\n\tconst context = createServiceContext(c);\n\n\tawait context.kv.delete(context, {\n\t\tkey: cacheKeys.auth.refresh(_refresh),\n\t\thash: true,\n\t});\n\n\tconst updateTokenRes = await UserTokens.updateMultiple({\n\t\tdata: {\n\t\t\trevoked_at: now,\n\t\t\trevoke_reason:\n\t\t\t\toptions?.revokeReason ?? constants.refreshTokenRevokeReasons.logout,\n\t\t\texpiry_date: now,\n\t\t\t...(options?.consume ? { consumed_at: now } : {}),\n\t\t},\n\t\twhere: [\n\t\t\t{\n\t\t\t\tkey: \"token\",\n\t\t\t\toperator: \"=\",\n\t\t\t\tvalue: hashedRefreshToken,\n\t\t\t},\n\t\t\t{\n\t\t\t\tkey: \"token_type\",\n\t\t\t\toperator: \"=\",\n\t\t\t\tvalue: constants.userTokens.refresh,\n\t\t\t},\n\t\t\t{\n\t\t\t\tkey: \"revoked_at\",\n\t\t\t\toperator: \"is\",\n\t\t\t\tvalue: null,\n\t\t\t},\n\t\t],\n\t});\n\tif (updateTokenRes.error) return updateTokenRes;\n\n\treturn {\n\t\terror: undefined,\n\t\tdata: undefined,\n\t};\n};\n\nexport default clearToken;\n"],"mappings":"gVAcA,MAAM,EAAa,MAClB,EACA,IACgC,CAChC,IAAM,EAAW,EAAU,EAAGA,EAAU,QAAQ,YAAY,EAG5D,GAFA,EAAa,EAAGA,EAAU,QAAQ,aAAc,CAAE,KAAM,GAAI,CAAC,EAEzD,CAAC,EACJ,MAAO,CACN,MAAO,IAAA,GACP,KAAM,IAAA,EACP,EAGD,IAAM,EAAS,EAAE,IAAI,QAAQ,EACvB,EAAM,IAAI,KAAK,CAAA,CAAE,YAAY,EAC7B,EAAqB,EAAc,CAAQ,EAE3C,EAAa,IAAI,EACtB,EAAE,IAAI,UAAU,CAAC,CAAC,OAClB,EAAO,EACR,EACM,EAAU,EAAqB,CAAC,EAEtC,MAAM,EAAQ,GAAG,OAAO,EAAS,CAChC,IAAK,EAAU,KAAK,QAAQ,CAAQ,EACpC,KAAM,EACP,CAAC,EAED,IAAM,EAAiB,MAAM,EAAW,eAAe,CACtD,KAAM,CACL,WAAY,EACZ,cACC,GAAS,cAAgBA,EAAU,0BAA0B,OAC9D,YAAa,EACb,GAAI,GAAS,QAAU,CAAE,YAAa,CAAI,EAAI,CAAC,CAChD,EACA,MAAO,CACN,CACC,IAAK,QACL,SAAU,IACV,MAAO,CACR,EACA,CACC,IAAK,aACL,SAAU,IACV,MAAOA,EAAU,WAAW,OAC7B,EACA,CACC,IAAK,aACL,SAAU,KACV,MAAO,IACR,CACD,CACD,CAAC,EAGD,OAFI,EAAe,MAAc,EAE1B,CACN,MAAO,IAAA,GACP,KAAM,IAAA,EACP,CACD"}