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
|
dockerfile: Dockerfile
|
||||||
ports:
|
ports:
|
||||||
- "3000:3000"
|
- "3000:3000"
|
||||||
|
env_file:
|
||||||
|
- .env
|
||||||
environment:
|
environment:
|
||||||
- NODE_ENV=production
|
- NODE_ENV=production
|
||||||
- OTEL_EXPORTER_OTLP_ENDPOINT=http://tracing:4318
|
- OTEL_EXPORTER_OTLP_ENDPOINT=http://tracing:4318
|
||||||
|
|
@ -27,10 +29,14 @@ services:
|
||||||
|
|
||||||
db:
|
db:
|
||||||
image: postgres:latest
|
image: postgres:latest
|
||||||
|
env_file:
|
||||||
|
- .env
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_USER: ${DB_USER}
|
POSTGRES_USER: ${DB_USER}
|
||||||
POSTGRES_PASSWORD: ${DB_PASSWORD}
|
POSTGRES_PASSWORD: ${DB_PASSWORD}
|
||||||
POSTGRES_DB: ${DB_NAME}
|
POSTGRES_DB: ${DB_NAME}
|
||||||
|
ports:
|
||||||
|
- "5432:5432"
|
||||||
volumes:
|
volumes:
|
||||||
- postgres_data:/var/lib/postgresql/data
|
- postgres_data:/var/lib/postgresql/data
|
||||||
networks:
|
networks:
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
CREATE TABLE "account" (
|
CREATE SCHEMA "auth";
|
||||||
|
--> statement-breakpoint
|
||||||
|
CREATE TABLE "auth"."account" (
|
||||||
"id" text PRIMARY KEY NOT NULL,
|
"id" text PRIMARY KEY NOT NULL,
|
||||||
"account_id" text NOT NULL,
|
"account_id" text NOT NULL,
|
||||||
"provider_id" text NOT NULL,
|
"provider_id" text NOT NULL,
|
||||||
|
|
@ -14,7 +16,7 @@ CREATE TABLE "account" (
|
||||||
"updated_at" timestamp NOT NULL
|
"updated_at" timestamp NOT NULL
|
||||||
);
|
);
|
||||||
--> statement-breakpoint
|
--> statement-breakpoint
|
||||||
CREATE TABLE "session" (
|
CREATE TABLE "auth"."session" (
|
||||||
"id" text PRIMARY KEY NOT NULL,
|
"id" text PRIMARY KEY NOT NULL,
|
||||||
"expires_at" timestamp NOT NULL,
|
"expires_at" timestamp NOT NULL,
|
||||||
"token" text NOT NULL,
|
"token" text NOT NULL,
|
||||||
|
|
@ -26,7 +28,7 @@ CREATE TABLE "session" (
|
||||||
CONSTRAINT "session_token_unique" UNIQUE("token")
|
CONSTRAINT "session_token_unique" UNIQUE("token")
|
||||||
);
|
);
|
||||||
--> statement-breakpoint
|
--> statement-breakpoint
|
||||||
CREATE TABLE "user" (
|
CREATE TABLE "auth"."user" (
|
||||||
"id" text PRIMARY KEY NOT NULL,
|
"id" text PRIMARY KEY NOT NULL,
|
||||||
"name" text NOT NULL,
|
"name" text NOT NULL,
|
||||||
"email" text NOT NULL,
|
"email" text NOT NULL,
|
||||||
|
|
@ -37,7 +39,7 @@ CREATE TABLE "user" (
|
||||||
CONSTRAINT "user_email_unique" UNIQUE("email")
|
CONSTRAINT "user_email_unique" UNIQUE("email")
|
||||||
);
|
);
|
||||||
--> statement-breakpoint
|
--> statement-breakpoint
|
||||||
CREATE TABLE "verification" (
|
CREATE TABLE "auth"."verification" (
|
||||||
"id" text PRIMARY KEY NOT NULL,
|
"id" text PRIMARY KEY NOT NULL,
|
||||||
"identifier" text NOT NULL,
|
"identifier" text NOT NULL,
|
||||||
"value" text NOT NULL,
|
"value" text NOT NULL,
|
||||||
|
|
@ -46,5 +48,5 @@ CREATE TABLE "verification" (
|
||||||
"updated_at" timestamp
|
"updated_at" timestamp
|
||||||
);
|
);
|
||||||
--> statement-breakpoint
|
--> 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 "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 "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"."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",
|
"prevId": "00000000-0000-0000-0000-000000000000",
|
||||||
"version": "7",
|
"version": "7",
|
||||||
"dialect": "postgresql",
|
"dialect": "postgresql",
|
||||||
"tables": {
|
"tables": {
|
||||||
"public.account": {
|
"auth.account": {
|
||||||
"name": "account",
|
"name": "account",
|
||||||
"schema": "",
|
"schema": "auth",
|
||||||
"columns": {
|
"columns": {
|
||||||
"id": {
|
"id": {
|
||||||
"name": "id",
|
"name": "id",
|
||||||
|
|
@ -93,6 +93,7 @@
|
||||||
"name": "account_user_id_user_id_fk",
|
"name": "account_user_id_user_id_fk",
|
||||||
"tableFrom": "account",
|
"tableFrom": "account",
|
||||||
"tableTo": "user",
|
"tableTo": "user",
|
||||||
|
"schemaTo": "auth",
|
||||||
"columnsFrom": [
|
"columnsFrom": [
|
||||||
"user_id"
|
"user_id"
|
||||||
],
|
],
|
||||||
|
|
@ -109,9 +110,9 @@
|
||||||
"checkConstraints": {},
|
"checkConstraints": {},
|
||||||
"isRLSEnabled": false
|
"isRLSEnabled": false
|
||||||
},
|
},
|
||||||
"public.session": {
|
"auth.session": {
|
||||||
"name": "session",
|
"name": "session",
|
||||||
"schema": "",
|
"schema": "auth",
|
||||||
"columns": {
|
"columns": {
|
||||||
"id": {
|
"id": {
|
||||||
"name": "id",
|
"name": "id",
|
||||||
|
|
@ -168,6 +169,7 @@
|
||||||
"name": "session_user_id_user_id_fk",
|
"name": "session_user_id_user_id_fk",
|
||||||
"tableFrom": "session",
|
"tableFrom": "session",
|
||||||
"tableTo": "user",
|
"tableTo": "user",
|
||||||
|
"schemaTo": "auth",
|
||||||
"columnsFrom": [
|
"columnsFrom": [
|
||||||
"user_id"
|
"user_id"
|
||||||
],
|
],
|
||||||
|
|
@ -192,9 +194,9 @@
|
||||||
"checkConstraints": {},
|
"checkConstraints": {},
|
||||||
"isRLSEnabled": false
|
"isRLSEnabled": false
|
||||||
},
|
},
|
||||||
"public.user": {
|
"auth.user": {
|
||||||
"name": "user",
|
"name": "user",
|
||||||
"schema": "",
|
"schema": "auth",
|
||||||
"columns": {
|
"columns": {
|
||||||
"id": {
|
"id": {
|
||||||
"name": "id",
|
"name": "id",
|
||||||
|
|
@ -255,9 +257,9 @@
|
||||||
"checkConstraints": {},
|
"checkConstraints": {},
|
||||||
"isRLSEnabled": false
|
"isRLSEnabled": false
|
||||||
},
|
},
|
||||||
"public.verification": {
|
"auth.verification": {
|
||||||
"name": "verification",
|
"name": "verification",
|
||||||
"schema": "",
|
"schema": "auth",
|
||||||
"columns": {
|
"columns": {
|
||||||
"id": {
|
"id": {
|
||||||
"name": "id",
|
"name": "id",
|
||||||
|
|
@ -306,7 +308,9 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"enums": {},
|
"enums": {},
|
||||||
"schemas": {},
|
"schemas": {
|
||||||
|
"auth": "auth"
|
||||||
|
},
|
||||||
"sequences": {},
|
"sequences": {},
|
||||||
"roles": {},
|
"roles": {},
|
||||||
"policies": {},
|
"policies": {},
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,8 @@
|
||||||
{
|
{
|
||||||
"idx": 0,
|
"idx": 0,
|
||||||
"version": "7",
|
"version": "7",
|
||||||
"when": 1736255248333,
|
"when": 1736605568502,
|
||||||
"tag": "0000_elite_ben_parker",
|
"tag": "0000_rich_reavers",
|
||||||
"breakpoints": true
|
"breakpoints": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,13 @@
|
||||||
"test": "echo \"Error: no test specified\" && exit 1",
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
"dev": "bun run --watch src/index.ts",
|
"dev": "bun run --watch src/index.ts",
|
||||||
"email": "email dev --dir src/emails",
|
"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"
|
"build": "bun build --compile --minify-whitespace --minify-syntax --target bun --outfile server ./src/index.ts"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@elysiajs/cors": "^1.2.0",
|
||||||
"@elysiajs/opentelemetry": "^1.2.0",
|
"@elysiajs/opentelemetry": "^1.2.0",
|
||||||
"@elysiajs/server-timing": "^1.2.0",
|
"@elysiajs/server-timing": "^1.2.0",
|
||||||
"@elysiajs/swagger": "^1.2.0",
|
"@elysiajs/swagger": "^1.2.0",
|
||||||
|
|
@ -37,4 +41,4 @@
|
||||||
"tsx": "^4.19.2"
|
"tsx": "^4.19.2"
|
||||||
},
|
},
|
||||||
"module": "src/index.js"
|
"module": "src/index.js"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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 "dotenv/config";
|
||||||
import { drizzle } from "drizzle-orm/node-postgres";
|
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(),
|
id: text("id").primaryKey(),
|
||||||
name: text("name").notNull(),
|
name: text("name").notNull(),
|
||||||
email: text("email").notNull().unique(),
|
email: text("email").notNull().unique(),
|
||||||
|
|
@ -10,7 +12,7 @@ export const user = pgTable("user", {
|
||||||
updatedAt: timestamp("updated_at").notNull(),
|
updatedAt: timestamp("updated_at").notNull(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const session = pgTable("session", {
|
export const session = auth_schema.table("session", {
|
||||||
id: text("id").primaryKey(),
|
id: text("id").primaryKey(),
|
||||||
expiresAt: timestamp("expires_at").notNull(),
|
expiresAt: timestamp("expires_at").notNull(),
|
||||||
token: text("token").notNull().unique(),
|
token: text("token").notNull().unique(),
|
||||||
|
|
@ -23,7 +25,7 @@ export const session = pgTable("session", {
|
||||||
.references(() => user.id),
|
.references(() => user.id),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const account = pgTable("account", {
|
export const account = auth_schema.table("account", {
|
||||||
id: text("id").primaryKey(),
|
id: text("id").primaryKey(),
|
||||||
accountId: text("account_id").notNull(),
|
accountId: text("account_id").notNull(),
|
||||||
providerId: text("provider_id").notNull(),
|
providerId: text("provider_id").notNull(),
|
||||||
|
|
@ -41,7 +43,7 @@ export const account = pgTable("account", {
|
||||||
updatedAt: timestamp("updated_at").notNull(),
|
updatedAt: timestamp("updated_at").notNull(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const verification = pgTable("verification", {
|
export const verification = auth_schema.table("verification", {
|
||||||
id: text("id").primaryKey(),
|
id: text("id").primaryKey(),
|
||||||
identifier: text("identifier").notNull(),
|
identifier: text("identifier").notNull(),
|
||||||
value: text("value").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 { swagger } from "@elysiajs/swagger";
|
||||||
import { opentelemetry } from "@elysiajs/opentelemetry";
|
import { opentelemetry } from "@elysiajs/opentelemetry";
|
||||||
import { serverTiming } from "@elysiajs/server-timing";
|
import { serverTiming } from "@elysiajs/server-timing";
|
||||||
|
import { cors } from '@elysiajs/cors'
|
||||||
import { note } from "./api/note/note.route";
|
import { note } from "./api/note/note.route";
|
||||||
import { betterAuthView } from "./lib/auth/auth-view";
|
import { betterAuthView } from "./lib/auth/auth-view";
|
||||||
import { userMiddleware, userInfo } from "./middlewares/auth-middleware";
|
import { userMiddleware, userInfo } from "./middlewares/auth-middleware";
|
||||||
|
|
||||||
const app = new Elysia()
|
const app = new Elysia()
|
||||||
|
.use(cors())
|
||||||
.use(opentelemetry())
|
.use(opentelemetry())
|
||||||
.use(swagger())
|
.use(swagger({
|
||||||
|
path: "/docs",
|
||||||
|
}))
|
||||||
.use(serverTiming())
|
.use(serverTiming())
|
||||||
.onError(({ error, code }) => {
|
.onError(({ error, code }) => {
|
||||||
if (code === "NOT_FOUND") return "Not Found :(";
|
if (code === "NOT_FOUND") return "Not Found :(";
|
||||||
console.error(error);
|
console.error(error);
|
||||||
})
|
})
|
||||||
.use(note)
|
|
||||||
.derive(({ request }) => userMiddleware(request))
|
.derive(({ request }) => userMiddleware(request))
|
||||||
.all("/api/auth/*", betterAuthView)
|
.all("/api/auth/*", betterAuthView)
|
||||||
|
.use(note)
|
||||||
.get("/user", ({ user, session }) => userInfo(user, session));
|
.get("/user", ({ user, session }) => userInfo(user, session));
|
||||||
|
|
||||||
app.listen(3000);
|
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";
|
import { auth } from "./auth";
|
||||||
|
|
||||||
export const betterAuthView = (context: Context) => {
|
export const betterAuthView = (context: Context) => {
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,28 @@
|
||||||
import { betterAuth } from "better-auth";
|
import { betterAuth } from "better-auth";
|
||||||
import { drizzleAdapter } from "better-auth/adapters/drizzle";
|
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({
|
export const auth = betterAuth({
|
||||||
database: drizzleAdapter(db, {
|
database: drizzleAdapter(db, {
|
||||||
// We're using Drizzle as our database
|
// We're using Drizzle as our database
|
||||||
provider: "pg",
|
provider: "pg",
|
||||||
|
schema: {
|
||||||
|
user: user,
|
||||||
|
session: session,
|
||||||
|
account: account,
|
||||||
|
verification: verification
|
||||||
|
}
|
||||||
}),
|
}),
|
||||||
emailAndPassword: {
|
emailAndPassword: {
|
||||||
enabled: true, // If you want to use email and password auth
|
enabled: true, // If you want to use email and password auth
|
||||||
},
|
},
|
||||||
|
plugins: [
|
||||||
|
openAPI({
|
||||||
|
path: "/docs",
|
||||||
|
}),
|
||||||
|
],
|
||||||
socialProviders: {
|
socialProviders: {
|
||||||
/*
|
/*
|
||||||
* We're using Google and Github as our social provider,
|
* We're using Google and Github as our social provider,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue