Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: specific packages for db and auth #71

Merged
merged 5 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ Thanks for your interest in contributing to next-international!
brew tap libsql/sqld
brew install sqld
```
3. Navigate to `apps/dashboard` and run `pnpm db:db`, then `pnpm db:migrate` in a separate terminal
5. Navigate to `apps/dashboard` and run `pnpm dev`
5. Navigate to the root of the repository and run `pnpm dev`

Before submitting a PR, run the `test`, `lint`, and `typecheck` scripts from the root of the repository.

17 changes: 4 additions & 13 deletions apps/dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,32 @@
"start": "next start",
"lint": "next lint",
"typecheck": "tsc --noEmit",
"test": "vitest",
"db:generate": "drizzle-kit generate:sqlite",
"db:migrate": "tsx drizzle/migrate.ts",
"db:studio": "drizzle-kit studio",
"db:dev": "turso dev --db-file ./db.sqlite"
"test": "vitest"
},
"dependencies": {
"@dnd-kit/core": "^6.1.0",
"@dnd-kit/modifiers": "^7.0.0",
"@dnd-kit/sortable": "^8.0.0",
"@dnd-kit/utilities": "^3.2.2",
"@libsql/client": "^0.5.6",
"@lucia-auth/adapter-drizzle": "^1.0.4",
"@radix-ui/themes": "^3.0.0",
"@resvg/resvg-wasm": "2.4.0",
"@tanstack/react-query": "^5.17.10",
"@vercel/analytics": "^1.1.1",
"@vercel/kv": "^1.0.1",
"@vercel/speed-insights": "^1.0.3",
"arctic": "^1.2.1",
"clsx": "^2.1.0",
"drizzle-orm": "^0.30.2",
"lucia": "^3.1.1",
"next": "14.2.1",
"next-themes": "^0.3.0",
"oslo": "^1.1.3",
"react": "^18",
"react-colorful": "^5.6.1",
"react-dom": "^18",
"satori": "^0.10.11",
"sonner": "^1.3.1",
"zundo": "^2.0.3",
"zustand": "^4.4.7"
"zustand": "^4.4.7",
"@ogstudio/db": "workspace:*",
"@ogstudio/auth": "workspace:*"
},
"devDependencies": {
"@next/eslint-plugin-next": "^14.1.0",
Expand All @@ -50,8 +43,6 @@
"@types/react": "^18",
"@types/react-dom": "^18",
"autoprefixer": "^10.4.16",
"dotenv": "^16.3.1",
"drizzle-kit": "^0.20.14",
"happy-dom": "^13.0.4",
"postcss": "^8.4.33",
"postcss-import": "^16.1.0",
Expand Down
4 changes: 2 additions & 2 deletions apps/dashboard/src/app/(splash)/profile/logoutAction.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"use server";
import { cookies } from "next/headers";
import { redirect } from "next/navigation";
import { getCachedSession } from "../../../lib/auth/api";
import { lucia } from "../../../lib/auth/lucia";
import { getCachedSession } from "@ogstudio/auth/api";
import { lucia } from "@ogstudio/auth/lucia";

export async function logoutAction() {
const { session } = await getCachedSession();
Expand Down
14 changes: 6 additions & 8 deletions apps/dashboard/src/app/api/auth/github/callback/route.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { cookies } from "next/headers";
import { OAuth2RequestError } from "arctic";
import { generateId } from "lucia";
import { eq } from "drizzle-orm";
import { github } from "../../../../../lib/auth/arctic";
import { db } from "../../../../../lib/db/db";
import { lucia } from "../../../../../lib/auth/lucia";
import { userTable } from "../../../../../lib/db/schema";
import { db, eq } from "@ogstudio/db/db";
import { userTable } from "@ogstudio/db/schema";
import { github, OAuth2RequestError } from "@ogstudio/auth/arctic";
import { lucia, generateId } from "@ogstudio/auth/lucia";

// https://docs.github.com/en/rest/users/users?apiVersion=2022-11-28
interface GitHubUser {
id: number;
avatar_url: string;
name: string;
login: string;
}

export async function GET(request: Request): Promise<Response> {
Expand Down Expand Up @@ -62,7 +60,7 @@ export async function GET(request: Request): Promise<Response> {
await db.insert(userTable).values({
id: userId,
githubId: githubUser.id,
name: githubUser.name,
name: githubUser.name || githubUser.login,
avatar: githubUser.avatar_url,
});

Expand Down
3 changes: 1 addition & 2 deletions apps/dashboard/src/app/api/auth/github/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { generateState } from "arctic";
import { cookies } from "next/headers";
import { github } from "../../../../lib/auth/arctic";
import { github, generateState } from "@ogstudio/auth/arctic";

export async function GET() {
const state = generateState();
Expand Down
11 changes: 4 additions & 7 deletions apps/dashboard/src/app/api/auth/google/callback/route.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { cookies } from "next/headers";
import { OAuth2RequestError } from "arctic";
import { generateId } from "lucia";
import { eq } from "drizzle-orm";
import { google } from "../../../../../lib/auth/arctic";
import { db } from "../../../../../lib/db/db";
import { lucia } from "../../../../../lib/auth/lucia";
import { userTable } from "../../../../../lib/db/schema";
import { db, eq } from "@ogstudio/db/db";
import { userTable } from "@ogstudio/db/schema";
import { google, OAuth2RequestError } from "@ogstudio/auth/arctic";
import { lucia, generateId } from "@ogstudio/auth/lucia";

// https://developers.google.com/identity/openid-connect/openid-connect#an-id-tokens-payload
interface GoogleUser {
Expand Down
7 changes: 5 additions & 2 deletions apps/dashboard/src/app/api/auth/google/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { generateCodeVerifier, generateState } from "arctic";
import { cookies } from "next/headers";
import { google } from "../../../../lib/auth/arctic";
import {
google,
generateCodeVerifier,
generateState,
} from "@ogstudio/auth/arctic";

export async function GET() {
const state = generateState();
Expand Down
4 changes: 2 additions & 2 deletions apps/dashboard/src/app/api/me/route.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { SessionObject } from "../../../lib/auth/api";
import { getSession } from "../../../lib/auth/api";
import type { SessionObject } from "@ogstudio/auth/api";
import { getSession } from "@ogstudio/auth/api";

export type MeResponse = SessionObject;

Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/src/app/api/og/export/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { kv } from "@vercel/kv";
import { revalidatePath } from "next/cache";
import { getSession } from "../../../../lib/auth/api";
import { getSession } from "@ogstudio/auth/api";
import type { OGElement } from "../../../../lib/types";

export interface ExportRequest {
Expand Down
5 changes: 5 additions & 0 deletions apps/dashboard/turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
".next/**",
"!.next/cache/**"
]
},
"dev": {
"dependsOn": [
"@ogstudio/db#migrate"
]
}
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ export const google = new Google(
process.env.GOOGLE_CLIENT_SECRET ?? "",
process.env.GOOGLE_REDIRECT_URI ?? "",
);

export { generateState, generateCodeVerifier, OAuth2RequestError } from 'arctic'
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Lucia } from "lucia";
import { DrizzleSQLiteAdapter } from "@lucia-auth/adapter-drizzle";
import { sessionTable, userTable } from "../db/schema";
import { db } from "../db/db";
import { sessionTable, userTable } from "@ogstudio/db/schema";
import { db } from "@ogstudio/db/db";

const adapter = new DrizzleSQLiteAdapter(db, sessionTable, userTable);

Expand All @@ -20,6 +20,8 @@ export const lucia = new Lucia(adapter, {
},
});

export { generateId } from 'lucia'

declare module "lucia" {
interface Register {
Lucia: typeof lucia;
Expand Down
24 changes: 24 additions & 0 deletions packages/auth/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@ogstudio/auth",
"version": "0.1.0",
"private": true,
"scripts": {
"lint": "eslint --cache",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@lucia-auth/adapter-drizzle": "^1.0.4",
"arctic": "^1.2.1",
"lucia": "^3.1.1",
"@ogstudio/db": "workspace:*"
},
"peerDependencies": {
"next": "14.2.1",
"react": "^18"
},
"devDependencies": {
"@types/react": "^18",
"@ogstudio/eslint": "workspace:*",
"@ogstudio/typescript": "workspace:*"
}
}
10 changes: 10 additions & 0 deletions packages/auth/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "@ogstudio/typescript/library.json",
"include": [
"**/*.ts"
],
"exclude": [
"node_modules"
]
}
8 changes: 8 additions & 0 deletions apps/dashboard/src/lib/db/db.ts → packages/db/db.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { drizzle } from "drizzle-orm/libsql";
import { createClient } from "@libsql/client";
import { config } from "dotenv";

if (!process.env.DATABASE_URL) {
config({
path: "../../apps/dashboard/.env",
});
}

export const client = createClient({
url: process.env.DATABASE_URL ?? "",
authToken: process.env.DATABASE_AUTH_TOKEN ?? "",
});

export const db = drizzle(client);
export { eq } from 'drizzle-orm'
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import "dotenv/config";
import { config } from "dotenv";
import type { Config } from "drizzle-kit";

if (!process.env.DATABASE_URL) {
config({
path: "../../apps/dashboard/.env",
});
}

export default {
schema: "./src/lib/db/schema.ts",
schema: "./schema.ts",
out: "./drizzle",
driver: "turso",
dbCredentials: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import "dotenv/config";
import { config } from "dotenv";

import { migrate } from "drizzle-orm/libsql/migrator";
import { client, db } from "../src/lib/db/db";
import { client, db } from "../db";

if (!process.env.DATABASE_URL) {
config({
path: "../../apps/dashboard/.env",
});
}

async function run() {
// This will run migrations on the database, skipping the ones already applied
Expand Down
24 changes: 24 additions & 0 deletions packages/db/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@ogstudio/db",
"version": "0.1.0",
"private": true,
"scripts": {
"lint": "eslint --cache",
"typecheck": "tsc --noEmit",
"generate": "drizzle-kit generate:sqlite",
"migrate": "tsx drizzle/migrate.ts",
"studio": "drizzle-kit studio",
"dev": "turso dev --db-file ./db.sqlite"
},
"dependencies": {
"@libsql/client": "^0.5.6",
"drizzle-orm": "^0.30.2"
},
"devDependencies": {
"@ogstudio/eslint": "workspace:*",
"@ogstudio/typescript": "workspace:*",
"dotenv": "^16.3.1",
"drizzle-kit": "^0.20.14",
"tsx": "^4.7.0"
}
}
File renamed without changes.
10 changes: 10 additions & 0 deletions packages/db/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "@ogstudio/typescript/library.json",
"include": [
"**/*.ts"
],
"exclude": [
"node_modules"
]
}
13 changes: 13 additions & 0 deletions packages/db/turbo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"$schema": "https://turbo.build/schema.json",
"extends": [
"//"
],
"pipeline": {
"migrate": {
"inputs": [
"./schema.ts"
]
}
}
}
Loading
Loading