better-auth
Version:
The most comprehensive authentication framework for TypeScript.
1 lines • 3.16 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","names":[],"sources":["../../../src/plugins/phone-number/index.ts"],"sourcesContent":["import type { BetterAuthPlugin } from \"@better-auth/core\";\nimport { createAuthMiddleware } from \"@better-auth/core/api\";\nimport { APIError } from \"better-call\";\nimport { mergeSchema } from \"../../db/schema\";\nimport { PHONE_NUMBER_ERROR_CODES } from \"./error-codes\";\nimport type { RequiredPhoneNumberOptions } from \"./routes\";\nimport {\n\trequestPasswordResetPhoneNumber,\n\tresetPasswordPhoneNumber,\n\tsendPhoneNumberOTP,\n\tsignInPhoneNumber,\n\tverifyPhoneNumber,\n} from \"./routes\";\nimport { schema } from \"./schema\";\nimport type { PhoneNumberOptions, UserWithPhoneNumber } from \"./types\";\n\nexport type { PhoneNumberOptions, UserWithPhoneNumber };\n\nexport const phoneNumber = (options?: PhoneNumberOptions | undefined) => {\n\tconst opts = {\n\t\texpiresIn: options?.expiresIn || 300,\n\t\totpLength: options?.otpLength || 6,\n\t\t...options,\n\t\tphoneNumber: \"phoneNumber\",\n\t\tphoneNumberVerified: \"phoneNumberVerified\",\n\t\tcode: \"code\",\n\t\tcreatedAt: \"createdAt\",\n\t};\n\n\treturn {\n\t\tid: \"phone-number\",\n\t\thooks: {\n\t\t\tbefore: [\n\t\t\t\t{\n\t\t\t\t\t// Stop any requests attempting to update the user's phone number\n\t\t\t\t\tmatcher: (ctx) =>\n\t\t\t\t\t\tctx.path === \"/update-user\" && \"phoneNumber\" in ctx.body,\n\t\t\t\t\thandler: createAuthMiddleware(async (_ctx) => {\n\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\tmessage: PHONE_NUMBER_ERROR_CODES.PHONE_NUMBER_CANNOT_BE_UPDATED,\n\t\t\t\t\t\t});\n\t\t\t\t\t}),\n\t\t\t\t},\n\t\t\t],\n\t\t},\n\t\tendpoints: {\n\t\t\tsignInPhoneNumber: signInPhoneNumber(opts as RequiredPhoneNumberOptions),\n\t\t\tsendPhoneNumberOTP: sendPhoneNumberOTP(\n\t\t\t\topts as RequiredPhoneNumberOptions,\n\t\t\t),\n\t\t\tverifyPhoneNumber: verifyPhoneNumber(opts as RequiredPhoneNumberOptions),\n\t\t\trequestPasswordResetPhoneNumber: requestPasswordResetPhoneNumber(\n\t\t\t\topts as RequiredPhoneNumberOptions,\n\t\t\t),\n\t\t\tresetPasswordPhoneNumber: resetPasswordPhoneNumber(\n\t\t\t\topts as RequiredPhoneNumberOptions,\n\t\t\t),\n\t\t},\n\t\tschema: mergeSchema(schema, options?.schema),\n\t\trateLimit: [\n\t\t\t{\n\t\t\t\tpathMatcher(path) {\n\t\t\t\t\treturn path.startsWith(\"/phone-number\");\n\t\t\t\t},\n\t\t\t\twindow: 60 * 1000,\n\t\t\t\tmax: 10,\n\t\t\t},\n\t\t],\n\t\toptions,\n\t\t$ERROR_CODES: PHONE_NUMBER_ERROR_CODES,\n\t} satisfies BetterAuthPlugin;\n};\n"],"mappings":";;;;;;;;AAkBA,MAAa,eAAe,YAA6C;CACxE,MAAM,OAAO;EACZ,WAAW,SAAS,aAAa;EACjC,WAAW,SAAS,aAAa;EACjC,GAAG;EACH,aAAa;EACb,qBAAqB;EACrB,MAAM;EACN,WAAW;EACX;AAED,QAAO;EACN,IAAI;EACJ,OAAO,EACN,QAAQ,CACP;GAEC,UAAU,QACT,IAAI,SAAS,kBAAkB,iBAAiB,IAAI;GACrD,SAAS,qBAAqB,OAAO,SAAS;AAC7C,UAAM,IAAI,SAAS,eAAe,EACjC,SAAS,yBAAyB,gCAClC,CAAC;KACD;GACF,CACD,EACD;EACD,WAAW;GACV,mBAAmB,kBAAkB,KAAmC;GACxE,oBAAoB,mBACnB,KACA;GACD,mBAAmB,kBAAkB,KAAmC;GACxE,iCAAiC,gCAChC,KACA;GACD,0BAA0B,yBACzB,KACA;GACD;EACD,QAAQ,YAAY,QAAQ,SAAS,OAAO;EAC5C,WAAW,CACV;GACC,YAAY,MAAM;AACjB,WAAO,KAAK,WAAW,gBAAgB;;GAExC,QAAQ,KAAK;GACb,KAAK;GACL,CACD;EACD;EACA,cAAc;EACd"}