aboutsummaryrefslogtreecommitdiff
path: root/apps/web/src/server/auth.ts
diff options
context:
space:
mode:
authorDhravya <[email protected]>2024-02-21 16:11:35 -0700
committerDhravya <[email protected]>2024-02-21 16:11:35 -0700
commitcdceb1bc87dc2d5ef42a7f172ea5dc2263c5c526 (patch)
treee0234f7fff63b312f18c945bed37a430d1e68755 /apps/web/src/server/auth.ts
downloadsupermemory-cdceb1bc87dc2d5ef42a7f172ea5dc2263c5c526.tar.xz
supermemory-cdceb1bc87dc2d5ef42a7f172ea5dc2263c5c526.zip
initialised monorepo with auth and extension communication
Diffstat (limited to 'apps/web/src/server/auth.ts')
-rw-r--r--apps/web/src/server/auth.ts74
1 files changed, 74 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);