better-auth
Version:
The most comprehensive authentication framework for TypeScript.
1 lines • 2.21 kB
Source Map (JSON)
{"version":3,"file":"password.mjs","names":[],"sources":["../../src/utils/password.ts"],"sourcesContent":["import type { GenericEndpointContext } from \"@better-auth/core\";\nimport { APIError } from \"better-call\";\n\nexport async function validatePassword(\n\tctx: GenericEndpointContext,\n\tdata: {\n\t\tpassword: string;\n\t\tuserId: string;\n\t},\n) {\n\tconst accounts = await ctx.context.internalAdapter.findAccounts(data.userId);\n\tconst credentialAccount = accounts?.find(\n\t\t(account) => account.providerId === \"credential\",\n\t);\n\tconst currentPassword = credentialAccount?.password;\n\tif (!credentialAccount || !currentPassword) {\n\t\treturn false;\n\t}\n\tconst compare = await ctx.context.password.verify({\n\t\thash: currentPassword,\n\t\tpassword: data.password,\n\t});\n\treturn compare;\n}\n\nexport async function checkPassword(userId: string, c: GenericEndpointContext) {\n\tconst accounts = await c.context.internalAdapter.findAccounts(userId);\n\tconst credentialAccount = accounts?.find(\n\t\t(account) => account.providerId === \"credential\",\n\t);\n\tconst currentPassword = credentialAccount?.password;\n\tif (!credentialAccount || !currentPassword || !c.body.password) {\n\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\tmessage: \"No password credential found\",\n\t\t});\n\t}\n\tconst compare = await c.context.password.verify({\n\t\thash: currentPassword,\n\t\tpassword: c.body.password,\n\t});\n\tif (!compare) {\n\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\tmessage: \"Invalid password\",\n\t\t});\n\t}\n\treturn true;\n}\n"],"mappings":";;;AAGA,eAAsB,iBACrB,KACA,MAIC;CAED,MAAM,qBADW,MAAM,IAAI,QAAQ,gBAAgB,aAAa,KAAK,OAAO,GACxC,MAClC,YAAY,QAAQ,eAAe,aACpC;CACD,MAAM,kBAAkB,mBAAmB;AAC3C,KAAI,CAAC,qBAAqB,CAAC,gBAC1B,QAAO;AAMR,QAJgB,MAAM,IAAI,QAAQ,SAAS,OAAO;EACjD,MAAM;EACN,UAAU,KAAK;EACf,CAAC;;AAIH,eAAsB,cAAc,QAAgB,GAA2B;CAE9E,MAAM,qBADW,MAAM,EAAE,QAAQ,gBAAgB,aAAa,OAAO,GACjC,MAClC,YAAY,QAAQ,eAAe,aACpC;CACD,MAAM,kBAAkB,mBAAmB;AAC3C,KAAI,CAAC,qBAAqB,CAAC,mBAAmB,CAAC,EAAE,KAAK,SACrD,OAAM,IAAI,SAAS,eAAe,EACjC,SAAS,gCACT,CAAC;AAMH,KAAI,CAJY,MAAM,EAAE,QAAQ,SAAS,OAAO;EAC/C,MAAM;EACN,UAAU,EAAE,KAAK;EACjB,CAAC,CAED,OAAM,IAAI,SAAS,eAAe,EACjC,SAAS,oBACT,CAAC;AAEH,QAAO"}