feat: setup better-auth

This commit is contained in:
Henry
2025-05-02 23:40:34 +01:00
parent 5996ad19c3
commit 091fea6f38
23 changed files with 4696 additions and 30 deletions

View File

@@ -0,0 +1,9 @@
import baseConfig from "@kan/eslint-config/base";
/** @type {import('typescript-eslint').Config} */
export default [
{
ignores: [],
},
...baseConfig,
];

View File

@@ -0,0 +1,32 @@
{
"name": "@kan/auth",
"private": true,
"version": "0.1.0",
"type": "module",
"exports": {
".": "./src/index.ts"
},
"license": "MIT",
"scripts": {
"build": "tsc",
"clean": "git clean -xdf .cache .turbo dist node_modules",
"dev": "tsc",
"format": "prettier --check . --ignore-path ../../.gitignore",
"lint": "eslint",
"typecheck": "tsc --noEmit --emitDeclarationOnly false"
},
"devDependencies": {
"@kan/db": "workspace:*",
"@kan/eslint-config": "workspace:*",
"@kan/prettier-config": "workspace:*",
"@kan/shared": "workspace:*",
"@kan/tsconfig": "workspace:*",
"eslint": "catalog:",
"prettier": "catalog:",
"typescript": "catalog:"
},
"prettier": "@kan/prettier-config",
"dependencies": {
"better-auth": "^1.2.7"
}
}

52
packages/auth/src/auth.ts Normal file
View File

@@ -0,0 +1,52 @@
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { apiKey } from "better-auth/plugins";
import { magicLink } from "better-auth/plugins/magic-link";
import { createDrizzleClient } from "@kan/db/client";
import * as schema from "@kan/db/schema";
const db = createDrizzleClient();
console.log("GOOGLE_CLIENT_ID", process.env.GOOGLE_CLIENT_ID);
console.log("GOOGLE_CLIENT_SECRET", process.env.GOOGLE_CLIENT_SECRET);
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg",
schema: {
...schema,
user: schema.users,
},
}),
socialProviders: {
google: {
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
},
},
user: {
additionalFields: {
stripeCustomerId: {
type: "string",
required: false,
defaultValue: null,
input: false,
},
},
},
plugins: [
apiKey(),
magicLink({
sendMagicLink: async ({ email, token, url }, request) => {
// send email to user
},
}),
],
advanced: {
cookiePrefix: "kan",
database: {
generateId: false,
},
},
});

View File

@@ -0,0 +1,3 @@
import { createAuthClient } from "better-auth/react";
export const authClient = createAuthClient();

View File

@@ -0,0 +1,6 @@
import { auth } from "./auth";
import { authClient } from "./clients";
export const name = "auth";
export { auth, authClient };

View File

@@ -0,0 +1,6 @@
{
"extends": "@kan/tsconfig/internal-package.json",
"compilerOptions": {},
"include": ["*.ts", "src"],
"exclude": ["node_modules"]
}