@sirketio/auth 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (594) hide show
  1. package/dist/_virtual/_rolldown/runtime.mjs +36 -0
  2. package/dist/adapter/index.d.mts +4 -0
  3. package/dist/adapter/index.mjs +7 -0
  4. package/dist/api/index.d.mts +3872 -0
  5. package/dist/api/index.mjs +206 -0
  6. package/dist/api/index.mjs.map +1 -0
  7. package/dist/api/middlewares/index.d.mts +1 -0
  8. package/dist/api/middlewares/index.mjs +3 -0
  9. package/dist/api/middlewares/origin-check.d.mts +18 -0
  10. package/dist/api/middlewares/origin-check.mjs +140 -0
  11. package/dist/api/middlewares/origin-check.mjs.map +1 -0
  12. package/dist/api/rate-limiter/index.mjs +204 -0
  13. package/dist/api/rate-limiter/index.mjs.map +1 -0
  14. package/dist/api/routes/account.d.mts +410 -0
  15. package/dist/api/routes/account.mjs +493 -0
  16. package/dist/api/routes/account.mjs.map +1 -0
  17. package/dist/api/routes/callback.d.mts +31 -0
  18. package/dist/api/routes/callback.mjs +179 -0
  19. package/dist/api/routes/callback.mjs.map +1 -0
  20. package/dist/api/routes/email-verification.d.mts +161 -0
  21. package/dist/api/routes/email-verification.mjs +299 -0
  22. package/dist/api/routes/email-verification.mjs.map +1 -0
  23. package/dist/api/routes/error.d.mts +28 -0
  24. package/dist/api/routes/error.mjs +386 -0
  25. package/dist/api/routes/error.mjs.map +1 -0
  26. package/dist/api/routes/index.d.mts +11 -0
  27. package/dist/api/routes/index.mjs +13 -0
  28. package/dist/api/routes/ok.d.mts +36 -0
  29. package/dist/api/routes/ok.mjs +30 -0
  30. package/dist/api/routes/ok.mjs.map +1 -0
  31. package/dist/api/routes/password.d.mts +182 -0
  32. package/dist/api/routes/password.mjs +198 -0
  33. package/dist/api/routes/password.mjs.map +1 -0
  34. package/dist/api/routes/session.d.mts +415 -0
  35. package/dist/api/routes/session.mjs +483 -0
  36. package/dist/api/routes/session.mjs.map +1 -0
  37. package/dist/api/routes/sign-in.d.mts +171 -0
  38. package/dist/api/routes/sign-in.mjs +263 -0
  39. package/dist/api/routes/sign-in.mjs.map +1 -0
  40. package/dist/api/routes/sign-out.d.mts +36 -0
  41. package/dist/api/routes/sign-out.mjs +33 -0
  42. package/dist/api/routes/sign-out.mjs.map +1 -0
  43. package/dist/api/routes/sign-up.d.mts +160 -0
  44. package/dist/api/routes/sign-up.mjs +227 -0
  45. package/dist/api/routes/sign-up.mjs.map +1 -0
  46. package/dist/api/routes/update-user.d.mts +445 -0
  47. package/dist/api/routes/update-user.mjs +493 -0
  48. package/dist/api/routes/update-user.mjs.map +1 -0
  49. package/dist/api/state/oauth.d.mts +18 -0
  50. package/dist/api/state/oauth.mjs +8 -0
  51. package/dist/api/state/oauth.mjs.map +1 -0
  52. package/dist/api/state/should-session-refresh.d.mts +13 -0
  53. package/dist/api/state/should-session-refresh.mjs +16 -0
  54. package/dist/api/state/should-session-refresh.mjs.map +1 -0
  55. package/dist/api/to-auth-endpoints.mjs +197 -0
  56. package/dist/api/to-auth-endpoints.mjs.map +1 -0
  57. package/dist/auth/base.mjs +45 -0
  58. package/dist/auth/base.mjs.map +1 -0
  59. package/dist/auth/minimal.d.mts +12 -0
  60. package/dist/auth/minimal.mjs +14 -0
  61. package/dist/auth/minimal.mjs.map +1 -0
  62. package/dist/auth/trusted-origins.mjs +31 -0
  63. package/dist/auth/trusted-origins.mjs.map +1 -0
  64. package/dist/client/broadcast-channel.d.mts +20 -0
  65. package/dist/client/broadcast-channel.mjs +46 -0
  66. package/dist/client/broadcast-channel.mjs.map +1 -0
  67. package/dist/client/config.mjs +90 -0
  68. package/dist/client/config.mjs.map +1 -0
  69. package/dist/client/fetch-plugins.mjs +18 -0
  70. package/dist/client/fetch-plugins.mjs.map +1 -0
  71. package/dist/client/focus-manager.d.mts +11 -0
  72. package/dist/client/focus-manager.mjs +32 -0
  73. package/dist/client/focus-manager.mjs.map +1 -0
  74. package/dist/client/index.d.mts +33 -0
  75. package/dist/client/index.mjs +21 -0
  76. package/dist/client/index.mjs.map +1 -0
  77. package/dist/client/online-manager.d.mts +12 -0
  78. package/dist/client/online-manager.mjs +35 -0
  79. package/dist/client/online-manager.mjs.map +1 -0
  80. package/dist/client/parser.mjs +73 -0
  81. package/dist/client/parser.mjs.map +1 -0
  82. package/dist/client/path-to-object.d.mts +65 -0
  83. package/dist/client/plugins/index.d.mts +53 -0
  84. package/dist/client/plugins/index.mjs +30 -0
  85. package/dist/client/plugins/infer-plugin.d.mts +16 -0
  86. package/dist/client/plugins/infer-plugin.mjs +11 -0
  87. package/dist/client/plugins/infer-plugin.mjs.map +1 -0
  88. package/dist/client/proxy.mjs +79 -0
  89. package/dist/client/proxy.mjs.map +1 -0
  90. package/dist/client/query.d.mts +23 -0
  91. package/dist/client/query.mjs +98 -0
  92. package/dist/client/query.mjs.map +1 -0
  93. package/dist/client/react/index.d.mts +128 -0
  94. package/dist/client/react/index.mjs +24 -0
  95. package/dist/client/react/index.mjs.map +1 -0
  96. package/dist/client/react/react-store.d.mts +47 -0
  97. package/dist/client/react/react-store.mjs +47 -0
  98. package/dist/client/react/react-store.mjs.map +1 -0
  99. package/dist/client/session-atom.mjs +29 -0
  100. package/dist/client/session-atom.mjs.map +1 -0
  101. package/dist/client/session-refresh.d.mts +28 -0
  102. package/dist/client/session-refresh.mjs +140 -0
  103. package/dist/client/session-refresh.mjs.map +1 -0
  104. package/dist/client/types.d.mts +41 -0
  105. package/dist/client/vanilla.d.mts +127 -0
  106. package/dist/client/vanilla.mjs +20 -0
  107. package/dist/client/vanilla.mjs.map +1 -0
  108. package/dist/context/create-context.mjs +211 -0
  109. package/dist/context/create-context.mjs.map +1 -0
  110. package/dist/context/helpers.mjs +83 -0
  111. package/dist/context/helpers.mjs.map +1 -0
  112. package/dist/context/init.mjs +20 -0
  113. package/dist/context/init.mjs.map +1 -0
  114. package/dist/cookies/cookie-utils.d.mts +29 -0
  115. package/dist/cookies/cookie-utils.mjs +105 -0
  116. package/dist/cookies/cookie-utils.mjs.map +1 -0
  117. package/dist/cookies/index.d.mts +121 -0
  118. package/dist/cookies/index.mjs +261 -0
  119. package/dist/cookies/index.mjs.map +1 -0
  120. package/dist/cookies/session-store.d.mts +36 -0
  121. package/dist/cookies/session-store.mjs +200 -0
  122. package/dist/cookies/session-store.mjs.map +1 -0
  123. package/dist/crypto/buffer.d.mts +8 -0
  124. package/dist/crypto/buffer.mjs +18 -0
  125. package/dist/crypto/buffer.mjs.map +1 -0
  126. package/dist/crypto/index.d.mts +27 -0
  127. package/dist/crypto/index.mjs +38 -0
  128. package/dist/crypto/index.mjs.map +1 -0
  129. package/dist/crypto/jwt.d.mts +8 -0
  130. package/dist/crypto/jwt.mjs +95 -0
  131. package/dist/crypto/jwt.mjs.map +1 -0
  132. package/dist/crypto/password.d.mts +12 -0
  133. package/dist/crypto/password.mjs +36 -0
  134. package/dist/crypto/password.mjs.map +1 -0
  135. package/dist/crypto/random.d.mts +5 -0
  136. package/dist/crypto/random.mjs +8 -0
  137. package/dist/crypto/random.mjs.map +1 -0
  138. package/dist/db/adapter-base.d.mts +8 -0
  139. package/dist/db/adapter-base.mjs +19 -0
  140. package/dist/db/adapter-base.mjs.map +1 -0
  141. package/dist/db/field-converter.d.mts +8 -0
  142. package/dist/db/field-converter.mjs +21 -0
  143. package/dist/db/field-converter.mjs.map +1 -0
  144. package/dist/db/field.d.mts +42 -0
  145. package/dist/db/get-schema.d.mts +11 -0
  146. package/dist/db/get-schema.mjs +39 -0
  147. package/dist/db/get-schema.mjs.map +1 -0
  148. package/dist/db/index.d.mts +18 -0
  149. package/dist/db/index.mjs +34 -0
  150. package/dist/db/index.mjs.map +1 -0
  151. package/dist/db/internal-adapter.d.mts +14 -0
  152. package/dist/db/internal-adapter.mjs +616 -0
  153. package/dist/db/internal-adapter.mjs.map +1 -0
  154. package/dist/db/schema.d.mts +49 -0
  155. package/dist/db/schema.mjs +118 -0
  156. package/dist/db/schema.mjs.map +1 -0
  157. package/dist/db/to-zod.d.mts +36 -0
  158. package/dist/db/to-zod.mjs +26 -0
  159. package/dist/db/to-zod.mjs.map +1 -0
  160. package/dist/db/verification-token-storage.mjs +28 -0
  161. package/dist/db/verification-token-storage.mjs.map +1 -0
  162. package/dist/db/with-hooks.d.mts +33 -0
  163. package/dist/db/with-hooks.mjs +159 -0
  164. package/dist/db/with-hooks.mjs.map +1 -0
  165. package/dist/index.d.mts +53 -0
  166. package/dist/index.mjs +27 -0
  167. package/dist/integrations/next-js.d.mts +29 -0
  168. package/dist/integrations/next-js.mjs +85 -0
  169. package/dist/integrations/next-js.mjs.map +1 -0
  170. package/dist/oauth2/index.d.mts +5 -0
  171. package/dist/oauth2/index.mjs +7 -0
  172. package/dist/oauth2/link-account.d.mts +48 -0
  173. package/dist/oauth2/link-account.mjs +143 -0
  174. package/dist/oauth2/link-account.mjs.map +1 -0
  175. package/dist/oauth2/state.d.mts +26 -0
  176. package/dist/oauth2/state.mjs +51 -0
  177. package/dist/oauth2/state.mjs.map +1 -0
  178. package/dist/oauth2/utils.d.mts +8 -0
  179. package/dist/oauth2/utils.mjs +31 -0
  180. package/dist/oauth2/utils.mjs.map +1 -0
  181. package/dist/plugins/access/access.d.mts +30 -0
  182. package/dist/plugins/access/access.mjs +46 -0
  183. package/dist/plugins/access/access.mjs.map +1 -0
  184. package/dist/plugins/access/index.d.mts +3 -0
  185. package/dist/plugins/access/index.mjs +3 -0
  186. package/dist/plugins/access/types.d.mts +17 -0
  187. package/dist/plugins/additional-fields/client.d.mts +96 -0
  188. package/dist/plugins/additional-fields/client.mjs +11 -0
  189. package/dist/plugins/additional-fields/client.mjs.map +1 -0
  190. package/dist/plugins/admin/access/index.d.mts +2 -0
  191. package/dist/plugins/admin/access/index.mjs +3 -0
  192. package/dist/plugins/admin/access/statement.d.mts +118 -0
  193. package/dist/plugins/admin/access/statement.mjs +53 -0
  194. package/dist/plugins/admin/access/statement.mjs.map +1 -0
  195. package/dist/plugins/admin/admin.d.mts +911 -0
  196. package/dist/plugins/admin/admin.mjs +95 -0
  197. package/dist/plugins/admin/admin.mjs.map +1 -0
  198. package/dist/plugins/admin/client.d.mts +76 -0
  199. package/dist/plugins/admin/client.mjs +36 -0
  200. package/dist/plugins/admin/client.mjs.map +1 -0
  201. package/dist/plugins/admin/error-codes.d.mts +29 -0
  202. package/dist/plugins/admin/error-codes.mjs +30 -0
  203. package/dist/plugins/admin/error-codes.mjs.map +1 -0
  204. package/dist/plugins/admin/has-permission.mjs +16 -0
  205. package/dist/plugins/admin/has-permission.mjs.map +1 -0
  206. package/dist/plugins/admin/index.d.mts +3 -0
  207. package/dist/plugins/admin/index.mjs +3 -0
  208. package/dist/plugins/admin/routes.mjs +841 -0
  209. package/dist/plugins/admin/routes.mjs.map +1 -0
  210. package/dist/plugins/admin/schema.d.mts +40 -0
  211. package/dist/plugins/admin/schema.mjs +34 -0
  212. package/dist/plugins/admin/schema.mjs.map +1 -0
  213. package/dist/plugins/admin/types.d.mts +89 -0
  214. package/dist/plugins/api-key/adapter.mjs +468 -0
  215. package/dist/plugins/api-key/adapter.mjs.map +1 -0
  216. package/dist/plugins/api-key/client.d.mts +46 -0
  217. package/dist/plugins/api-key/client.mjs +19 -0
  218. package/dist/plugins/api-key/client.mjs.map +1 -0
  219. package/dist/plugins/api-key/error-codes.d.mts +33 -0
  220. package/dist/plugins/api-key/error-codes.mjs +34 -0
  221. package/dist/plugins/api-key/error-codes.mjs.map +1 -0
  222. package/dist/plugins/api-key/index.d.mts +1251 -0
  223. package/dist/plugins/api-key/index.mjs +134 -0
  224. package/dist/plugins/api-key/index.mjs.map +1 -0
  225. package/dist/plugins/api-key/rate-limit.mjs +74 -0
  226. package/dist/plugins/api-key/rate-limit.mjs.map +1 -0
  227. package/dist/plugins/api-key/routes/create-api-key.mjs +252 -0
  228. package/dist/plugins/api-key/routes/create-api-key.mjs.map +1 -0
  229. package/dist/plugins/api-key/routes/delete-all-expired-api-keys.mjs +24 -0
  230. package/dist/plugins/api-key/routes/delete-all-expired-api-keys.mjs.map +1 -0
  231. package/dist/plugins/api-key/routes/delete-api-key.mjs +74 -0
  232. package/dist/plugins/api-key/routes/delete-api-key.mjs.map +1 -0
  233. package/dist/plugins/api-key/routes/get-api-key.mjs +158 -0
  234. package/dist/plugins/api-key/routes/get-api-key.mjs.map +1 -0
  235. package/dist/plugins/api-key/routes/index.mjs +71 -0
  236. package/dist/plugins/api-key/routes/index.mjs.map +1 -0
  237. package/dist/plugins/api-key/routes/list-api-keys.mjs +194 -0
  238. package/dist/plugins/api-key/routes/list-api-keys.mjs.map +1 -0
  239. package/dist/plugins/api-key/routes/update-api-key.mjs +248 -0
  240. package/dist/plugins/api-key/routes/update-api-key.mjs.map +1 -0
  241. package/dist/plugins/api-key/routes/verify-api-key.mjs +224 -0
  242. package/dist/plugins/api-key/routes/verify-api-key.mjs.map +1 -0
  243. package/dist/plugins/api-key/schema.d.mts +199 -0
  244. package/dist/plugins/api-key/schema.mjs +130 -0
  245. package/dist/plugins/api-key/schema.mjs.map +1 -0
  246. package/dist/plugins/api-key/types.d.mts +346 -0
  247. package/dist/plugins/bearer/index.d.mts +45 -0
  248. package/dist/plugins/bearer/index.mjs +66 -0
  249. package/dist/plugins/bearer/index.mjs.map +1 -0
  250. package/dist/plugins/captcha/constants.d.mts +10 -0
  251. package/dist/plugins/captcha/constants.mjs +22 -0
  252. package/dist/plugins/captcha/constants.mjs.map +1 -0
  253. package/dist/plugins/captcha/error-codes.mjs +16 -0
  254. package/dist/plugins/captcha/error-codes.mjs.map +1 -0
  255. package/dist/plugins/captcha/index.d.mts +21 -0
  256. package/dist/plugins/captcha/index.mjs +62 -0
  257. package/dist/plugins/captcha/index.mjs.map +1 -0
  258. package/dist/plugins/captcha/types.d.mts +28 -0
  259. package/dist/plugins/captcha/utils.mjs +11 -0
  260. package/dist/plugins/captcha/utils.mjs.map +1 -0
  261. package/dist/plugins/captcha/verify-handlers/captchafox.mjs +28 -0
  262. package/dist/plugins/captcha/verify-handlers/captchafox.mjs.map +1 -0
  263. package/dist/plugins/captcha/verify-handlers/cloudflare-turnstile.mjs +26 -0
  264. package/dist/plugins/captcha/verify-handlers/cloudflare-turnstile.mjs.map +1 -0
  265. package/dist/plugins/captcha/verify-handlers/google-recaptcha.mjs +30 -0
  266. package/dist/plugins/captcha/verify-handlers/google-recaptcha.mjs.map +1 -0
  267. package/dist/plugins/captcha/verify-handlers/h-captcha.mjs +28 -0
  268. package/dist/plugins/captcha/verify-handlers/h-captcha.mjs.map +1 -0
  269. package/dist/plugins/captcha/verify-handlers/index.mjs +6 -0
  270. package/dist/plugins/custom-session/client.d.mts +17 -0
  271. package/dist/plugins/custom-session/client.mjs +11 -0
  272. package/dist/plugins/custom-session/client.mjs.map +1 -0
  273. package/dist/plugins/custom-session/index.d.mts +72 -0
  274. package/dist/plugins/custom-session/index.mjs +78 -0
  275. package/dist/plugins/custom-session/index.mjs.map +1 -0
  276. package/dist/plugins/device-authorization/client.d.mts +17 -0
  277. package/dist/plugins/device-authorization/client.mjs +18 -0
  278. package/dist/plugins/device-authorization/client.mjs.map +1 -0
  279. package/dist/plugins/device-authorization/error-codes.mjs +21 -0
  280. package/dist/plugins/device-authorization/error-codes.mjs.map +1 -0
  281. package/dist/plugins/device-authorization/index.d.mts +424 -0
  282. package/dist/plugins/device-authorization/index.mjs +50 -0
  283. package/dist/plugins/device-authorization/index.mjs.map +1 -0
  284. package/dist/plugins/device-authorization/routes.mjs +510 -0
  285. package/dist/plugins/device-authorization/routes.mjs.map +1 -0
  286. package/dist/plugins/device-authorization/schema.mjs +57 -0
  287. package/dist/plugins/device-authorization/schema.mjs.map +1 -0
  288. package/dist/plugins/email-otp/client.d.mts +21 -0
  289. package/dist/plugins/email-otp/client.mjs +18 -0
  290. package/dist/plugins/email-otp/client.mjs.map +1 -0
  291. package/dist/plugins/email-otp/error-codes.d.mts +11 -0
  292. package/dist/plugins/email-otp/error-codes.mjs +12 -0
  293. package/dist/plugins/email-otp/error-codes.mjs.map +1 -0
  294. package/dist/plugins/email-otp/index.d.mts +428 -0
  295. package/dist/plugins/email-otp/index.mjs +130 -0
  296. package/dist/plugins/email-otp/index.mjs.map +1 -0
  297. package/dist/plugins/email-otp/otp-token.mjs +29 -0
  298. package/dist/plugins/email-otp/otp-token.mjs.map +1 -0
  299. package/dist/plugins/email-otp/routes.mjs +631 -0
  300. package/dist/plugins/email-otp/routes.mjs.map +1 -0
  301. package/dist/plugins/email-otp/types.d.mts +86 -0
  302. package/dist/plugins/email-otp/utils.mjs +17 -0
  303. package/dist/plugins/email-otp/utils.mjs.map +1 -0
  304. package/dist/plugins/generic-oauth/client.d.mts +33 -0
  305. package/dist/plugins/generic-oauth/client.mjs +14 -0
  306. package/dist/plugins/generic-oauth/client.mjs.map +1 -0
  307. package/dist/plugins/generic-oauth/error-codes.d.mts +16 -0
  308. package/dist/plugins/generic-oauth/error-codes.mjs +17 -0
  309. package/dist/plugins/generic-oauth/error-codes.mjs.map +1 -0
  310. package/dist/plugins/generic-oauth/index.d.mts +201 -0
  311. package/dist/plugins/generic-oauth/index.mjs +145 -0
  312. package/dist/plugins/generic-oauth/index.mjs.map +1 -0
  313. package/dist/plugins/generic-oauth/providers/auth0.d.mts +37 -0
  314. package/dist/plugins/generic-oauth/providers/auth0.mjs +62 -0
  315. package/dist/plugins/generic-oauth/providers/auth0.mjs.map +1 -0
  316. package/dist/plugins/generic-oauth/providers/gumroad.d.mts +32 -0
  317. package/dist/plugins/generic-oauth/providers/gumroad.mjs +60 -0
  318. package/dist/plugins/generic-oauth/providers/gumroad.mjs.map +1 -0
  319. package/dist/plugins/generic-oauth/providers/hubspot.d.mts +37 -0
  320. package/dist/plugins/generic-oauth/providers/hubspot.mjs +60 -0
  321. package/dist/plugins/generic-oauth/providers/hubspot.mjs.map +1 -0
  322. package/dist/plugins/generic-oauth/providers/index.d.mts +9 -0
  323. package/dist/plugins/generic-oauth/providers/index.mjs +11 -0
  324. package/dist/plugins/generic-oauth/providers/keycloak.d.mts +37 -0
  325. package/dist/plugins/generic-oauth/providers/keycloak.mjs +62 -0
  326. package/dist/plugins/generic-oauth/providers/keycloak.mjs.map +1 -0
  327. package/dist/plugins/generic-oauth/providers/line.d.mts +55 -0
  328. package/dist/plugins/generic-oauth/providers/line.mjs +91 -0
  329. package/dist/plugins/generic-oauth/providers/line.mjs.map +1 -0
  330. package/dist/plugins/generic-oauth/providers/microsoft-entra-id.d.mts +37 -0
  331. package/dist/plugins/generic-oauth/providers/microsoft-entra-id.mjs +66 -0
  332. package/dist/plugins/generic-oauth/providers/microsoft-entra-id.mjs.map +1 -0
  333. package/dist/plugins/generic-oauth/providers/okta.d.mts +37 -0
  334. package/dist/plugins/generic-oauth/providers/okta.mjs +62 -0
  335. package/dist/plugins/generic-oauth/providers/okta.mjs.map +1 -0
  336. package/dist/plugins/generic-oauth/providers/patreon.d.mts +30 -0
  337. package/dist/plugins/generic-oauth/providers/patreon.mjs +59 -0
  338. package/dist/plugins/generic-oauth/providers/patreon.mjs.map +1 -0
  339. package/dist/plugins/generic-oauth/providers/slack.d.mts +30 -0
  340. package/dist/plugins/generic-oauth/providers/slack.mjs +61 -0
  341. package/dist/plugins/generic-oauth/providers/slack.mjs.map +1 -0
  342. package/dist/plugins/generic-oauth/routes.mjs +411 -0
  343. package/dist/plugins/generic-oauth/routes.mjs.map +1 -0
  344. package/dist/plugins/generic-oauth/types.d.mts +159 -0
  345. package/dist/plugins/haveibeenpwned/index.d.mts +46 -0
  346. package/dist/plugins/haveibeenpwned/index.mjs +57 -0
  347. package/dist/plugins/haveibeenpwned/index.mjs.map +1 -0
  348. package/dist/plugins/index.d.mts +65 -0
  349. package/dist/plugins/index.mjs +48 -0
  350. package/dist/plugins/jwt/adapter.mjs +27 -0
  351. package/dist/plugins/jwt/adapter.mjs.map +1 -0
  352. package/dist/plugins/jwt/client.d.mts +40 -0
  353. package/dist/plugins/jwt/client.mjs +19 -0
  354. package/dist/plugins/jwt/client.mjs.map +1 -0
  355. package/dist/plugins/jwt/index.d.mts +224 -0
  356. package/dist/plugins/jwt/index.mjs +202 -0
  357. package/dist/plugins/jwt/index.mjs.map +1 -0
  358. package/dist/plugins/jwt/schema.d.mts +26 -0
  359. package/dist/plugins/jwt/schema.mjs +23 -0
  360. package/dist/plugins/jwt/schema.mjs.map +1 -0
  361. package/dist/plugins/jwt/sign.d.mts +57 -0
  362. package/dist/plugins/jwt/sign.mjs +66 -0
  363. package/dist/plugins/jwt/sign.mjs.map +1 -0
  364. package/dist/plugins/jwt/types.d.mts +194 -0
  365. package/dist/plugins/jwt/utils.d.mts +42 -0
  366. package/dist/plugins/jwt/utils.mjs +64 -0
  367. package/dist/plugins/jwt/utils.mjs.map +1 -0
  368. package/dist/plugins/jwt/verify.d.mts +12 -0
  369. package/dist/plugins/jwt/verify.mjs +46 -0
  370. package/dist/plugins/jwt/verify.mjs.map +1 -0
  371. package/dist/plugins/last-login-method/client.d.mts +38 -0
  372. package/dist/plugins/last-login-method/client.mjs +32 -0
  373. package/dist/plugins/last-login-method/client.mjs.map +1 -0
  374. package/dist/plugins/last-login-method/index.d.mts +118 -0
  375. package/dist/plugins/last-login-method/index.mjs +76 -0
  376. package/dist/plugins/last-login-method/index.mjs.map +1 -0
  377. package/dist/plugins/magic-link/client.d.mts +10 -0
  378. package/dist/plugins/magic-link/client.mjs +11 -0
  379. package/dist/plugins/magic-link/client.mjs.map +1 -0
  380. package/dist/plugins/magic-link/index.d.mts +193 -0
  381. package/dist/plugins/magic-link/index.mjs +177 -0
  382. package/dist/plugins/magic-link/index.mjs.map +1 -0
  383. package/dist/plugins/magic-link/utils.mjs +12 -0
  384. package/dist/plugins/magic-link/utils.mjs.map +1 -0
  385. package/dist/plugins/mcp/authorize.mjs +133 -0
  386. package/dist/plugins/mcp/authorize.mjs.map +1 -0
  387. package/dist/plugins/mcp/index.d.mts +458 -0
  388. package/dist/plugins/mcp/index.mjs +717 -0
  389. package/dist/plugins/mcp/index.mjs.map +1 -0
  390. package/dist/plugins/multi-session/client.d.mts +19 -0
  391. package/dist/plugins/multi-session/client.mjs +20 -0
  392. package/dist/plugins/multi-session/client.mjs.map +1 -0
  393. package/dist/plugins/multi-session/error-codes.d.mts +9 -0
  394. package/dist/plugins/multi-session/error-codes.mjs +8 -0
  395. package/dist/plugins/multi-session/error-codes.mjs.map +1 -0
  396. package/dist/plugins/multi-session/index.d.mts +235 -0
  397. package/dist/plugins/multi-session/index.mjs +172 -0
  398. package/dist/plugins/multi-session/index.mjs.map +1 -0
  399. package/dist/plugins/oauth-proxy/index.d.mts +97 -0
  400. package/dist/plugins/oauth-proxy/index.mjs +305 -0
  401. package/dist/plugins/oauth-proxy/index.mjs.map +1 -0
  402. package/dist/plugins/oauth-proxy/utils.mjs +51 -0
  403. package/dist/plugins/oauth-proxy/utils.mjs.map +1 -0
  404. package/dist/plugins/oidc-provider/authorize.mjs +194 -0
  405. package/dist/plugins/oidc-provider/authorize.mjs.map +1 -0
  406. package/dist/plugins/oidc-provider/client.d.mts +12 -0
  407. package/dist/plugins/oidc-provider/client.mjs +11 -0
  408. package/dist/plugins/oidc-provider/client.mjs.map +1 -0
  409. package/dist/plugins/oidc-provider/error.mjs +17 -0
  410. package/dist/plugins/oidc-provider/error.mjs.map +1 -0
  411. package/dist/plugins/oidc-provider/index.d.mts +702 -0
  412. package/dist/plugins/oidc-provider/index.mjs +1093 -0
  413. package/dist/plugins/oidc-provider/index.mjs.map +1 -0
  414. package/dist/plugins/oidc-provider/schema.d.mts +160 -0
  415. package/dist/plugins/oidc-provider/schema.mjs +132 -0
  416. package/dist/plugins/oidc-provider/schema.mjs.map +1 -0
  417. package/dist/plugins/oidc-provider/types.d.mts +517 -0
  418. package/dist/plugins/oidc-provider/utils/prompt.mjs +19 -0
  419. package/dist/plugins/oidc-provider/utils/prompt.mjs.map +1 -0
  420. package/dist/plugins/oidc-provider/utils.mjs +15 -0
  421. package/dist/plugins/oidc-provider/utils.mjs.map +1 -0
  422. package/dist/plugins/one-tap/client.d.mts +174 -0
  423. package/dist/plugins/one-tap/client.mjs +188 -0
  424. package/dist/plugins/one-tap/client.mjs.map +1 -0
  425. package/dist/plugins/one-tap/index.d.mts +83 -0
  426. package/dist/plugins/one-tap/index.mjs +95 -0
  427. package/dist/plugins/one-tap/index.mjs.map +1 -0
  428. package/dist/plugins/one-time-token/client.d.mts +10 -0
  429. package/dist/plugins/one-time-token/client.mjs +11 -0
  430. package/dist/plugins/one-time-token/client.mjs.map +1 -0
  431. package/dist/plugins/one-time-token/index.d.mts +133 -0
  432. package/dist/plugins/one-time-token/index.mjs +82 -0
  433. package/dist/plugins/one-time-token/index.mjs.map +1 -0
  434. package/dist/plugins/one-time-token/utils.mjs +12 -0
  435. package/dist/plugins/one-time-token/utils.mjs.map +1 -0
  436. package/dist/plugins/open-api/generator.d.mts +115 -0
  437. package/dist/plugins/open-api/generator.mjs +315 -0
  438. package/dist/plugins/open-api/generator.mjs.map +1 -0
  439. package/dist/plugins/open-api/index.d.mts +97 -0
  440. package/dist/plugins/open-api/index.mjs +67 -0
  441. package/dist/plugins/open-api/index.mjs.map +1 -0
  442. package/dist/plugins/open-api/logo.mjs +15 -0
  443. package/dist/plugins/open-api/logo.mjs.map +1 -0
  444. package/dist/plugins/organization/access/index.d.mts +2 -0
  445. package/dist/plugins/organization/access/index.mjs +3 -0
  446. package/dist/plugins/organization/access/statement.d.mts +249 -0
  447. package/dist/plugins/organization/access/statement.mjs +81 -0
  448. package/dist/plugins/organization/access/statement.mjs.map +1 -0
  449. package/dist/plugins/organization/adapter.d.mts +792 -0
  450. package/dist/plugins/organization/adapter.mjs +624 -0
  451. package/dist/plugins/organization/adapter.mjs.map +1 -0
  452. package/dist/plugins/organization/call.mjs +19 -0
  453. package/dist/plugins/organization/call.mjs.map +1 -0
  454. package/dist/plugins/organization/client.d.mts +372 -0
  455. package/dist/plugins/organization/client.mjs +95 -0
  456. package/dist/plugins/organization/client.mjs.map +1 -0
  457. package/dist/plugins/organization/error-codes.d.mts +65 -0
  458. package/dist/plugins/organization/error-codes.mjs +66 -0
  459. package/dist/plugins/organization/error-codes.mjs.map +1 -0
  460. package/dist/plugins/organization/has-permission.mjs +35 -0
  461. package/dist/plugins/organization/has-permission.mjs.map +1 -0
  462. package/dist/plugins/organization/index.d.mts +5 -0
  463. package/dist/plugins/organization/index.mjs +4 -0
  464. package/dist/plugins/organization/organization.d.mts +394 -0
  465. package/dist/plugins/organization/organization.mjs +428 -0
  466. package/dist/plugins/organization/organization.mjs.map +1 -0
  467. package/dist/plugins/organization/permission.d.mts +17 -0
  468. package/dist/plugins/organization/permission.mjs +16 -0
  469. package/dist/plugins/organization/permission.mjs.map +1 -0
  470. package/dist/plugins/organization/routes/crud-access-control.d.mts +394 -0
  471. package/dist/plugins/organization/routes/crud-access-control.mjs +678 -0
  472. package/dist/plugins/organization/routes/crud-access-control.mjs.map +1 -0
  473. package/dist/plugins/organization/routes/crud-invites.d.mts +1031 -0
  474. package/dist/plugins/organization/routes/crud-invites.mjs +551 -0
  475. package/dist/plugins/organization/routes/crud-invites.mjs.map +1 -0
  476. package/dist/plugins/organization/routes/crud-members.d.mts +940 -0
  477. package/dist/plugins/organization/routes/crud-members.mjs +466 -0
  478. package/dist/plugins/organization/routes/crud-members.mjs.map +1 -0
  479. package/dist/plugins/organization/routes/crud-org.d.mts +708 -0
  480. package/dist/plugins/organization/routes/crud-org.mjs +423 -0
  481. package/dist/plugins/organization/routes/crud-org.mjs.map +1 -0
  482. package/dist/plugins/organization/routes/crud-team.d.mts +1071 -0
  483. package/dist/plugins/organization/routes/crud-team.mjs +676 -0
  484. package/dist/plugins/organization/routes/crud-team.mjs.map +1 -0
  485. package/dist/plugins/organization/schema.d.mts +376 -0
  486. package/dist/plugins/organization/schema.mjs +68 -0
  487. package/dist/plugins/organization/schema.mjs.map +1 -0
  488. package/dist/plugins/organization/types.d.mts +677 -0
  489. package/dist/plugins/phone-number/client.d.mts +31 -0
  490. package/dist/plugins/phone-number/client.mjs +20 -0
  491. package/dist/plugins/phone-number/client.mjs.map +1 -0
  492. package/dist/plugins/phone-number/error-codes.d.mts +20 -0
  493. package/dist/plugins/phone-number/error-codes.mjs +21 -0
  494. package/dist/plugins/phone-number/error-codes.mjs.map +1 -0
  495. package/dist/plugins/phone-number/index.d.mts +318 -0
  496. package/dist/plugins/phone-number/index.mjs +49 -0
  497. package/dist/plugins/phone-number/index.mjs.map +1 -0
  498. package/dist/plugins/phone-number/routes.mjs +472 -0
  499. package/dist/plugins/phone-number/routes.mjs.map +1 -0
  500. package/dist/plugins/phone-number/schema.d.mts +23 -0
  501. package/dist/plugins/phone-number/schema.mjs +20 -0
  502. package/dist/plugins/phone-number/schema.mjs.map +1 -0
  503. package/dist/plugins/phone-number/types.d.mts +118 -0
  504. package/dist/plugins/two-factor/backup-codes/index.d.mts +279 -0
  505. package/dist/plugins/two-factor/backup-codes/index.mjs +277 -0
  506. package/dist/plugins/two-factor/backup-codes/index.mjs.map +1 -0
  507. package/dist/plugins/two-factor/client.d.mts +55 -0
  508. package/dist/plugins/two-factor/client.mjs +37 -0
  509. package/dist/plugins/two-factor/client.mjs.map +1 -0
  510. package/dist/plugins/two-factor/constant.mjs +8 -0
  511. package/dist/plugins/two-factor/constant.mjs.map +1 -0
  512. package/dist/plugins/two-factor/error-code.d.mts +17 -0
  513. package/dist/plugins/two-factor/error-code.mjs +18 -0
  514. package/dist/plugins/two-factor/error-code.mjs.map +1 -0
  515. package/dist/plugins/two-factor/index.d.mts +670 -0
  516. package/dist/plugins/two-factor/index.mjs +228 -0
  517. package/dist/plugins/two-factor/index.mjs.map +1 -0
  518. package/dist/plugins/two-factor/otp/index.d.mts +216 -0
  519. package/dist/plugins/two-factor/otp/index.mjs +199 -0
  520. package/dist/plugins/two-factor/otp/index.mjs.map +1 -0
  521. package/dist/plugins/two-factor/schema.d.mts +41 -0
  522. package/dist/plugins/two-factor/schema.mjs +36 -0
  523. package/dist/plugins/two-factor/schema.mjs.map +1 -0
  524. package/dist/plugins/two-factor/totp/index.d.mts +210 -0
  525. package/dist/plugins/two-factor/totp/index.mjs +157 -0
  526. package/dist/plugins/two-factor/totp/index.mjs.map +1 -0
  527. package/dist/plugins/two-factor/types.d.mts +73 -0
  528. package/dist/plugins/two-factor/utils.mjs +12 -0
  529. package/dist/plugins/two-factor/utils.mjs.map +1 -0
  530. package/dist/plugins/two-factor/verify-two-factor.mjs +85 -0
  531. package/dist/plugins/two-factor/verify-two-factor.mjs.map +1 -0
  532. package/dist/plugins/username/client.d.mts +26 -0
  533. package/dist/plugins/username/client.mjs +18 -0
  534. package/dist/plugins/username/client.mjs.map +1 -0
  535. package/dist/plugins/username/error-codes.d.mts +16 -0
  536. package/dist/plugins/username/error-codes.mjs +17 -0
  537. package/dist/plugins/username/error-codes.mjs.map +1 -0
  538. package/dist/plugins/username/index.d.mts +251 -0
  539. package/dist/plugins/username/index.mjs +234 -0
  540. package/dist/plugins/username/index.mjs.map +1 -0
  541. package/dist/plugins/username/schema.d.mts +33 -0
  542. package/dist/plugins/username/schema.mjs +26 -0
  543. package/dist/plugins/username/schema.mjs.map +1 -0
  544. package/dist/providers/index.d.mts +1 -0
  545. package/dist/providers/index.mjs +3 -0
  546. package/dist/state.d.mts +42 -0
  547. package/dist/state.mjs +107 -0
  548. package/dist/state.mjs.map +1 -0
  549. package/dist/types/adapter.d.mts +2 -0
  550. package/dist/types/api.d.mts +29 -0
  551. package/dist/types/auth.d.mts +29 -0
  552. package/dist/types/helper.d.mts +10 -0
  553. package/dist/types/index.d.mts +11 -0
  554. package/dist/types/index.mjs +1 -0
  555. package/dist/types/models.d.mts +11 -0
  556. package/dist/types/plugins.d.mts +20 -0
  557. package/dist/utils/boolean.mjs +8 -0
  558. package/dist/utils/boolean.mjs.map +1 -0
  559. package/dist/utils/constants.mjs +6 -0
  560. package/dist/utils/constants.mjs.map +1 -0
  561. package/dist/utils/date.mjs +8 -0
  562. package/dist/utils/date.mjs.map +1 -0
  563. package/dist/utils/get-request-ip.d.mts +7 -0
  564. package/dist/utils/get-request-ip.mjs +23 -0
  565. package/dist/utils/get-request-ip.mjs.map +1 -0
  566. package/dist/utils/hide-metadata.d.mts +7 -0
  567. package/dist/utils/hide-metadata.mjs +6 -0
  568. package/dist/utils/hide-metadata.mjs.map +1 -0
  569. package/dist/utils/index.d.mts +4 -0
  570. package/dist/utils/index.mjs +6 -0
  571. package/dist/utils/is-api-error.d.mts +7 -0
  572. package/dist/utils/is-api-error.mjs +11 -0
  573. package/dist/utils/is-api-error.mjs.map +1 -0
  574. package/dist/utils/is-atom.mjs +8 -0
  575. package/dist/utils/is-atom.mjs.map +1 -0
  576. package/dist/utils/is-promise.mjs +8 -0
  577. package/dist/utils/is-promise.mjs.map +1 -0
  578. package/dist/utils/middleware-response.mjs +9 -0
  579. package/dist/utils/middleware-response.mjs.map +1 -0
  580. package/dist/utils/password.mjs +26 -0
  581. package/dist/utils/password.mjs.map +1 -0
  582. package/dist/utils/plugin-helper.mjs +17 -0
  583. package/dist/utils/plugin-helper.mjs.map +1 -0
  584. package/dist/utils/shim.mjs +24 -0
  585. package/dist/utils/shim.mjs.map +1 -0
  586. package/dist/utils/time.d.mts +49 -0
  587. package/dist/utils/time.mjs +100 -0
  588. package/dist/utils/time.mjs.map +1 -0
  589. package/dist/utils/url.d.mts +8 -0
  590. package/dist/utils/url.mjs +92 -0
  591. package/dist/utils/url.mjs.map +1 -0
  592. package/dist/utils/wildcard.mjs +108 -0
  593. package/dist/utils/wildcard.mjs.map +1 -0
  594. package/package.json +428 -0
@@ -0,0 +1,841 @@
1
+ import { getDate } from "../../utils/date.mjs";
2
+ import { parseSessionOutput, parseUserOutput } from "../../db/schema.mjs";
3
+ import { deleteSessionCookie, expireCookie, setSessionCookie } from "../../cookies/index.mjs";
4
+ import { getSessionFromCtx } from "../../api/routes/session.mjs";
5
+ import "../../api/index.mjs";
6
+ import { ADMIN_ERROR_CODES } from "./error-codes.mjs";
7
+ import { hasPermission } from "./has-permission.mjs";
8
+ import { APIError, BASE_ERROR_CODES } from "@better-auth/core/error";
9
+ import { createAuthEndpoint, createAuthMiddleware } from "@better-auth/core/api";
10
+ import * as z from "zod";
11
+ import { whereOperators } from "@better-auth/core/db/adapter";
12
+
13
+ //#region src/plugins/admin/routes.ts
14
+ /**
15
+ * Ensures a valid session, if not will throw.
16
+ * Will also provide additional types on the user to include role types.
17
+ */
18
+ const adminMiddleware = createAuthMiddleware(async (ctx) => {
19
+ const session = await getSessionFromCtx(ctx);
20
+ if (!session) throw APIError.fromStatus("UNAUTHORIZED");
21
+ return { session };
22
+ });
23
+ function parseRoles(roles) {
24
+ return Array.isArray(roles) ? roles.join(",") : roles;
25
+ }
26
+ const setRoleBodySchema = z.object({
27
+ userId: z.coerce.string().meta({ description: "The user id" }),
28
+ role: z.union([z.string().meta({ description: "The role to set. `admin` or `user` by default" }), z.array(z.string().meta({ description: "The roles to set. `admin` or `user` by default" }))]).meta({ description: "The role to set, this can be a string or an array of strings. Eg: `admin` or `[admin, user]`" })
29
+ });
30
+ /**
31
+ * ### Endpoint
32
+ *
33
+ * POST `/admin/set-role`
34
+ *
35
+ * ### API Methods
36
+ *
37
+ * **server:**
38
+ * `auth.api.setRole`
39
+ *
40
+ * **client:**
41
+ * `authClient.admin.setRole`
42
+ *
43
+ * @see [Read our docs to learn more.](https://better-auth.com/docs/plugins/admin#api-method-admin-set-role)
44
+ */
45
+ const setRole = (opts) => createAuthEndpoint("/admin/set-role", {
46
+ method: "POST",
47
+ body: setRoleBodySchema,
48
+ requireHeaders: true,
49
+ use: [adminMiddleware],
50
+ metadata: {
51
+ openapi: {
52
+ operationId: "setUserRole",
53
+ summary: "Set the role of a user",
54
+ description: "Set the role of a user",
55
+ responses: { 200: {
56
+ description: "User role updated",
57
+ content: { "application/json": { schema: {
58
+ type: "object",
59
+ properties: { user: { $ref: "#/components/schemas/User" } }
60
+ } } }
61
+ } }
62
+ },
63
+ $Infer: { body: {} }
64
+ }
65
+ }, async (ctx) => {
66
+ if (!hasPermission({
67
+ userId: ctx.context.session.user.id,
68
+ role: ctx.context.session.user.role,
69
+ options: opts,
70
+ permissions: { user: ["set-role"] }
71
+ })) throw APIError.from("FORBIDDEN", ADMIN_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_CHANGE_USERS_ROLE);
72
+ const roles = opts.roles;
73
+ if (roles) {
74
+ const inputRoles = Array.isArray(ctx.body.role) ? ctx.body.role : [ctx.body.role];
75
+ for (const role of inputRoles) if (!roles[role]) throw APIError.from("BAD_REQUEST", ADMIN_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_SET_NON_EXISTENT_VALUE);
76
+ }
77
+ const updatedUser = await ctx.context.internalAdapter.updateUser(ctx.body.userId, { role: parseRoles(ctx.body.role) });
78
+ return ctx.json({ user: parseUserOutput(ctx.context.options, updatedUser) });
79
+ });
80
+ const getUserQuerySchema = z.object({ id: z.string().meta({ description: "The id of the User" }) });
81
+ const getUser = (opts) => createAuthEndpoint("/admin/get-user", {
82
+ method: "GET",
83
+ query: getUserQuerySchema,
84
+ use: [adminMiddleware],
85
+ metadata: { openapi: {
86
+ operationId: "getUser",
87
+ summary: "Get an existing user",
88
+ description: "Get an existing user",
89
+ responses: { 200: {
90
+ description: "User",
91
+ content: { "application/json": { schema: {
92
+ type: "object",
93
+ properties: { user: { $ref: "#/components/schemas/User" } }
94
+ } } }
95
+ } }
96
+ } }
97
+ }, async (ctx) => {
98
+ const { id } = ctx.query;
99
+ if (!hasPermission({
100
+ userId: ctx.context.session.user.id,
101
+ role: ctx.context.session.user.role,
102
+ options: opts,
103
+ permissions: { user: ["get"] }
104
+ })) throw APIError.from("FORBIDDEN", ADMIN_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_GET_USER);
105
+ const user = await ctx.context.internalAdapter.findUserById(id);
106
+ if (!user) throw APIError.from("NOT_FOUND", BASE_ERROR_CODES.USER_NOT_FOUND);
107
+ return parseUserOutput(ctx.context.options, user);
108
+ });
109
+ const createUserBodySchema = z.object({
110
+ email: z.string().meta({ description: "The email of the user" }),
111
+ password: z.string().optional().meta({ description: "The password of the user. If not provided, the user will be created without a credential account (useful for magic link or social login only users)." }),
112
+ name: z.string().meta({ description: "The name of the user" }),
113
+ role: z.union([z.string().meta({ description: "The role of the user" }), z.array(z.string().meta({ description: "The roles of user" }))]).optional().meta({ description: `A string or array of strings representing the roles to apply to the new user. Eg: \"user\"` }),
114
+ data: z.record(z.string(), z.any()).optional().meta({ description: "Extra fields for the user. Including custom additional fields." })
115
+ });
116
+ /**
117
+ * ### Endpoint
118
+ *
119
+ * POST `/admin/create-user`
120
+ *
121
+ * ### API Methods
122
+ *
123
+ * **server:**
124
+ * `auth.api.createUser`
125
+ *
126
+ * **client:**
127
+ * `authClient.admin.createUser`
128
+ *
129
+ * @see [Read our docs to learn more.](https://better-auth.com/docs/plugins/admin#api-method-admin-create-user)
130
+ */
131
+ const createUser = (opts) => createAuthEndpoint("/admin/create-user", {
132
+ method: "POST",
133
+ body: createUserBodySchema,
134
+ metadata: {
135
+ openapi: {
136
+ operationId: "createUser",
137
+ summary: "Create a new user",
138
+ description: "Create a new user",
139
+ responses: { 200: {
140
+ description: "User created",
141
+ content: { "application/json": { schema: {
142
+ type: "object",
143
+ properties: { user: { $ref: "#/components/schemas/User" } }
144
+ } } }
145
+ } }
146
+ },
147
+ $Infer: { body: {} }
148
+ }
149
+ }, async (ctx) => {
150
+ const session = await getSessionFromCtx(ctx);
151
+ if (!session && (ctx.request || ctx.headers)) throw ctx.error("UNAUTHORIZED");
152
+ if (session) {
153
+ if (!hasPermission({
154
+ userId: session.user.id,
155
+ role: session.user.role,
156
+ options: opts,
157
+ permissions: { user: ["create"] }
158
+ })) throw APIError.from("FORBIDDEN", ADMIN_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_CREATE_USERS);
159
+ }
160
+ const email = ctx.body.email.toLowerCase();
161
+ if (!z.email().safeParse(email).success) throw APIError.from("BAD_REQUEST", BASE_ERROR_CODES.INVALID_EMAIL);
162
+ if (await ctx.context.internalAdapter.findUserByEmail(email)) throw APIError.from("BAD_REQUEST", ADMIN_ERROR_CODES.USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL);
163
+ const user = await ctx.context.internalAdapter.createUser({
164
+ email,
165
+ name: ctx.body.name,
166
+ role: (ctx.body.role && parseRoles(ctx.body.role)) ?? opts?.defaultRole ?? "user",
167
+ ...ctx.body.data
168
+ });
169
+ if (!user) throw APIError.from("INTERNAL_SERVER_ERROR", ADMIN_ERROR_CODES.FAILED_TO_CREATE_USER);
170
+ if (ctx.body.password) {
171
+ const hashedPassword = await ctx.context.password.hash(ctx.body.password);
172
+ await ctx.context.internalAdapter.linkAccount({
173
+ accountId: user.id,
174
+ providerId: "credential",
175
+ password: hashedPassword,
176
+ userId: user.id
177
+ });
178
+ }
179
+ return ctx.json({ user: parseUserOutput(ctx.context.options, user) });
180
+ });
181
+ const adminUpdateUserBodySchema = z.object({
182
+ userId: z.coerce.string().meta({ description: "The user id" }),
183
+ data: z.record(z.any(), z.any()).meta({ description: "The user data to update" })
184
+ });
185
+ /**
186
+ * ### Endpoint
187
+ *
188
+ * POST `/admin/update-user`
189
+ *
190
+ * ### API Methods
191
+ *
192
+ * **server:**
193
+ * `auth.api.adminUpdateUser`
194
+ *
195
+ * **client:**
196
+ * `authClient.admin.updateUser`
197
+ *
198
+ * @see [Read our docs to learn more.](https://better-auth.com/docs/plugins/admin#api-method-admin-update-user)
199
+ */
200
+ const adminUpdateUser = (opts) => createAuthEndpoint("/admin/update-user", {
201
+ method: "POST",
202
+ body: adminUpdateUserBodySchema,
203
+ use: [adminMiddleware],
204
+ metadata: { openapi: {
205
+ operationId: "updateUser",
206
+ summary: "Update a user",
207
+ description: "Update a user's details",
208
+ responses: { 200: {
209
+ description: "User updated",
210
+ content: { "application/json": { schema: {
211
+ type: "object",
212
+ properties: { user: { $ref: "#/components/schemas/User" } }
213
+ } } }
214
+ } }
215
+ } }
216
+ }, async (ctx) => {
217
+ if (!hasPermission({
218
+ userId: ctx.context.session.user.id,
219
+ role: ctx.context.session.user.role,
220
+ options: opts,
221
+ permissions: { user: ["update"] }
222
+ })) throw APIError.from("FORBIDDEN", ADMIN_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_UPDATE_USERS);
223
+ if (Object.keys(ctx.body.data).length === 0) throw APIError.from("BAD_REQUEST", ADMIN_ERROR_CODES.NO_DATA_TO_UPDATE);
224
+ if (Object.prototype.hasOwnProperty.call(ctx.body.data, "role")) {
225
+ if (!hasPermission({
226
+ userId: ctx.context.session.user.id,
227
+ role: ctx.context.session.user.role,
228
+ options: opts,
229
+ permissions: { user: ["set-role"] }
230
+ })) throw APIError.from("FORBIDDEN", ADMIN_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_CHANGE_USERS_ROLE);
231
+ const roleValue = ctx.body.data.role;
232
+ const inputRoles = Array.isArray(roleValue) ? roleValue : [roleValue];
233
+ for (const role of inputRoles) {
234
+ if (typeof role !== "string") throw APIError.from("BAD_REQUEST", ADMIN_ERROR_CODES.INVALID_ROLE_TYPE);
235
+ if (opts.roles && !opts.roles[role]) throw APIError.from("BAD_REQUEST", ADMIN_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_SET_NON_EXISTENT_VALUE);
236
+ }
237
+ ctx.body.data.role = parseRoles(inputRoles);
238
+ }
239
+ const updatedUser = await ctx.context.internalAdapter.updateUser(ctx.body.userId, ctx.body.data);
240
+ return ctx.json(parseUserOutput(ctx.context.options, updatedUser));
241
+ });
242
+ const listUsersQuerySchema = z.object({
243
+ searchValue: z.string().optional().meta({ description: "The value to search for. Eg: \"some name\"" }),
244
+ searchField: z.enum(["email", "name"]).meta({ description: "The field to search in, defaults to email. Can be `email` or `name`. Eg: \"name\"" }).optional(),
245
+ searchOperator: z.enum([
246
+ "contains",
247
+ "starts_with",
248
+ "ends_with"
249
+ ]).meta({ description: "The operator to use for the search. Can be `contains`, `starts_with` or `ends_with`. Eg: \"contains\"" }).optional(),
250
+ limit: z.string().meta({ description: "The number of users to return" }).or(z.number()).optional(),
251
+ offset: z.string().meta({ description: "The offset to start from" }).or(z.number()).optional(),
252
+ sortBy: z.string().meta({ description: "The field to sort by" }).optional(),
253
+ sortDirection: z.enum(["asc", "desc"]).meta({ description: "The direction to sort by" }).optional(),
254
+ filterField: z.string().meta({ description: "The field to filter by" }).optional(),
255
+ filterValue: z.string().meta({ description: "The value to filter by" }).or(z.number()).or(z.boolean()).or(z.array(z.string())).or(z.array(z.number())).optional(),
256
+ filterOperator: z.enum(whereOperators).meta({ description: "The operator to use for the filter" }).optional()
257
+ });
258
+ const listUsers = (opts) => createAuthEndpoint("/admin/list-users", {
259
+ method: "GET",
260
+ use: [adminMiddleware],
261
+ query: listUsersQuerySchema,
262
+ metadata: { openapi: {
263
+ operationId: "listUsers",
264
+ summary: "List users",
265
+ description: "List users",
266
+ responses: { 200: {
267
+ description: "List of users",
268
+ content: { "application/json": { schema: {
269
+ type: "object",
270
+ properties: {
271
+ users: {
272
+ type: "array",
273
+ items: { $ref: "#/components/schemas/User" }
274
+ },
275
+ total: { type: "number" },
276
+ limit: { type: "number" },
277
+ offset: { type: "number" }
278
+ },
279
+ required: ["users", "total"]
280
+ } } }
281
+ } }
282
+ } }
283
+ }, async (ctx) => {
284
+ const session = ctx.context.session;
285
+ if (!hasPermission({
286
+ userId: ctx.context.session.user.id,
287
+ role: session.user.role,
288
+ options: opts,
289
+ permissions: { user: ["list"] }
290
+ })) throw APIError.from("FORBIDDEN", ADMIN_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_LIST_USERS);
291
+ const where = [];
292
+ if (ctx.query?.searchValue) where.push({
293
+ field: ctx.query.searchField || "email",
294
+ operator: ctx.query.searchOperator || "contains",
295
+ value: ctx.query.searchValue
296
+ });
297
+ if (ctx.query?.filterValue !== void 0) where.push({
298
+ field: ctx.query.filterField || "email",
299
+ operator: ctx.query.filterOperator || "eq",
300
+ value: ctx.query.filterValue
301
+ });
302
+ try {
303
+ const users = await ctx.context.internalAdapter.listUsers(Number(ctx.query?.limit) || void 0, Number(ctx.query?.offset) || void 0, ctx.query?.sortBy ? {
304
+ field: ctx.query.sortBy,
305
+ direction: ctx.query.sortDirection || "asc"
306
+ } : void 0, where.length ? where : void 0);
307
+ const total = await ctx.context.internalAdapter.countTotalUsers(where.length ? where : void 0);
308
+ return ctx.json({
309
+ users: users.map((user) => parseUserOutput(ctx.context.options, user)),
310
+ total,
311
+ limit: Number(ctx.query?.limit) || void 0,
312
+ offset: Number(ctx.query?.offset) || void 0
313
+ });
314
+ } catch {
315
+ return ctx.json({
316
+ users: [],
317
+ total: 0
318
+ });
319
+ }
320
+ });
321
+ const listUserSessionsBodySchema = z.object({ userId: z.coerce.string().meta({ description: "The user id" }) });
322
+ /**
323
+ * ### Endpoint
324
+ *
325
+ * POST `/admin/list-user-sessions`
326
+ *
327
+ * ### API Methods
328
+ *
329
+ * **server:**
330
+ * `auth.api.listUserSessions`
331
+ *
332
+ * **client:**
333
+ * `authClient.admin.listUserSessions`
334
+ *
335
+ * @see [Read our docs to learn more.](https://better-auth.com/docs/plugins/admin#api-method-admin-list-user-sessions)
336
+ */
337
+ const listUserSessions = (opts) => createAuthEndpoint("/admin/list-user-sessions", {
338
+ method: "POST",
339
+ use: [adminMiddleware],
340
+ body: listUserSessionsBodySchema,
341
+ metadata: { openapi: {
342
+ operationId: "listUserSessions",
343
+ summary: "List user sessions",
344
+ description: "List user sessions",
345
+ responses: { 200: {
346
+ description: "List of user sessions",
347
+ content: { "application/json": { schema: {
348
+ type: "object",
349
+ properties: { sessions: {
350
+ type: "array",
351
+ items: { $ref: "#/components/schemas/Session" }
352
+ } }
353
+ } } }
354
+ } }
355
+ } }
356
+ }, async (ctx) => {
357
+ const session = ctx.context.session;
358
+ if (!hasPermission({
359
+ userId: ctx.context.session.user.id,
360
+ role: session.user.role,
361
+ options: opts,
362
+ permissions: { session: ["list"] }
363
+ })) throw APIError.from("FORBIDDEN", ADMIN_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_LIST_USERS_SESSIONS);
364
+ const sessions = await ctx.context.internalAdapter.listSessions(ctx.body.userId);
365
+ return ctx.json({ sessions: sessions.map((s) => parseSessionOutput(ctx.context.options, s)) });
366
+ });
367
+ const unbanUserBodySchema = z.object({ userId: z.coerce.string().meta({ description: "The user id" }) });
368
+ /**
369
+ * ### Endpoint
370
+ *
371
+ * POST `/admin/unban-user`
372
+ *
373
+ * ### API Methods
374
+ *
375
+ * **server:**
376
+ * `auth.api.unbanUser`
377
+ *
378
+ * **client:**
379
+ * `authClient.admin.unbanUser`
380
+ *
381
+ * @see [Read our docs to learn more.](https://better-auth.com/docs/plugins/admin#api-method-admin-unban-user)
382
+ */
383
+ const unbanUser = (opts) => createAuthEndpoint("/admin/unban-user", {
384
+ method: "POST",
385
+ body: unbanUserBodySchema,
386
+ use: [adminMiddleware],
387
+ metadata: { openapi: {
388
+ operationId: "unbanUser",
389
+ summary: "Unban a user",
390
+ description: "Unban a user",
391
+ responses: { 200: {
392
+ description: "User unbanned",
393
+ content: { "application/json": { schema: {
394
+ type: "object",
395
+ properties: { user: { $ref: "#/components/schemas/User" } }
396
+ } } }
397
+ } }
398
+ } }
399
+ }, async (ctx) => {
400
+ const session = ctx.context.session;
401
+ if (!hasPermission({
402
+ userId: ctx.context.session.user.id,
403
+ role: session.user.role,
404
+ options: opts,
405
+ permissions: { user: ["ban"] }
406
+ })) throw APIError.from("FORBIDDEN", ADMIN_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_BAN_USERS);
407
+ const user = await ctx.context.internalAdapter.updateUser(ctx.body.userId, {
408
+ banned: false,
409
+ banExpires: null,
410
+ banReason: null,
411
+ updatedAt: /* @__PURE__ */ new Date()
412
+ });
413
+ return ctx.json({ user: parseUserOutput(ctx.context.options, user) });
414
+ });
415
+ const banUserBodySchema = z.object({
416
+ userId: z.coerce.string().meta({ description: "The user id" }),
417
+ banReason: z.string().meta({ description: "The reason for the ban" }).optional(),
418
+ banExpiresIn: z.number().meta({ description: "The number of seconds until the ban expires" }).optional()
419
+ });
420
+ /**
421
+ * ### Endpoint
422
+ *
423
+ * POST `/admin/ban-user`
424
+ *
425
+ * ### API Methods
426
+ *
427
+ * **server:**
428
+ * `auth.api.banUser`
429
+ *
430
+ * **client:**
431
+ * `authClient.admin.banUser`
432
+ *
433
+ * @see [Read our docs to learn more.](https://better-auth.com/docs/plugins/admin#api-method-admin-ban-user)
434
+ */
435
+ const banUser = (opts) => createAuthEndpoint("/admin/ban-user", {
436
+ method: "POST",
437
+ body: banUserBodySchema,
438
+ use: [adminMiddleware],
439
+ metadata: { openapi: {
440
+ operationId: "banUser",
441
+ summary: "Ban a user",
442
+ description: "Ban a user",
443
+ responses: { 200: {
444
+ description: "User banned",
445
+ content: { "application/json": { schema: {
446
+ type: "object",
447
+ properties: { user: { $ref: "#/components/schemas/User" } }
448
+ } } }
449
+ } }
450
+ } }
451
+ }, async (ctx) => {
452
+ const session = ctx.context.session;
453
+ if (!hasPermission({
454
+ userId: ctx.context.session.user.id,
455
+ role: session.user.role,
456
+ options: opts,
457
+ permissions: { user: ["ban"] }
458
+ })) throw APIError.from("FORBIDDEN", ADMIN_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_BAN_USERS);
459
+ if (!await ctx.context.internalAdapter.findUserById(ctx.body.userId)) throw APIError.from("NOT_FOUND", BASE_ERROR_CODES.USER_NOT_FOUND);
460
+ if (ctx.body.userId === ctx.context.session.user.id) throw APIError.from("BAD_REQUEST", ADMIN_ERROR_CODES.YOU_CANNOT_BAN_YOURSELF);
461
+ const user = await ctx.context.internalAdapter.updateUser(ctx.body.userId, {
462
+ banned: true,
463
+ banReason: ctx.body.banReason || opts?.defaultBanReason || "No reason",
464
+ banExpires: ctx.body.banExpiresIn ? getDate(ctx.body.banExpiresIn, "sec") : opts?.defaultBanExpiresIn ? getDate(opts.defaultBanExpiresIn, "sec") : void 0,
465
+ updatedAt: /* @__PURE__ */ new Date()
466
+ });
467
+ await ctx.context.internalAdapter.deleteSessions(ctx.body.userId);
468
+ return ctx.json({ user: parseUserOutput(ctx.context.options, user) });
469
+ });
470
+ const impersonateUserBodySchema = z.object({ userId: z.coerce.string().meta({ description: "The user id" }) });
471
+ /**
472
+ * ### Endpoint
473
+ *
474
+ * POST `/admin/impersonate-user`
475
+ *
476
+ * ### API Methods
477
+ *
478
+ * **server:**
479
+ * `auth.api.impersonateUser`
480
+ *
481
+ * **client:**
482
+ * `authClient.admin.impersonateUser`
483
+ *
484
+ * @see [Read our docs to learn more.](https://better-auth.com/docs/plugins/admin#api-method-admin-impersonate-user)
485
+ */
486
+ const impersonateUser = (opts) => createAuthEndpoint("/admin/impersonate-user", {
487
+ method: "POST",
488
+ body: impersonateUserBodySchema,
489
+ use: [adminMiddleware],
490
+ metadata: { openapi: {
491
+ operationId: "impersonateUser",
492
+ summary: "Impersonate a user",
493
+ description: "Impersonate a user",
494
+ responses: { 200: {
495
+ description: "Impersonation session created",
496
+ content: { "application/json": { schema: {
497
+ type: "object",
498
+ properties: {
499
+ session: { $ref: "#/components/schemas/Session" },
500
+ user: { $ref: "#/components/schemas/User" }
501
+ }
502
+ } } }
503
+ } }
504
+ } }
505
+ }, async (ctx) => {
506
+ if (!hasPermission({
507
+ userId: ctx.context.session.user.id,
508
+ role: ctx.context.session.user.role,
509
+ options: opts,
510
+ permissions: { user: ["impersonate"] }
511
+ })) throw APIError.from("FORBIDDEN", ADMIN_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_IMPERSONATE_USERS);
512
+ const targetUser = await ctx.context.internalAdapter.findUserById(ctx.body.userId);
513
+ if (!targetUser) throw APIError.from("NOT_FOUND", BASE_ERROR_CODES.USER_NOT_FOUND);
514
+ const adminRoles = (Array.isArray(opts.adminRoles) ? opts.adminRoles : opts.adminRoles?.split(",") || []).map((role) => role.trim());
515
+ const targetUserRole = (targetUser.role || opts.defaultRole || "user").split(",");
516
+ if (opts.allowImpersonatingAdmins !== true && (targetUserRole.some((role) => adminRoles.includes(role)) || opts.adminUserIds?.includes(targetUser.id))) throw APIError.from("FORBIDDEN", ADMIN_ERROR_CODES.YOU_CANNOT_IMPERSONATE_ADMINS);
517
+ const session = await ctx.context.internalAdapter.createSession(targetUser.id, true, {
518
+ impersonatedBy: ctx.context.session.user.id,
519
+ expiresAt: opts?.impersonationSessionDuration ? getDate(opts.impersonationSessionDuration, "sec") : getDate(3600, "sec")
520
+ }, true);
521
+ if (!session) throw APIError.from("INTERNAL_SERVER_ERROR", ADMIN_ERROR_CODES.FAILED_TO_CREATE_USER);
522
+ const authCookies = ctx.context.authCookies;
523
+ deleteSessionCookie(ctx);
524
+ const dontRememberMeCookie = await ctx.getSignedCookie(ctx.context.authCookies.dontRememberToken.name, ctx.context.secret);
525
+ const adminCookieProp = ctx.context.createAuthCookie("admin_session");
526
+ await ctx.setSignedCookie(adminCookieProp.name, `${ctx.context.session.session.token}:${dontRememberMeCookie || ""}`, ctx.context.secret, authCookies.sessionToken.attributes);
527
+ await setSessionCookie(ctx, {
528
+ session,
529
+ user: targetUser
530
+ }, true);
531
+ return ctx.json({
532
+ session,
533
+ user: parseUserOutput(ctx.context.options, targetUser)
534
+ });
535
+ });
536
+ /**
537
+ * ### Endpoint
538
+ *
539
+ * POST `/admin/stop-impersonating`
540
+ *
541
+ * ### API Methods
542
+ *
543
+ * **server:**
544
+ * `auth.api.stopImpersonating`
545
+ *
546
+ * **client:**
547
+ * `authClient.admin.stopImpersonating`
548
+ *
549
+ * @see [Read our docs to learn more.](https://better-auth.com/docs/plugins/admin#api-method-admin-stop-impersonating)
550
+ */
551
+ const stopImpersonating = () => createAuthEndpoint("/admin/stop-impersonating", {
552
+ method: "POST",
553
+ requireHeaders: true
554
+ }, async (ctx) => {
555
+ const session = await getSessionFromCtx(ctx);
556
+ if (!session) throw APIError.fromStatus("UNAUTHORIZED");
557
+ if (!session.session.impersonatedBy) throw APIError.fromStatus("BAD_REQUEST", { message: "You are not impersonating anyone" });
558
+ const user = await ctx.context.internalAdapter.findUserById(session.session.impersonatedBy);
559
+ if (!user) throw APIError.fromStatus("INTERNAL_SERVER_ERROR", { message: "Failed to find user" });
560
+ const adminSessionCookie = ctx.context.createAuthCookie("admin_session");
561
+ const adminCookie = await ctx.getSignedCookie(adminSessionCookie.name, ctx.context.secret);
562
+ if (!adminCookie) throw APIError.fromStatus("INTERNAL_SERVER_ERROR", { message: "Failed to find admin session" });
563
+ const [adminSessionToken, dontRememberMeCookie] = adminCookie?.split(":");
564
+ const adminSession = await ctx.context.internalAdapter.findSession(adminSessionToken);
565
+ if (!adminSession || adminSession.session.userId !== user.id) throw APIError.fromStatus("INTERNAL_SERVER_ERROR", { message: "Failed to find admin session" });
566
+ await ctx.context.internalAdapter.deleteSession(session.session.token);
567
+ await setSessionCookie(ctx, adminSession, !!dontRememberMeCookie);
568
+ expireCookie(ctx, adminSessionCookie);
569
+ return ctx.json({
570
+ session: parseSessionOutput(ctx.context.options, adminSession.session),
571
+ user: parseUserOutput(ctx.context.options, adminSession.user)
572
+ });
573
+ });
574
+ const revokeUserSessionBodySchema = z.object({ sessionToken: z.string().meta({ description: "The session token" }) });
575
+ /**
576
+ * ### Endpoint
577
+ *
578
+ * POST `/admin/revoke-user-session`
579
+ *
580
+ * ### API Methods
581
+ *
582
+ * **server:**
583
+ * `auth.api.revokeUserSession`
584
+ *
585
+ * **client:**
586
+ * `authClient.admin.revokeUserSession`
587
+ *
588
+ * @see [Read our docs to learn more.](https://better-auth.com/docs/plugins/admin#api-method-admin-revoke-user-session)
589
+ */
590
+ const revokeUserSession = (opts) => createAuthEndpoint("/admin/revoke-user-session", {
591
+ method: "POST",
592
+ body: revokeUserSessionBodySchema,
593
+ use: [adminMiddleware],
594
+ metadata: { openapi: {
595
+ operationId: "revokeUserSession",
596
+ summary: "Revoke a user session",
597
+ description: "Revoke a user session",
598
+ responses: { 200: {
599
+ description: "Session revoked",
600
+ content: { "application/json": { schema: {
601
+ type: "object",
602
+ properties: { success: { type: "boolean" } }
603
+ } } }
604
+ } }
605
+ } }
606
+ }, async (ctx) => {
607
+ const session = ctx.context.session;
608
+ if (!hasPermission({
609
+ userId: ctx.context.session.user.id,
610
+ role: session.user.role,
611
+ options: opts,
612
+ permissions: { session: ["revoke"] }
613
+ })) throw APIError.from("FORBIDDEN", ADMIN_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_REVOKE_USERS_SESSIONS);
614
+ await ctx.context.internalAdapter.deleteSession(ctx.body.sessionToken);
615
+ return ctx.json({ success: true });
616
+ });
617
+ const revokeUserSessionsBodySchema = z.object({ userId: z.coerce.string().meta({ description: "The user id" }) });
618
+ /**
619
+ * ### Endpoint
620
+ *
621
+ * POST `/admin/revoke-user-sessions`
622
+ *
623
+ * ### API Methods
624
+ *
625
+ * **server:**
626
+ * `auth.api.revokeUserSessions`
627
+ *
628
+ * **client:**
629
+ * `authClient.admin.revokeUserSessions`
630
+ *
631
+ * @see [Read our docs to learn more.](https://better-auth.com/docs/plugins/admin#api-method-admin-revoke-user-sessions)
632
+ */
633
+ const revokeUserSessions = (opts) => createAuthEndpoint("/admin/revoke-user-sessions", {
634
+ method: "POST",
635
+ body: revokeUserSessionsBodySchema,
636
+ use: [adminMiddleware],
637
+ metadata: { openapi: {
638
+ operationId: "revokeUserSessions",
639
+ summary: "Revoke all user sessions",
640
+ description: "Revoke all user sessions",
641
+ responses: { 200: {
642
+ description: "Sessions revoked",
643
+ content: { "application/json": { schema: {
644
+ type: "object",
645
+ properties: { success: { type: "boolean" } }
646
+ } } }
647
+ } }
648
+ } }
649
+ }, async (ctx) => {
650
+ const session = ctx.context.session;
651
+ if (!hasPermission({
652
+ userId: ctx.context.session.user.id,
653
+ role: session.user.role,
654
+ options: opts,
655
+ permissions: { session: ["revoke"] }
656
+ })) throw APIError.from("FORBIDDEN", ADMIN_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_REVOKE_USERS_SESSIONS);
657
+ await ctx.context.internalAdapter.deleteSessions(ctx.body.userId);
658
+ return ctx.json({ success: true });
659
+ });
660
+ const removeUserBodySchema = z.object({ userId: z.coerce.string().meta({ description: "The user id" }) });
661
+ /**
662
+ * ### Endpoint
663
+ *
664
+ * POST `/admin/remove-user`
665
+ *
666
+ * ### API Methods
667
+ *
668
+ * **server:**
669
+ * `auth.api.removeUser`
670
+ *
671
+ * **client:**
672
+ * `authClient.admin.removeUser`
673
+ *
674
+ * @see [Read our docs to learn more.](https://better-auth.com/docs/plugins/admin#api-method-admin-remove-user)
675
+ */
676
+ const removeUser = (opts) => createAuthEndpoint("/admin/remove-user", {
677
+ method: "POST",
678
+ body: removeUserBodySchema,
679
+ use: [adminMiddleware],
680
+ metadata: { openapi: {
681
+ operationId: "removeUser",
682
+ summary: "Remove a user",
683
+ description: "Delete a user and all their sessions and accounts. Cannot be undone.",
684
+ responses: { 200: {
685
+ description: "User removed",
686
+ content: { "application/json": { schema: {
687
+ type: "object",
688
+ properties: { success: { type: "boolean" } }
689
+ } } }
690
+ } }
691
+ } }
692
+ }, async (ctx) => {
693
+ const session = ctx.context.session;
694
+ if (!hasPermission({
695
+ userId: ctx.context.session.user.id,
696
+ role: session.user.role,
697
+ options: opts,
698
+ permissions: { user: ["delete"] }
699
+ })) throw APIError.from("FORBIDDEN", ADMIN_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_DELETE_USERS);
700
+ if (ctx.body.userId === ctx.context.session.user.id) throw APIError.from("BAD_REQUEST", ADMIN_ERROR_CODES.YOU_CANNOT_REMOVE_YOURSELF);
701
+ if (!await ctx.context.internalAdapter.findUserById(ctx.body.userId)) throw APIError.from("NOT_FOUND", BASE_ERROR_CODES.USER_NOT_FOUND);
702
+ await ctx.context.internalAdapter.deleteUser(ctx.body.userId);
703
+ return ctx.json({ success: true });
704
+ });
705
+ const setUserPasswordBodySchema = z.object({
706
+ newPassword: z.string().nonempty("newPassword cannot be empty").meta({ description: "The new password" }),
707
+ userId: z.coerce.string().nonempty("userId cannot be empty").meta({ description: "The user id" })
708
+ });
709
+ /**
710
+ * ### Endpoint
711
+ *
712
+ * POST `/admin/set-user-password`
713
+ *
714
+ * ### API Methods
715
+ *
716
+ * **server:**
717
+ * `auth.api.setUserPassword`
718
+ *
719
+ * **client:**
720
+ * `authClient.admin.setUserPassword`
721
+ *
722
+ * @see [Read our docs to learn more.](https://better-auth.com/docs/plugins/admin#api-method-admin-set-user-password)
723
+ */
724
+ const setUserPassword = (opts) => createAuthEndpoint("/admin/set-user-password", {
725
+ method: "POST",
726
+ body: setUserPasswordBodySchema,
727
+ use: [adminMiddleware],
728
+ metadata: { openapi: {
729
+ operationId: "setUserPassword",
730
+ summary: "Set a user's password",
731
+ description: "Set a user's password",
732
+ responses: { 200: {
733
+ description: "Password set",
734
+ content: { "application/json": { schema: {
735
+ type: "object",
736
+ properties: { status: { type: "boolean" } }
737
+ } } }
738
+ } }
739
+ } }
740
+ }, async (ctx) => {
741
+ if (!hasPermission({
742
+ userId: ctx.context.session.user.id,
743
+ role: ctx.context.session.user.role,
744
+ options: opts,
745
+ permissions: { user: ["set-password"] }
746
+ })) throw APIError.from("FORBIDDEN", ADMIN_ERROR_CODES.YOU_ARE_NOT_ALLOWED_TO_SET_USERS_PASSWORD);
747
+ const { newPassword, userId } = ctx.body;
748
+ const minPasswordLength = ctx.context.password.config.minPasswordLength;
749
+ if (newPassword.length < minPasswordLength) {
750
+ ctx.context.logger.error("Password is too short");
751
+ throw APIError.from("BAD_REQUEST", BASE_ERROR_CODES.PASSWORD_TOO_SHORT);
752
+ }
753
+ const maxPasswordLength = ctx.context.password.config.maxPasswordLength;
754
+ if (newPassword.length > maxPasswordLength) {
755
+ ctx.context.logger.error("Password is too long");
756
+ throw APIError.from("BAD_REQUEST", BASE_ERROR_CODES.PASSWORD_TOO_LONG);
757
+ }
758
+ const hashedPassword = await ctx.context.password.hash(newPassword);
759
+ await ctx.context.internalAdapter.updatePassword(userId, hashedPassword);
760
+ return ctx.json({ status: true });
761
+ });
762
+ const userHasPermissionBodySchema = z.object({
763
+ userId: z.coerce.string().optional().meta({ description: `The user id. Eg: "user-id"` }),
764
+ role: z.string().optional().meta({ description: `The role to check permission for. Eg: "admin"` })
765
+ }).and(z.union([z.object({
766
+ permission: z.record(z.string(), z.array(z.string())),
767
+ permissions: z.undefined()
768
+ }), z.object({
769
+ permission: z.undefined(),
770
+ permissions: z.record(z.string(), z.array(z.string()))
771
+ })]));
772
+ /**
773
+ * ### Endpoint
774
+ *
775
+ * POST `/admin/has-permission`
776
+ *
777
+ * ### API Methods
778
+ *
779
+ * **server:**
780
+ * `auth.api.userHasPermission`
781
+ *
782
+ * **client:**
783
+ * `authClient.admin.hasPermission`
784
+ *
785
+ * @see [Read our docs to learn more.](https://better-auth.com/docs/plugins/admin#api-method-admin-has-permission)
786
+ */
787
+ const userHasPermission = (opts) => {
788
+ return createAuthEndpoint("/admin/has-permission", {
789
+ method: "POST",
790
+ body: userHasPermissionBodySchema,
791
+ metadata: {
792
+ openapi: {
793
+ description: "Check if the user has permission",
794
+ requestBody: { content: { "application/json": { schema: {
795
+ type: "object",
796
+ properties: { permissions: {
797
+ type: "object",
798
+ description: "The permission to check"
799
+ } },
800
+ required: ["permissions"]
801
+ } } } },
802
+ responses: { "200": {
803
+ description: "Success",
804
+ content: { "application/json": { schema: {
805
+ type: "object",
806
+ properties: {
807
+ error: { type: "string" },
808
+ success: { type: "boolean" }
809
+ },
810
+ required: ["success"]
811
+ } } }
812
+ } }
813
+ },
814
+ $Infer: { body: {} }
815
+ }
816
+ }, async (ctx) => {
817
+ if (!ctx.body?.permissions) throw new APIError("BAD_REQUEST", { message: "invalid permission check. no permission(s) were passed." });
818
+ const session = await getSessionFromCtx(ctx);
819
+ if (!session && (ctx.request || ctx.headers)) throw new APIError("UNAUTHORIZED");
820
+ if (!session && !ctx.body.userId && !ctx.body.role) throw new APIError("BAD_REQUEST", { message: "user id or role is required" });
821
+ const user = session?.user || (ctx.body.role ? {
822
+ id: ctx.body.userId || "",
823
+ role: ctx.body.role
824
+ } : null) || (ctx.body.userId ? await ctx.context.internalAdapter.findUserById(ctx.body.userId) : null);
825
+ if (!user) throw new APIError("BAD_REQUEST", { message: "user not found" });
826
+ const result = hasPermission({
827
+ userId: user.id,
828
+ role: user.role,
829
+ options: opts,
830
+ permissions: ctx.body.permissions
831
+ });
832
+ return ctx.json({
833
+ error: null,
834
+ success: result
835
+ });
836
+ });
837
+ };
838
+
839
+ //#endregion
840
+ export { adminUpdateUser, banUser, createUser, getUser, impersonateUser, listUserSessions, listUsers, removeUser, revokeUserSession, revokeUserSessions, setRole, setUserPassword, stopImpersonating, unbanUser, userHasPermission };
841
+ //# sourceMappingURL=routes.mjs.map