remakker
remakker
BABetter Auth
Created by remakker on 3/21/2025 in #help
ctx.redirect not working?
When the user is signing in, I want to check for something in the additional fields and redirect based on that. But even this simple redirect script doesn't seem to work.
export const auth = betterAuth({
// other config
hooks: {
after: createAuthMiddleware(async (ctx) => {
if (ctx.path.startsWith('/sign-in')) {
console.log("Redirecting to /test");
throw ctx.redirect('/test');
}
}),
}
})
export const auth = betterAuth({
// other config
hooks: {
after: createAuthMiddleware(async (ctx) => {
if (ctx.path.startsWith('/sign-in')) {
console.log("Redirecting to /test");
throw ctx.redirect('/test');
}
}),
}
})
3 replies
BABetter Auth
Created by remakker on 1/21/2025 in #help
Type mismatch between auth.api.getSession and typeof user.$inferSelect
I have a backend server with express and drizzle. I get the type of the userTable with typeof user.$inferSelect This type is different from the type of session.user that returns from auth.api.getSession.
export type SelectUser = typeof user.$inferSelect;
type SelectUser = {
id: string;
name: string;
email: string;
emailVerified: boolean;
image: string | null;
createdAt: Date;
updatedAt: Date;
role: string | null;
banned: boolean | null;
banReason: string | null;
banExpires: Date | null;
usid: string | null;
}
export type SelectUser = typeof user.$inferSelect;
type SelectUser = {
id: string;
name: string;
email: string;
emailVerified: boolean;
image: string | null;
createdAt: Date;
updatedAt: Date;
role: string | null;
banned: boolean | null;
banReason: string | null;
banExpires: Date | null;
usid: string | null;
}
export const createContext = async (opts: CreateExpressContextOptions) => {
const session = await auth.api.getSession({
headers: fromNodeHeaders(opts.req.headers)
});
const user = session?.user
};
const user: {
id: string;
email: string;
emailVerified: boolean;
name: string;
createdAt: Date;
updatedAt: Date;
image: string | null | undefined;
banned: boolean | null | undefined;
role: string | null | undefined;
banReason: string | null | undefined;
banExpires: Date | null | undefined;
usid: string | null | undefined;
} | undefined
export const createContext = async (opts: CreateExpressContextOptions) => {
const session = await auth.api.getSession({
headers: fromNodeHeaders(opts.req.headers)
});
const user = session?.user
};
const user: {
id: string;
email: string;
emailVerified: boolean;
name: string;
createdAt: Date;
updatedAt: Date;
image: string | null | undefined;
banned: boolean | null | undefined;
role: string | null | undefined;
banReason: string | null | undefined;
banExpires: Date | null | undefined;
usid: string | null | undefined;
} | undefined
Is there a way to remove the undefined from the type getSession().user?
1 replies
BABetter Auth
Created by remakker on 1/20/2025 in #help
Server additionalFields Error
When following this example from the docs: https://www.better-auth.com/docs/concepts/database#extending-core-schema, I get a few errors, based on the request I send.
// server auth.ts
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "sqlite",
schema: {
user,
session,
account,
verification,
},
}),
emailAndPassword: {
enabled: true,
},
user: {
additionalFields: {
role: {
type: "string",
input: false,
default: "user",
required: true,
},
usid: {
type: "string",
input: false,
required: false,
},
},
},
});
// server auth.ts
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "sqlite",
schema: {
user,
session,
account,
verification,
},
}),
emailAndPassword: {
enabled: true,
},
user: {
additionalFields: {
role: {
type: "string",
input: false,
default: "user",
required: true,
},
usid: {
type: "string",
input: false,
required: false,
},
},
},
});
// also on server
await auth.api.signUpEmail({
body: {
password: "password123",
name: 'test123',
}
})
// also on server
await auth.api.signUpEmail({
body: {
password: "password123",
name: 'test123',
}
})
If i execute this I get ROLE_IS_REQUIRED error, but there was no Typescript error, which is not good. Also why do I get this error if I have defined a default value in my auth instance?
// request with role included
await auth.api.signUpEmail({
body: {
password: "password123",
name: 'test123',
role: "user"
}
})
// request with role included
await auth.api.signUpEmail({
body: {
password: "password123",
name: 'test123',
role: "user"
}
})
When I execute this request, I get a long error, with this message: SQLite error: NOT NULL constraint failed: user.role,
Where is this going wrong?
6 replies