intial setting and auth intergrated
This commit is contained in:
@@ -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,
|
||||
|
||||
|
||||
Reference in New Issue
Block a user