aboutsummaryrefslogtreecommitdiff
path: root/apps/web/src/server
diff options
context:
space:
mode:
Diffstat (limited to 'apps/web/src/server')
-rw-r--r--apps/web/src/server/auth.ts74
-rw-r--r--apps/web/src/server/db/index.ts12
-rw-r--r--apps/web/src/server/db/schema.ts111
3 files changed, 197 insertions, 0 deletions
diff --git a/apps/web/src/server/auth.ts b/apps/web/src/server/auth.ts
new file mode 100644
index 00000000..cb0a26d9
--- /dev/null
+++ b/apps/web/src/server/auth.ts
@@ -0,0 +1,74 @@
+import { DrizzleAdapter } from "@auth/drizzle-adapter";
+import {
+ getServerSession,
+ type DefaultSession,
+ type NextAuthOptions,
+} from "next-auth";
+import { type Adapter } from "next-auth/adapters";
+import GoogleProvider from "next-auth/providers/google";
+
+import { env } from "@/env";
+import { db } from "@/server/db";
+import { createTable } from "@/server/db/schema";
+
+/**
+ * Module augmentation for `next-auth` types. Allows us to add custom properties to the `session`
+ * object and keep type safety.
+ *
+ * @see https://next-auth.js.org/getting-started/typescript#module-augmentation
+ */
+declare module "next-auth" {
+ interface Session extends DefaultSession {
+ user: {
+ id: string;
+ // ...other properties
+ // role: UserRole;
+ } & DefaultSession["user"];
+ }
+
+ // interface User {
+ // // ...other properties
+ // // role: UserRole;
+ // }
+}
+
+/**
+ * Options for NextAuth.js used to configure adapters, providers, callbacks, etc.
+ *
+ * @see https://next-auth.js.org/configuration/options
+ */
+export const authOptions: NextAuthOptions = {
+ callbacks: {
+ session: ({ session, token }) => ({
+ ...session,
+ user: {
+ ...session.user,
+ id: token.id,
+ token
+ },
+ })
+ },
+ adapter: DrizzleAdapter(db, createTable) as Adapter,
+ providers: [
+ GoogleProvider({
+ clientId: env.GOOGLE_CLIENT_ID,
+ clientSecret: env.GOOGLE_CLIENT_SECRET,
+ }),
+ /**
+ * ...add more providers here.
+ *
+ * Most other providers require a bit more work than the Discord provider. For example, the
+ * GitHub provider requires you to add the `refresh_token_expires_in` field to the Account
+ * model. Refer to the NextAuth.js docs for the provider you want to use. Example:
+ *
+ * @see https://next-auth.js.org/providers/github
+ */
+ ],
+};
+
+/**
+ * Wrapper for `getServerSession` so that you don't need to import the `authOptions` in every file.
+ *
+ * @see https://next-auth.js.org/configuration/nextjs
+ */
+export const getServerAuthSession = () => getServerSession(authOptions);
diff --git a/apps/web/src/server/db/index.ts b/apps/web/src/server/db/index.ts
new file mode 100644
index 00000000..5c2246d5
--- /dev/null
+++ b/apps/web/src/server/db/index.ts
@@ -0,0 +1,12 @@
+import Database from "better-sqlite3";
+import { drizzle } from "drizzle-orm/better-sqlite3";
+
+import { env } from "@/env.js";
+import * as schema from "./schema";
+
+export const db = drizzle(
+ new Database(env.DATABASE_URL, {
+ fileMustExist: false,
+ }),
+ { schema }
+);
diff --git a/apps/web/src/server/db/schema.ts b/apps/web/src/server/db/schema.ts
new file mode 100644
index 00000000..7de02f15
--- /dev/null
+++ b/apps/web/src/server/db/schema.ts
@@ -0,0 +1,111 @@
+import { relations, sql } from "drizzle-orm";
+import {
+ index,
+ int,
+ primaryKey,
+ sqliteTableCreator,
+ text,
+} from "drizzle-orm/sqlite-core";
+import { type AdapterAccount } from "next-auth/adapters";
+
+/**
+ * This is an example of how to use the multi-project schema feature of Drizzle ORM. Use the same
+ * database instance for multiple projects.
+ *
+ * @see https://orm.drizzle.team/docs/goodies#multi-project-schema
+ */
+export const createTable = sqliteTableCreator((name) => `anycontext_${name}`);
+
+export const posts = createTable(
+ "post",
+ {
+ id: int("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
+ name: text("name", { length: 256 }),
+ createdById: text("createdById", { length: 255 })
+ .notNull()
+ .references(() => users.id),
+ createdAt: int("created_at", { mode: "timestamp" })
+ .default(sql`CURRENT_TIMESTAMP`)
+ .notNull(),
+ updatedAt: int("updatedAt", { mode: "timestamp" }),
+ },
+ (example) => ({
+ createdByIdIdx: index("createdById_idx").on(example.createdById),
+ nameIndex: index("name_idx").on(example.name),
+ })
+);
+
+export const users = createTable("user", {
+ id: text("id", { length: 255 }).notNull().primaryKey(),
+ name: text("name", { length: 255 }),
+ email: text("email", { length: 255 }).notNull(),
+ emailVerified: int("emailVerified", {
+ mode: "timestamp",
+ }).default(sql`CURRENT_TIMESTAMP`),
+ image: text("image", { length: 255 }),
+});
+
+export const usersRelations = relations(users, ({ many }) => ({
+ accounts: many(accounts),
+}));
+
+export const accounts = createTable(
+ "account",
+ {
+ userId: text("userId", { length: 255 })
+ .notNull()
+ .references(() => users.id),
+ type: text("type", { length: 255 })
+ .$type<AdapterAccount["type"]>()
+ .notNull(),
+ provider: text("provider", { length: 255 }).notNull(),
+ providerAccountId: text("providerAccountId", { length: 255 }).notNull(),
+ refresh_token: text("refresh_token"),
+ access_token: text("access_token"),
+ expires_at: int("expires_at"),
+ token_type: text("token_type", { length: 255 }),
+ scope: text("scope", { length: 255 }),
+ id_token: text("id_token"),
+ session_state: text("session_state", { length: 255 }),
+ },
+ (account) => ({
+ compoundKey: primaryKey({
+ columns: [account.provider, account.providerAccountId],
+ }),
+ userIdIdx: index("account_userId_idx").on(account.userId),
+ })
+);
+
+export const accountsRelations = relations(accounts, ({ one }) => ({
+ user: one(users, { fields: [accounts.userId], references: [users.id] }),
+}));
+
+export const sessions = createTable(
+ "session",
+ {
+ sessionToken: text("sessionToken", { length: 255 }).notNull().primaryKey(),
+ userId: text("userId", { length: 255 })
+ .notNull()
+ .references(() => users.id),
+ expires: int("expires", { mode: "timestamp" }).notNull(),
+ },
+ (session) => ({
+ userIdIdx: index("session_userId_idx").on(session.userId),
+ })
+);
+
+export const sessionsRelations = relations(sessions, ({ one }) => ({
+ user: one(users, { fields: [sessions.userId], references: [users.id] }),
+}));
+
+export const verificationTokens = createTable(
+ "verificationToken",
+ {
+ identifier: text("identifier", { length: 255 }).notNull(),
+ token: text("token", { length: 255 }).notNull(),
+ expires: int("expires", { mode: "timestamp" }).notNull(),
+ },
+ (vt) => ({
+ compoundKey: primaryKey({ columns: [vt.identifier, vt.token] }),
+ })
+);