momoneko
momoneko
BABetter Auth
Created by momoneko on 5/8/2025 in #help
Create user with custom ID
Is there a way to pass it the id I want from the outside? It doesn't seem to have any parameter that would allow me to do that 😦
7 replies
BABetter Auth
Created by momoneko on 5/6/2025 in #help
Infer type before initializing betterAuth
I think I got it. Here's the diff of the changes I did
--- oldAuthClass.ts 2025-05-07 18:11:43
+++ authClass.ts 2025-05-07 18:12:53
@@ -1,5 +1,5 @@
-import { betterAuth, BetterAuthOptions } from "better-auth";
-import { jwt } from "better-auth/plugins";
+import { betterAuth, BetterAuthOptions, UnionToIntersection } from "better-auth";
+import { anonymous, jwt } from "better-auth/plugins";


const authInstance = betterAuth({
@@ -12,19 +12,28 @@
},
plugins: [jwt()],
});
+const plugins = [anonymous, jwt];

+type Plugins = typeof plugins;
+type PlugInReturnTypes = ReturnType<Plugins[number]>;
+type PluginEndpoints = UnionToIntersection<PlugInReturnTypes['endpoints']>;
+type PluginApis = {
+ api: PluginEndpoints;
+};
+
export class AuthService {
- authInstance: ReturnType<typeof betterAuth>;
+ authInstance: ReturnType<typeof betterAuth> & PluginApis;
constructor(o: BetterAuthOptions) {
this.authInstance = betterAuth({
...o,
emailAndPassword: {
enabled: true,
},
advanced: {
cookiePrefix: "pva_",
},
- plugins: [jwt()],
+ plugins: plugins.map((plugin) => plugin())
});
}

--- oldAuthClass.ts 2025-05-07 18:11:43
+++ authClass.ts 2025-05-07 18:12:53
@@ -1,5 +1,5 @@
-import { betterAuth, BetterAuthOptions } from "better-auth";
-import { jwt } from "better-auth/plugins";
+import { betterAuth, BetterAuthOptions, UnionToIntersection } from "better-auth";
+import { anonymous, jwt } from "better-auth/plugins";


const authInstance = betterAuth({
@@ -12,19 +12,28 @@
},
plugins: [jwt()],
});
+const plugins = [anonymous, jwt];

+type Plugins = typeof plugins;
+type PlugInReturnTypes = ReturnType<Plugins[number]>;
+type PluginEndpoints = UnionToIntersection<PlugInReturnTypes['endpoints']>;
+type PluginApis = {
+ api: PluginEndpoints;
+};
+
export class AuthService {
- authInstance: ReturnType<typeof betterAuth>;
+ authInstance: ReturnType<typeof betterAuth> & PluginApis;
constructor(o: BetterAuthOptions) {
this.authInstance = betterAuth({
...o,
emailAndPassword: {
enabled: true,
},
advanced: {
cookiePrefix: "pva_",
},
- plugins: [jwt()],
+ plugins: plugins.map((plugin) => plugin())
});
}

The interesting part would be this
const plugins = [anonymous, jwt];

type Plugins = typeof plugins;
type PlugInReturnTypes = ReturnType<Plugins[number]>;
type PluginEndpoints = UnionToIntersection<PlugInReturnTypes['endpoints']>;
type PluginApis = {
api: PluginEndpoints;
};
const plugins = [anonymous, jwt];

type Plugins = typeof plugins;
type PlugInReturnTypes = ReturnType<Plugins[number]>;
type PluginEndpoints = UnionToIntersection<PlugInReturnTypes['endpoints']>;
type PluginApis = {
api: PluginEndpoints;
};
Have fun 😉
3 replies