feat(auth): setup login and sign up and can use email or username

This commit is contained in:
2025-12-30 21:04:26 -06:00
parent ff2cd7e9f8
commit 9eeede7fbe
14 changed files with 563 additions and 34 deletions

View File

@@ -1,6 +1,12 @@
import { betterAuth, type User } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { admin, apiKey, jwt, username } from "better-auth/plugins";
import {
admin,
apiKey,
customSession,
jwt,
username,
} from "better-auth/plugins";
import { eq } from "drizzle-orm";
import { db } from "../db/db.controller.js";
import * as rawSchema from "../db/schema/auth.schema.js";
@@ -21,15 +27,52 @@ export const auth = betterAuth({
baseURL: process.env.URL,
database: drizzleAdapter(db, {
provider: "pg",
schema,
}),
user: {
additionalFields: {
role: {
type: "string",
required: false,
input: false,
},
lastLogin: {
type: "date",
required: false,
input: false,
},
},
},
plugins: [
jwt({ jwt: { expirationTime: "1h" } }),
apiKey(),
admin(),
username(),
username({
minUsernameLength: 5,
usernameValidator: (username) => {
if (username === "admin") {
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,
};
}),
],
trustedOrigins: allowedOrigins,
// email or username and password.
emailAndPassword: {
enabled: true,
minPasswordLength: 8, // optional config

View File

@@ -4,7 +4,14 @@ import { createLogger } from "../logger/logger.controller.js";
interface Data {
success: boolean;
module: "system" | "ocp" | "routes" | "datamart" | "utils";
subModule: "db" | "labeling" | "printer" | "prodSql" | "query" | "sendmail";
subModule:
| "db"
| "labeling"
| "printer"
| "prodSql"
| "query"
| "sendmail"
| "auth";
level: "info" | "error" | "debug" | "fatal";
message: string;
data?: unknown[];