UNPKG

better-auth-harmony

Version:

Validation and normalization for better-auth

1 lines 985 B
import {createAuthMiddleware,APIError}from'better-auth/api';import {ParseError,parsePhoneNumberWithError}from'libphonenumber-js/max';var u=new Set(["/sign-in/phone-number","/phone-number/request-password-reset","/phone-number/forget-password","/phone-number/reset-password","/phone-number/send-otp","/phone-number/verify"]),p=({path:r})=>typeof r=="string"&&u.has(r);var l=({defaultCountry:r,defaultCallingCode:a,extract:h=true,acceptRawInputOnError:s=false,matchers:i=[p],normalizer:m=e=>parsePhoneNumberWithError(e,{defaultCountry:r,defaultCallingCode:a,extract:h}).number}={})=>({id:"harmony-phone-number",hooks:{before:[{matcher:e=>i.some(o=>o(e)),handler:createAuthMiddleware(async e=>{let{phoneNumber:o}=e.body;if(typeof o!="string")return;let t=o;try{t=await m(o,e.request);}catch(n){if(!s&&n instanceof ParseError)throw new APIError("BAD_REQUEST",{message:n.message});if(s)t=o;else throw n}return {context:{...e,body:{...e.body,phoneNumber:t}}}})}]}}),w=l;export{w as default};