UNPKG

better-auth

Version:

The most comprehensive authentication library for TypeScript.

118 lines (115 loc) 3.97 kB
import { z } from 'zod'; import 'better-call'; import { a as createAuthEndpoint, s as sessionMiddleware } from '../../shared/better-auth.oUCDPPbQ.mjs'; import '../../shared/better-auth.8zoxzg-F.mjs'; import '@better-auth/utils/base64'; import '@better-auth/utils/hmac'; import '@better-auth/utils/binary'; import '../../shared/better-auth.Cc72UxUH.mjs'; import '../../shared/better-auth.BZads9rH.mjs'; import '../organization/access/index.mjs'; import { g as generateRandomString } from '../../shared/better-auth.B4Qoxdgc.mjs'; import '../../shared/better-auth.Cqykj82J.mjs'; import 'defu'; import '@better-auth/utils/hash'; import '@noble/ciphers/chacha'; import '@noble/ciphers/utils'; import '@noble/ciphers/webcrypto'; import 'jose'; import '@noble/hashes/scrypt'; import '@better-auth/utils'; import '@better-auth/utils/hex'; import '@noble/hashes/utils'; import '@better-auth/utils/otp'; import '../admin/access/index.mjs'; import '@better-fetch/fetch'; import '@better-auth/utils/random'; import '../../shared/better-auth.fsvwNeUx.mjs'; import 'kysely'; import '../../cookies/index.mjs'; import '../../shared/better-auth.DdzSJf-n.mjs'; import '../../shared/better-auth.CW6D9eSx.mjs'; import '../../shared/better-auth.tB5eU6EY.mjs'; import '../../shared/better-auth.VTXNLFMT.mjs'; import '../../shared/better-auth.dn8_oqOu.mjs'; import '../../social-providers/index.mjs'; import '../../shared/better-auth.DufyW0qf.mjs'; import '../../shared/better-auth.BUPPRXfK.mjs'; import '../../shared/better-auth.DDEbWX-S.mjs'; import 'jose/errors'; import '../../shared/better-auth.ffWeg50w.mjs'; import '../../shared/better-auth.OuYYTHC7.mjs'; import '../access/index.mjs'; const oneTimeToken = (options) => { return { id: "one-time-token", endpoints: { generateOneTimeToken: createAuthEndpoint( "/one-time-token/generate", { method: "GET", use: [sessionMiddleware] }, async (c) => { if (options?.disableClientRequest && c.request) { throw c.error("BAD_REQUEST", { message: "Client requests are disabled" }); } const session = c.context.session; const token = options?.generateToken ? await options.generateToken(session, c) : generateRandomString(32); const expiresAt = new Date( Date.now() + (options?.expiresIn ?? 3) * 60 * 1e3 ); await c.context.internalAdapter.createVerificationValue({ value: session.session.token, identifier: `one-time-token:${token}`, expiresAt }); return c.json({ token }); } ), verifyOneTimeToken: createAuthEndpoint( "/one-time-token/verify", { method: "POST", body: z.object({ token: z.string() }) }, async (c) => { const { token } = c.body; const verificationValue = await c.context.internalAdapter.findVerificationValue( `one-time-token:${token}` ); if (!verificationValue) { throw c.error("BAD_REQUEST", { message: "Invalid token" }); } if (verificationValue.expiresAt < /* @__PURE__ */ new Date()) { await c.context.internalAdapter.deleteVerificationValue( verificationValue.id ); throw c.error("BAD_REQUEST", { message: "Token expired" }); } await c.context.internalAdapter.deleteVerificationValue( verificationValue.id ); const session = await c.context.internalAdapter.findSession( verificationValue.value ); if (!session) { throw c.error("BAD_REQUEST", { message: "Session not found" }); } return c.json(session); } ) } }; }; export { oneTimeToken };