mirror of
https://github.com/better-auth/better-auth.git
synced 2026-06-01 20:06:41 -05:00
feat: add one time token generator
This commit is contained in:
@@ -82,3 +82,4 @@ oneTimeToken({
|
||||
expiresIn: 10 // 10 minutes
|
||||
})
|
||||
```
|
||||
* **`generateToken`**: A custom token generator function that takes `session` object and a `ctx` as paramters.
|
||||
@@ -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,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user