intial setting and auth intergrated

This commit is contained in:
2026-02-24 15:53:58 -06:00
parent 326c2e125c
commit c3379919b9
17 changed files with 304 additions and 52 deletions

View File

@@ -4,12 +4,12 @@ import {
admin,
apiKey,
createAuthMiddleware,
customSession,
//customSession,
jwt,
lastLoginMethod,
username,
} from "better-auth/plugins";
import { eq } from "drizzle-orm";
//import { eq } from "drizzle-orm";
import { db } from "../db/db.controller.js";
import * as rawSchema from "../db/schema/auth.schema.js";
import { allowedOrigins } from "./cors.utils.js";
@@ -31,20 +31,15 @@ export const auth = betterAuth({
provider: "pg",
schema,
}),
user: {
additionalFields: {
role: {
type: "string",
required: false,
input: false,
},
lastLogin: {
type: "date",
required: true,
input: false,
},
},
},
// user: {
// additionalFields: {
// role: {
// type: "string",
// //required: false,
// input: false,
// },
// },
// },
plugins: [
jwt({ jwt: { expirationTime: "1h" } }),
apiKey(),
@@ -53,26 +48,27 @@ export const auth = betterAuth({
username({
minUsernameLength: 5,
usernameValidator: (username) => {
if (username === "admin") {
if (username === "admin" || username === "root") {
return false;
}
return true;
},
}),
customSession(async ({ user, session }) => {
const roles = await db
.select({ roles: rawSchema.user.role })
.from(rawSchema.user)
.where(eq(rawSchema.user.id, session.id));
return {
roles,
user: {
...user,
//newField: "newField",
},
session,
};
}),
// customSession(async ({ user, session }) => {
// const roles = await db
// .select({ roles: rawSchema.user.role })
// .from(rawSchema.user)
// .where(eq(rawSchema.user.id, session.id));
// return {
// roles,
// user: {
// ...user,
// //newField: "newField",
// },
// session,
// };
// }),
],
trustedOrigins: allowedOrigins,