feat: add one time token generator

This commit is contained in:
Bereket Engida
2025-05-15 20:24:56 -07:00
parent c717df986f
commit 72e66ddc7f
2 changed files with 15 additions and 1 deletions

View File

@@ -82,3 +82,4 @@ oneTimeToken({
expiresIn: 10 // 10 minutes
})
```
* **`generateToken`**: A custom token generator function that takes `session` object and a `ctx` as paramters.

View File

@@ -2,6 +2,7 @@ import { z } from "zod";
import { createAuthEndpoint, type BetterAuthPlugin } from "..";
import { sessionMiddleware } from "../../api";
import { generateRandomString } from "../../crypto";
import type { GenericEndpointContext, Session, User } from "../../types";
interface OneTimeTokenOptions {
/**
@@ -14,6 +15,16 @@ interface OneTimeTokenOptions {
* Only allow server initiated requests
*/
disableClientRequest?: boolean;
/**
* Generate a custom token
*/
generateToken?: (
session: {
user: User & Record<string, any>;
session: Session & Record<string, any>;
},
ctx: GenericEndpointContext,
) => Promise<string>;
}
export const oneTimeToken = (options?: OneTimeTokenOptions) => {
@@ -34,7 +45,9 @@ export const oneTimeToken = (options?: OneTimeTokenOptions) => {
});
}
const session = c.context.session;
const token = generateRandomString(32);
const token = options?.generateToken
? await options.generateToken(session, c)
: generateRandomString(32);
const expiresAt = new Date(
Date.now() + (options?.expiresIn ?? 3) * 60 * 1000,
);