Files
portabase/src/lib/auth/auth.ts
T

301 lines
7.6 KiB
TypeScript
Raw Normal View History

2025-05-18 20:46:59 +02:00
import {betterAuth} from "better-auth";
import {drizzleAdapter} from "better-auth/adapters/drizzle";
import {db} from "@/db";
import {env} from "@/env.mjs";
import {nextCookies} from "better-auth/next-js";
import {admin as adminPlugin, openAPI, organization} from "better-auth/plugins";
import {ac, admin, orgAdmin, orgMember, orgOwner, pending, superadmin, user} from "@/lib/auth/permissions";
2025-05-16 15:54:17 +02:00
2025-05-18 20:46:59 +02:00
import {headers} from "next/headers";
import {count, eq} from "drizzle-orm";
2025-05-16 15:54:17 +02:00
import * as drizzleUser from "@/db/schema/01_user";
import * as drizzleOrganization from "@/db/schema/02_organization";
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg",
schema: {
...drizzleUser,
...drizzleOrganization,
},
}),
emailAndPassword: {
enabled: true,
requireEmailVerification: false,
/*async sendResetPassword(data, request) {
// Send an email to the user with a link to reset their password
},
async sendVerificationEmail(data, request) {
// Send an email to the user with a link to verify their email
},
async verifyEmail(data, request) {
// Verify the email address
},*/
},
socialProviders: {
google: {
clientId: env.AUTH_GOOGLE_ID!,
clientSecret: env.AUTH_GOOGLE_SECRET!,
},
},
plugins: [
openAPI(),
nextCookies(),
organization({
ac,
roles: {
orgOwner,
orgAdmin,
orgMember,
},
}),
adminPlugin({
adminRoles: ["admin", "superadmin"],
defaultRole: (await db.select({ count: count() }).from(drizzleUser["user"]))[0].count === 0 ? "superadmin" : "pending",
ac,
roles: {
admin,
user,
pending,
superadmin,
},
}),
],
advanced: {
database: {
generateId: false,
},
},
user: {
additionalFields: {
deletedAt: {
type: "number", //pg timestamp
nullable: true,
required: false,
},
},
},
session: {
additionalFields: {
activeOrganizationId: {
type: "string",
required: false,
},
},
},
/* databaseHooks: {
session: {
create: {
before: async (session) => {
const organizationId = await getLastOrganizationOrFirst(session.userId);
if (!organizationId) {
return {
...session,
};
}
console.log("sessionId", session.id);
const [aa] = await db
.update(drizzleUser.session)
.set({ activeOrganizationId: organizationId })
.where(eq(drizzleUser.session.id, session.id))
.returning();
console.log("aaaaa", aa);
return {
...session,
activeOrganizationId: organizationId,
};
},
},
},
},*/
trustedOrigins: [env.NEXT_PUBLIC_PROJECT_URL, "http://app"],
});
/*export const signUpUser = async (email: string, password: string, name: string) => {
const user = await auth.api.signUpEmail({
body: {
email,
password,
name,
},
});
return user;
};
export const signInUser = async (email: string, password: string) => {
const user = await auth.api.signInEmail({
body: {
email,
password,
},
});
return user;
};*/
export const createUser = async (name: string, email: string, password: string, role: "user" | "pending" | "admin" | "superadmin" = "pending") => {
2025-05-18 20:46:59 +02:00
return await auth.api.createUser({
2025-05-16 15:54:17 +02:00
headers: await headers(),
body: {
name,
email,
password,
role,
},
});
};
export const getSessions = async () => {
2025-05-18 20:46:59 +02:00
return await auth.api.listSessions({
2025-05-16 15:54:17 +02:00
headers: await headers(),
});
};
export const getSession = async () => {
2025-05-18 20:46:59 +02:00
return await auth.api.getSession({
2025-05-16 15:54:17 +02:00
headers: await headers(),
});
};
export const revokeSession = async (e: string) => {
try {
const { status } = await auth.api.revokeSession({
body: {
token: e,
},
headers: await headers(),
});
return status;
} catch (e) {}
};
export const getAccounts = async () => {
2025-05-18 20:46:59 +02:00
return await auth.api.listUserAccounts({
2025-05-16 15:54:17 +02:00
headers: await headers(),
});
};
export const unlinkAccount = async (provider: string, account: string) => {
try {
const { status } = await auth.api.unlinkAccount({
body: {
providerId: provider,
accountId: account,
},
headers: await headers(),
});
return status;
} catch (e) {}
};
2025-05-18 20:46:59 +02:00
export const getOrganization = async ({
organizationId,
organizationSlug,
}: {
organizationId?: string;
organizationSlug?: string;
}) => {
const query = organizationId
? { organizationId }
: { organizationSlug };
2025-05-16 15:54:17 +02:00
2025-05-18 20:46:59 +02:00
console.log(query);
try {
return await auth.api.getFullOrganization({
headers: await headers(),
query,
});
} catch (e) {
console.error(e);
return null;
}
2025-05-16 15:54:17 +02:00
};
export const listOrganizations = async () => {
try {
2025-05-18 20:46:59 +02:00
return await auth.api.listOrganizations({
2025-05-16 15:54:17 +02:00
headers: await headers(),
});
} catch (e) {}
};
export const getLastOrganizationOrFirst = async (userId: string) => {
try {
const organizations = await db.query.organizationMember.findMany({
where: eq(drizzleOrganization.member.userId, userId),
});
if (organizations.length > 0) {
return organizations[0].id;
}
return null;
} catch (e) {
return null;
}
};
export const createOrganization = async (name: string, slug: string) => {
try {
const organization = await auth.api.createOrganization({
headers: await headers(),
body: {
name,
slug,
},
});
return organization;
} catch (e) {
console.log("err", e);
}
};
export const checkSlugOrganization = async (slug: string) => {
try {
const { status } = await auth.api.checkOrganizationSlug({
headers: await headers(),
body: {
slug,
},
});
return status;
} catch (e) {
console.log("err", e);
}
};
export const getActiveMember = async () => {
try {
const member = await auth.api.getActiveMember({
headers: await headers(),
});
return member;
} catch (e) {
console.log("err", e);
}
};
export const setActiveOrganization = async (slug: string) => {
try {
const organization = await auth.api.setActiveOrganization({
headers: await headers(),
body: {
organizationSlug: slug,
},
});
return organization;
} catch (e) {
console.log("error", e);
}
};