Typescript - how to infer additional, non-database fields during registration

Hi everyone, I'm implementing the email credential flow. When signing up, I want the user to also specify a registrationToken similar to how matrix.org (decentralised chat client) does it. This registrationToken should not be stored in the database (it's used strictly during the sign up process). The code I've written works fine, but I can't find from the docs a way to remove the type error - could you please provide some guidance? Thank you!
No description
No description
No description
3 Replies
nktnet
nktnetOP3mo ago
After some investigation, I discovered that the following hack/workaround can be used to remove the type error:
export const { signIn, signUp, signOut, useSession } = createAuthClient({
baseURL: process.env.BETTER_AUTH_URL ?? 'http://localhost:3000',
plugins: [
inferAdditionalFields({
user: {
registrationToken: {
type: 'string',
},
},
}),
],
});
export const { signIn, signUp, signOut, useSession } = createAuthClient({
baseURL: process.env.BETTER_AUTH_URL ?? 'http://localhost:3000',
plugins: [
inferAdditionalFields({
user: {
registrationToken: {
type: 'string',
},
},
}),
],
});
Note: if we instead follow the docs and use
import { inferAdditionalFields } from 'better-auth/client/plugins';
import { createAuthClient } from 'better-auth/react';
import type { auth } from '@/lib/auth';

export const { signIn, signUp, signOut, useSession } = createAuthClient({
baseURL: process.env.BETTER_AUTH_URL ?? 'http://localhost:3000',
plugins: [
inferAdditionalFields<typeof auth>({
user: {
registrationToken: {
type: 'string',
},
},
}),
],
});
import { inferAdditionalFields } from 'better-auth/client/plugins';
import { createAuthClient } from 'better-auth/react';
import type { auth } from '@/lib/auth';

export const { signIn, signUp, signOut, useSession } = createAuthClient({
baseURL: process.env.BETTER_AUTH_URL ?? 'http://localhost:3000',
plugins: [
inferAdditionalFields<typeof auth>({
user: {
registrationToken: {
type: 'string',
},
},
}),
],
});
to try to sync with the server auth types, the hack for registrationToken will no longer work (it gets ignored). Is there a better way? Is there a better way?
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
nktnet
nktnetOP3mo ago
The issue is, registrationToken is not a user field to be stored in the database and won't be inferred - it's a variable that the frontend submits to the backend, solely for the purpose of comparing against the backend's REGISTRATION_TOKEN variable (similar to how a matrix server would require one to register)

Did you find this page helpful?