feat(auth): setup login and sign up and can use email or username
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user