Skip to content

Commit

Permalink
Auth (#51)
Browse files Browse the repository at this point in the history
* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Update .dev.vars-example

* wip

* emails

* wip

* wip

* wip

* wip

* wip

* wip

* wip
  • Loading branch information
pontusab authored Jan 7, 2025
1 parent 99106fe commit caef478
Show file tree
Hide file tree
Showing 132 changed files with 7,981 additions and 1,722 deletions.
7 changes: 6 additions & 1 deletion apps/api/.dev.vars-example
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
RESEND_API_KEY=
BETTER_AUTH_SECRET="<your-secret-here>"
BETTER_AUTH_TRUSTED_ORIGINS="<server-origin-1>,<server-origin-2>"
BETTER_AUTH_TRUSTED_ORIGINS="<server-origin-1>,<server-origin-2>"
BETTER_AUTH_BASE_URL=http://localhost:3000
GITHUB_CLIENT_ID=
GITHUB_CLIENT_SECRET=
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
50 changes: 39 additions & 11 deletions apps/api/drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
import { defineConfig } from "drizzle-kit";
import fs from "node:fs";
import path from "node:path";
import type { Config } from "drizzle-kit";

export default defineConfig({
const getLocalD1 = (): string => {
const wranglerDir = path.resolve(".wrangler");

try {
const files = fs.readdirSync(wranglerDir, {
encoding: "utf-8",
recursive: true,
});

const dbFile = files.find((f) => f.endsWith(".sqlite"));

if (!dbFile) {
throw new Error(`No SQLite database found in ${wranglerDir}`);
}

return path.resolve(wranglerDir, dbFile);
} catch {
return path.resolve(wranglerDir, "default.sqlite");
}
};

export default {
dialect: "sqlite",
driver: "d1-http",
out: "drizzle",
schema: "./src/db/schema.ts",
// Only needed for drizzle studio
// dbCredentials: {
// accountId: Bun.env.DB_ACCOUNT_ID!,
// databaseId: Bun.env.DB_DATABASE_ID!,
// token: Bun.env.DB_TOKEN!,
// },
});
out: "./drizzle",
...(!process.env.DEV_MODE
? {
driver: "d1-http",
}
: {}),
...(process.env.DEV_MODE
? {
dbCredentials: {
url: getLocalD1(),
},
}
: {}),
} satisfies Config;
127 changes: 127 additions & 0 deletions apps/api/drizzle/0000_busy_mandrill.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
CREATE TABLE `accounts` (
`id` text PRIMARY KEY NOT NULL,
`account_id` text NOT NULL,
`provider_id` text NOT NULL,
`user_id` text NOT NULL,
`access_token` text,
`refresh_token` text,
`id_token` text,
`access_token_expires_at` integer,
`refresh_token_expires_at` integer,
`scope` text,
`password` text,
`created_at` integer NOT NULL,
`updated_at` integer NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `accounts_user_id_idx` ON `accounts` (`user_id`);--> statement-breakpoint
CREATE INDEX `provider_compound_idx` ON `accounts` (`provider_id`,`account_id`);--> statement-breakpoint
CREATE TABLE `invitations` (
`id` text PRIMARY KEY NOT NULL,
`organization_id` text NOT NULL,
`email` text NOT NULL,
`role` text,
`status` text NOT NULL,
`expires_at` integer NOT NULL,
`inviter_id` text NOT NULL,
FOREIGN KEY (`organization_id`) REFERENCES `organizations`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`inviter_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `org_email_idx` ON `invitations` (`organization_id`,`email`);--> statement-breakpoint
CREATE INDEX `invitations_expires_at_idx` ON `invitations` (`expires_at`);--> statement-breakpoint
CREATE TABLE `members` (
`id` text PRIMARY KEY NOT NULL,
`organization_id` text NOT NULL,
`user_id` text NOT NULL,
`role` text NOT NULL,
`created_at` integer NOT NULL,
FOREIGN KEY (`organization_id`) REFERENCES `organizations`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `org_user_idx` ON `members` (`organization_id`,`user_id`);--> statement-breakpoint
CREATE TABLE `organizations` (
`id` text PRIMARY KEY NOT NULL,
`name` text NOT NULL,
`slug` text,
`logo` text,
`plan` text DEFAULT 'free' NOT NULL,
`api_key` text NOT NULL,
`created_at` integer NOT NULL,
`metadata` text
);
--> statement-breakpoint
CREATE UNIQUE INDEX `organizations_slug_unique` ON `organizations` (`slug`);--> statement-breakpoint
CREATE UNIQUE INDEX `organizations_api_key_unique` ON `organizations` (`api_key`);--> statement-breakpoint
CREATE INDEX `slug_idx` ON `organizations` (`slug`);--> statement-breakpoint
CREATE INDEX `org_api_key_idx` ON `organizations` (`api_key`);--> statement-breakpoint
CREATE TABLE `project_settings` (
`id` text PRIMARY KEY NOT NULL,
`project_id` text NOT NULL,
`cache` integer DEFAULT true NOT NULL,
`context` integer DEFAULT true NOT NULL,
`temperature` real DEFAULT 0 NOT NULL,
`instructions` text,
`memory` integer DEFAULT true NOT NULL,
`grammar` integer DEFAULT true NOT NULL,
`created_at` integer NOT NULL,
`updated_at` integer NOT NULL,
FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `project_idx` ON `project_settings` (`project_id`);--> statement-breakpoint
CREATE TABLE `projects` (
`id` text PRIMARY KEY NOT NULL,
`name` text NOT NULL,
`description` text,
`organization_id` text NOT NULL,
`created_at` integer NOT NULL,
`updated_at` integer,
FOREIGN KEY (`organization_id`) REFERENCES `organizations`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `org_idx` ON `projects` (`organization_id`);--> statement-breakpoint
CREATE TABLE `sessions` (
`id` text PRIMARY KEY NOT NULL,
`expires_at` integer NOT NULL,
`token` text NOT NULL,
`created_at` integer NOT NULL,
`updated_at` integer NOT NULL,
`ip_address` text,
`user_agent` text,
`user_id` text NOT NULL,
`active_organization_id` text,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE UNIQUE INDEX `sessions_token_unique` ON `sessions` (`token`);--> statement-breakpoint
CREATE INDEX `user_id_idx` ON `sessions` (`user_id`);--> statement-breakpoint
CREATE INDEX `token_idx` ON `sessions` (`token`);--> statement-breakpoint
CREATE INDEX `expires_at_idx` ON `sessions` (`expires_at`);--> statement-breakpoint
CREATE TABLE `users` (
`id` text PRIMARY KEY NOT NULL,
`name` text NOT NULL,
`email` text NOT NULL,
`email_verified` integer NOT NULL,
`image` text,
`api_key` text NOT NULL,
`created_at` integer NOT NULL,
`updated_at` integer NOT NULL
);
--> statement-breakpoint
CREATE UNIQUE INDEX `users_email_unique` ON `users` (`email`);--> statement-breakpoint
CREATE UNIQUE INDEX `users_api_key_unique` ON `users` (`api_key`);--> statement-breakpoint
CREATE INDEX `email_idx` ON `users` (`email`);--> statement-breakpoint
CREATE TABLE `verifications` (
`id` text PRIMARY KEY NOT NULL,
`identifier` text NOT NULL,
`value` text NOT NULL,
`expires_at` integer NOT NULL,
`created_at` integer,
`updated_at` integer
);
--> statement-breakpoint
CREATE INDEX `identifier_idx` ON `verifications` (`identifier`);--> statement-breakpoint
CREATE INDEX `verifications_expires_at_idx` ON `verifications` (`expires_at`);
82 changes: 0 additions & 82 deletions apps/api/drizzle/0000_swift_jocasta.sql

This file was deleted.

2 changes: 2 additions & 0 deletions apps/api/drizzle/0001_steep_scrambler.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE `projects` ADD `slug` text NOT NULL;--> statement-breakpoint
CREATE UNIQUE INDEX `slug_org_idx` ON `projects` (`slug`,`organization_id`);
Loading

0 comments on commit caef478

Please sign in to comment.