auth is working
This commit is contained in:
parent
71d0b0be3c
commit
a7e30cfce2
13 changed files with 67 additions and 60 deletions
BIN
bun.lockb
BIN
bun.lockb
Binary file not shown.
|
|
@ -5,6 +5,8 @@ services:
|
|||
dockerfile: Dockerfile
|
||||
ports:
|
||||
- "3000:3000"
|
||||
env_file:
|
||||
- .env
|
||||
environment:
|
||||
- NODE_ENV=production
|
||||
- OTEL_EXPORTER_OTLP_ENDPOINT=http://tracing:4318
|
||||
|
|
@ -27,10 +29,14 @@ services:
|
|||
|
||||
db:
|
||||
image: postgres:latest
|
||||
env_file:
|
||||
- .env
|
||||
environment:
|
||||
POSTGRES_USER: ${DB_USER}
|
||||
POSTGRES_PASSWORD: ${DB_PASSWORD}
|
||||
POSTGRES_DB: ${DB_NAME}
|
||||
ports:
|
||||
- "5432:5432"
|
||||
volumes:
|
||||
- postgres_data:/var/lib/postgresql/data
|
||||
networks:
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
CREATE TABLE "account" (
|
||||
CREATE SCHEMA "auth";
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "auth"."account" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"account_id" text NOT NULL,
|
||||
"provider_id" text NOT NULL,
|
||||
|
|
@ -14,7 +16,7 @@ CREATE TABLE "account" (
|
|||
"updated_at" timestamp NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "session" (
|
||||
CREATE TABLE "auth"."session" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"expires_at" timestamp NOT NULL,
|
||||
"token" text NOT NULL,
|
||||
|
|
@ -26,7 +28,7 @@ CREATE TABLE "session" (
|
|||
CONSTRAINT "session_token_unique" UNIQUE("token")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "user" (
|
||||
CREATE TABLE "auth"."user" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"name" text NOT NULL,
|
||||
"email" text NOT NULL,
|
||||
|
|
@ -37,7 +39,7 @@ CREATE TABLE "user" (
|
|||
CONSTRAINT "user_email_unique" UNIQUE("email")
|
||||
);
|
||||
--> statement-breakpoint
|
||||
CREATE TABLE "verification" (
|
||||
CREATE TABLE "auth"."verification" (
|
||||
"id" text PRIMARY KEY NOT NULL,
|
||||
"identifier" text NOT NULL,
|
||||
"value" text NOT NULL,
|
||||
|
|
@ -46,5 +48,5 @@ CREATE TABLE "verification" (
|
|||
"updated_at" timestamp
|
||||
);
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "account" ADD CONSTRAINT "account_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "session" ADD CONSTRAINT "session_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action;
|
||||
ALTER TABLE "auth"."account" ADD CONSTRAINT "account_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "auth"."user"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
|
||||
ALTER TABLE "auth"."session" ADD CONSTRAINT "session_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "auth"."user"("id") ON DELETE no action ON UPDATE no action;
|
||||
|
|
@ -1,12 +1,12 @@
|
|||
{
|
||||
"id": "f4717319-16e8-43a1-8b25-12a12964c98f",
|
||||
"id": "1ada386b-032f-4c0c-a867-e25f2e921167",
|
||||
"prevId": "00000000-0000-0000-0000-000000000000",
|
||||
"version": "7",
|
||||
"dialect": "postgresql",
|
||||
"tables": {
|
||||
"public.account": {
|
||||
"auth.account": {
|
||||
"name": "account",
|
||||
"schema": "",
|
||||
"schema": "auth",
|
||||
"columns": {
|
||||
"id": {
|
||||
"name": "id",
|
||||
|
|
@ -93,6 +93,7 @@
|
|||
"name": "account_user_id_user_id_fk",
|
||||
"tableFrom": "account",
|
||||
"tableTo": "user",
|
||||
"schemaTo": "auth",
|
||||
"columnsFrom": [
|
||||
"user_id"
|
||||
],
|
||||
|
|
@ -109,9 +110,9 @@
|
|||
"checkConstraints": {},
|
||||
"isRLSEnabled": false
|
||||
},
|
||||
"public.session": {
|
||||
"auth.session": {
|
||||
"name": "session",
|
||||
"schema": "",
|
||||
"schema": "auth",
|
||||
"columns": {
|
||||
"id": {
|
||||
"name": "id",
|
||||
|
|
@ -168,6 +169,7 @@
|
|||
"name": "session_user_id_user_id_fk",
|
||||
"tableFrom": "session",
|
||||
"tableTo": "user",
|
||||
"schemaTo": "auth",
|
||||
"columnsFrom": [
|
||||
"user_id"
|
||||
],
|
||||
|
|
@ -192,9 +194,9 @@
|
|||
"checkConstraints": {},
|
||||
"isRLSEnabled": false
|
||||
},
|
||||
"public.user": {
|
||||
"auth.user": {
|
||||
"name": "user",
|
||||
"schema": "",
|
||||
"schema": "auth",
|
||||
"columns": {
|
||||
"id": {
|
||||
"name": "id",
|
||||
|
|
@ -255,9 +257,9 @@
|
|||
"checkConstraints": {},
|
||||
"isRLSEnabled": false
|
||||
},
|
||||
"public.verification": {
|
||||
"auth.verification": {
|
||||
"name": "verification",
|
||||
"schema": "",
|
||||
"schema": "auth",
|
||||
"columns": {
|
||||
"id": {
|
||||
"name": "id",
|
||||
|
|
@ -306,7 +308,9 @@
|
|||
}
|
||||
},
|
||||
"enums": {},
|
||||
"schemas": {},
|
||||
"schemas": {
|
||||
"auth": "auth"
|
||||
},
|
||||
"sequences": {},
|
||||
"roles": {},
|
||||
"policies": {},
|
||||
|
|
|
|||
|
|
@ -5,8 +5,8 @@
|
|||
{
|
||||
"idx": 0,
|
||||
"version": "7",
|
||||
"when": 1736255248333,
|
||||
"tag": "0000_elite_ben_parker",
|
||||
"when": 1736605568502,
|
||||
"tag": "0000_rich_reavers",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
|
|
|
|||
|
|
@ -5,9 +5,13 @@
|
|||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"dev": "bun run --watch src/index.ts",
|
||||
"email": "email dev --dir src/emails",
|
||||
"db:studio": "drizzle-kit studio",
|
||||
"db:generate": "drizzle-kit generate",
|
||||
"db:migrate": "drizzle-kit migrate",
|
||||
"build": "bun build --compile --minify-whitespace --minify-syntax --target bun --outfile server ./src/index.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"@elysiajs/cors": "^1.2.0",
|
||||
"@elysiajs/opentelemetry": "^1.2.0",
|
||||
"@elysiajs/server-timing": "^1.2.0",
|
||||
"@elysiajs/swagger": "^1.2.0",
|
||||
|
|
|
|||
|
|
@ -1,23 +0,0 @@
|
|||
import { pgTable, varchar, timestamp } from "drizzle-orm/pg-core";
|
||||
|
||||
import { createId } from "@paralleldrive/cuid2";
|
||||
import { createInsertSchema } from "drizzle-typebox";
|
||||
import { t } from "elysia";
|
||||
|
||||
export const user = pgTable("user", {
|
||||
id: varchar("id")
|
||||
.$defaultFn(() => createId())
|
||||
.primaryKey(),
|
||||
username: varchar("username").notNull().unique(),
|
||||
password: varchar("password").notNull(),
|
||||
email: varchar("email").notNull().unique(),
|
||||
salt: varchar("salt", { length: 64 }).notNull(),
|
||||
createdAt: timestamp("created_at").defaultNow().notNull(),
|
||||
});
|
||||
|
||||
const _createUser = createInsertSchema(user, {
|
||||
email: t.String({ format: "email" }),
|
||||
});
|
||||
|
||||
// ✅ This works, by referencing the type from `drizzle-typebox`
|
||||
export const createUserType = t.Omit(_createUser, ["id", "salt", "createdAt"]);
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
import "dotenv/config";
|
||||
import { drizzle } from "drizzle-orm/node-postgres";
|
||||
|
||||
const db = drizzle(process.env.DATABASE_URL!);
|
||||
export const db = drizzle(process.env.DATABASE_URL!);
|
||||
|
|
|
|||
|
|
@ -1,7 +0,0 @@
|
|||
import { createUserType } from "../api/user/user.model";
|
||||
|
||||
export const db = {
|
||||
insert: {
|
||||
user: createUserType,
|
||||
},
|
||||
};
|
||||
|
|
@ -1,6 +1,8 @@
|
|||
import { pgTable, text, timestamp, boolean } from "drizzle-orm/pg-core";
|
||||
import { pgTable, pgSchema, text, timestamp, boolean } from "drizzle-orm/pg-core";
|
||||
|
||||
export const user = pgTable("user", {
|
||||
export const auth_schema = pgSchema("auth")
|
||||
|
||||
export const user = auth_schema.table("user", {
|
||||
id: text("id").primaryKey(),
|
||||
name: text("name").notNull(),
|
||||
email: text("email").notNull().unique(),
|
||||
|
|
@ -10,7 +12,7 @@ export const user = pgTable("user", {
|
|||
updatedAt: timestamp("updated_at").notNull(),
|
||||
});
|
||||
|
||||
export const session = pgTable("session", {
|
||||
export const session = auth_schema.table("session", {
|
||||
id: text("id").primaryKey(),
|
||||
expiresAt: timestamp("expires_at").notNull(),
|
||||
token: text("token").notNull().unique(),
|
||||
|
|
@ -23,7 +25,7 @@ export const session = pgTable("session", {
|
|||
.references(() => user.id),
|
||||
});
|
||||
|
||||
export const account = pgTable("account", {
|
||||
export const account = auth_schema.table("account", {
|
||||
id: text("id").primaryKey(),
|
||||
accountId: text("account_id").notNull(),
|
||||
providerId: text("provider_id").notNull(),
|
||||
|
|
@ -41,7 +43,7 @@ export const account = pgTable("account", {
|
|||
updatedAt: timestamp("updated_at").notNull(),
|
||||
});
|
||||
|
||||
export const verification = pgTable("verification", {
|
||||
export const verification = auth_schema.table("verification", {
|
||||
id: text("id").primaryKey(),
|
||||
identifier: text("identifier").notNull(),
|
||||
value: text("value").notNull(),
|
||||
|
|
|
|||
11
src/index.ts
11
src/index.ts
|
|
@ -2,22 +2,27 @@ import { Elysia } from "elysia";
|
|||
import { swagger } from "@elysiajs/swagger";
|
||||
import { opentelemetry } from "@elysiajs/opentelemetry";
|
||||
import { serverTiming } from "@elysiajs/server-timing";
|
||||
|
||||
import { cors } from '@elysiajs/cors'
|
||||
import { note } from "./api/note/note.route";
|
||||
import { betterAuthView } from "./lib/auth/auth-view";
|
||||
import { userMiddleware, userInfo } from "./middlewares/auth-middleware";
|
||||
|
||||
const app = new Elysia()
|
||||
.use(cors())
|
||||
.use(opentelemetry())
|
||||
.use(swagger())
|
||||
.use(swagger({
|
||||
path: "/docs",
|
||||
}))
|
||||
.use(serverTiming())
|
||||
.onError(({ error, code }) => {
|
||||
if (code === "NOT_FOUND") return "Not Found :(";
|
||||
console.error(error);
|
||||
})
|
||||
.use(note)
|
||||
.derive(({ request }) => userMiddleware(request))
|
||||
.all("/api/auth/*", betterAuthView)
|
||||
.use(note)
|
||||
.get("/user", ({ user, session }) => userInfo(user, session));
|
||||
|
||||
app.listen(3000);
|
||||
|
||||
console.log("Server is running on: http://localhost:3000")
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { Context, Elysia } from "elysia";
|
||||
import { Context } from "elysia";
|
||||
import { auth } from "./auth";
|
||||
|
||||
export const betterAuthView = (context: Context) => {
|
||||
|
|
|
|||
|
|
@ -1,14 +1,28 @@
|
|||
import { betterAuth } from "better-auth";
|
||||
import { drizzleAdapter } from "better-auth/adapters/drizzle";
|
||||
import { db } from "../../db/model";
|
||||
import { db } from "../../db/index";
|
||||
import { openAPI } from "better-auth/plugins"
|
||||
import { user, account, verification, session } from "../../db/schema";
|
||||
|
||||
export const auth = betterAuth({
|
||||
database: drizzleAdapter(db, {
|
||||
// We're using Drizzle as our database
|
||||
provider: "pg",
|
||||
schema: {
|
||||
user: user,
|
||||
session: session,
|
||||
account: account,
|
||||
verification: verification
|
||||
}
|
||||
}),
|
||||
emailAndPassword: {
|
||||
enabled: true, // If you want to use email and password auth
|
||||
},
|
||||
plugins: [
|
||||
openAPI({
|
||||
path: "/docs",
|
||||
}),
|
||||
],
|
||||
socialProviders: {
|
||||
/*
|
||||
* We're using Google and Github as our social provider,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue