Skip to content

The Open Source DocuSign Alternative.

feat: google auth without schema change

Doug Andrade6253c42ca11b
diff --git a/apps/web/process-env.d.ts b/apps/web/process-env.d.ts
index 92a6401fc..e91137fe1 100644
--- a/apps/web/process-env.d.ts
+++ b/apps/web/process-env.d.ts
@@ -11,5 +11,8 @@ declare namespace NodeJS {
NEXT_PRIVATE_STRIPE_WEBHOOK_SECRET: string;
NEXT_PUBLIC_SUBSCRIPTIONS_ENABLED: string;
+
+ NEXT_PRIVATE_GOOGLE_CLIENT_ID: string;
+ NEXT_PRIVATE_GOOGLE_CLIENT_SECRET: string;
}
}
diff --git a/apps/web/src/components/forms/signin.tsx b/apps/web/src/components/forms/signin.tsx
index be1991e95..449863e71 100644
--- a/apps/web/src/components/forms/signin.tsx
+++ b/apps/web/src/components/forms/signin.tsx
@@ -61,8 +61,8 @@ export const SignInForm = ({ className }: SignInFormProps) => {
const onSignInWithGoogleClick = async () => {
try {
- // await signIn('google', { callbackUrl: '/dashboard' });
- throw new Error('Not implemented');
+ await signIn('google', { callbackUrl: '/dashboard' });
+ // throw new Error('Not implemented');
} catch (err) {
toast({
title: 'An unknown error occurred',
diff --git a/packages/lib/next-auth/auth-options.ts b/packages/lib/next-auth/auth-options.ts
index 4d3c9fa4d..6c285fae3 100644
--- a/packages/lib/next-auth/auth-options.ts
+++ b/packages/lib/next-auth/auth-options.ts
@@ -1,7 +1,8 @@
import { PrismaAdapter } from '@next-auth/prisma-adapter';
import { compare } from 'bcrypt';
-import { AuthOptions, User } from 'next-auth';
+import { AuthOptions, Session, User } from 'next-auth';
import CredentialsProvider from 'next-auth/providers/credentials';
+import GoogleProvider from 'next-auth/providers/google';
import { prisma } from '@documenso/prisma';
@@ -44,16 +45,59 @@ export const NEXT_AUTH_OPTIONS: AuthOptions = {
id: String(user.id) as any,
email: user.email,
name: user.name,
- image: '',
} satisfies User;
},
}),
+ GoogleProvider({
+ clientId: process.env.NEXT_PRIVATE_GOOGLE_CLIENT_ID ?? '',
+ clientSecret: process.env.NEXT_PRIVATE_GOOGLE_CLIENT_SECRET ?? '',
+ allowDangerousEmailAccountLinking: true,
+ profile(profile) {
+ return {
+ id: profile.sub as any,
+ name: profile.name,
+ email: profile.email,
+ };
+ },
+ }),
],
- // callbacks: {
- // jwt: async ({ token, user: _user }) => {
- // return {
- // ...token,
- // };
- // },
- // },
+ callbacks: {
+ async jwt({ token, user, account, profile }) {
+ console.log('jwt', { token, user, account, profile });
+ const dbUser = await prisma.user.findFirst({
+ where: {
+ email: token.email as string,
+ },
+ });
+
+ if (!dbUser) {
+ if (user) {
+ token.id = user?.id;
+ }
+ return token;
+ }
+
+ return {
+ id: dbUser.id,
+ name: dbUser.name,
+ email: dbUser.email,
+ };
+ },
+ async session({ token, session }) {
+ console.log('session', { token, session });
+ if (token) {
+ const documensoSession = {
+ ...session,
+ user: {
+ id: Number(token.id),
+ name: token.name,
+ email: token.email,
+ },
+ } as Session;
+
+ return documensoSession;
+ }
+ return session;
+ },
+ },
};
diff --git a/packages/lib/types/next-auth.d.ts b/packages/lib/types/next-auth.d.ts
new file mode 100644
index 000000000..c36fbbc5f
--- /dev/null
+++ b/packages/lib/types/next-auth.d.ts
@@ -0,0 +1,23 @@
+import type { User as PrismaUser } from '@prisma/client';
+import type { DefaultUser } from 'next-auth';
+
+declare module 'next-auth' {
+ interface Session {
+ user: User;
+ }
+
+ interface User extends Omit<DefaultUser, 'id' | 'image'> {
+ id: PrismaUser['id'];
+ email?: PrismaUser['email'];
+ name?: PrismaUser['name'];
+ emailVerified?: PrismaUser['emailVerified'];
+ }
+}
+
+declare module 'next-auth/jwt' {
+ interface JWT {
+ id: string | number;
+ name?: string | null;
+ email: string | null;
+ }
+}
diff --git a/turbo.json b/turbo.json
index 388f952af..1d40fcdbd 100644
--- a/turbo.json
+++ b/turbo.json
@@ -18,6 +18,8 @@
"NEXT_PUBLIC_SITE_URL",
"NEXT_PRIVATE_DATABASE_URL",
"NEXT_PRIVATE_NEXT_AUTH_SECRET",
- "NEXT_PUBLIC_SUBSCRIPTIONS_ENABLED"
+ "NEXT_PUBLIC_SUBSCRIPTIONS_ENABLED",
+ "NEXT_PRIVATE_GOOGLE_CLIENT_ID",
+ "NEXT_PRIVATE_GOOGLE_CLIENT_SECRET"
]
}