mirror of
https://github.com/documenso/documenso.git
synced 2025-11-10 04:22:32 +10:00
Merge pull request #207 from doug-andrade/v2-google-auth
feat: add google as auth provider **no schema change**
This commit is contained in:
@ -2,6 +2,10 @@
|
||||
NEXTAUTH_URL="http://localhost:3000"
|
||||
NEXTAUTH_SECRET="secret"
|
||||
|
||||
# [[AUTH OPTIONAL]]
|
||||
NEXT_PRIVATE_GOOGLE_CLIENT_ID=""
|
||||
NEXT_PRIVATE_GOOGLE_CLIENT_SECRET=""
|
||||
|
||||
# [[APP]]
|
||||
NEXT_PUBLIC_SITE_URL="http://localhost:3000"
|
||||
NEXT_PUBLIC_APP_URL="http://localhost:3000"
|
||||
|
||||
3
apps/web/process-env.d.ts
vendored
3
apps/web/process-env.d.ts
vendored
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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',
|
||||
|
||||
@ -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, { GoogleProfile } from 'next-auth/providers/google';
|
||||
|
||||
import { prisma } from '@documenso/prisma';
|
||||
|
||||
@ -40,19 +41,64 @@ export const NEXT_AUTH_OPTIONS: AuthOptions = {
|
||||
}
|
||||
|
||||
return {
|
||||
id: String(user.id) as any,
|
||||
id: Number(user.id),
|
||||
email: user.email,
|
||||
name: user.name,
|
||||
image: '',
|
||||
} satisfies User;
|
||||
},
|
||||
}),
|
||||
GoogleProvider<GoogleProfile>({
|
||||
clientId: process.env.NEXT_PRIVATE_GOOGLE_CLIENT_ID ?? '',
|
||||
clientSecret: process.env.NEXT_PRIVATE_GOOGLE_CLIENT_SECRET ?? '',
|
||||
allowDangerousEmailAccountLinking: true,
|
||||
profile(profile) {
|
||||
return {
|
||||
id: Number(profile.sub),
|
||||
name: profile.name || `${profile.given_name} ${profile.family_name}`.trim(),
|
||||
email: profile.email,
|
||||
};
|
||||
},
|
||||
}),
|
||||
],
|
||||
// callbacks: {
|
||||
// jwt: async ({ token, user: _user }) => {
|
||||
// return {
|
||||
// ...token,
|
||||
// };
|
||||
// },
|
||||
// },
|
||||
callbacks: {
|
||||
async jwt({ token, user }) {
|
||||
if (!token.email) {
|
||||
throw new Error('No email in token');
|
||||
}
|
||||
|
||||
const retrievedUser = await prisma.user.findFirst({
|
||||
where: {
|
||||
email: token.email,
|
||||
},
|
||||
});
|
||||
|
||||
if (!retrievedUser) {
|
||||
return {
|
||||
...token,
|
||||
id: user.id,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
id: retrievedUser.id,
|
||||
name: retrievedUser.name,
|
||||
email: retrievedUser.email,
|
||||
};
|
||||
},
|
||||
|
||||
async session({ token, session }) {
|
||||
if (token && token.email) {
|
||||
return {
|
||||
...session,
|
||||
user: {
|
||||
id: Number(token.id),
|
||||
name: token.name,
|
||||
email: token.email,
|
||||
},
|
||||
} satisfies Session;
|
||||
}
|
||||
|
||||
return session;
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
5
packages/lib/tsconfig.json
Normal file
5
packages/lib/tsconfig.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"extends": "@documenso/tsconfig/react-library.json",
|
||||
"include": ["."],
|
||||
"exclude": ["dist", "build", "node_modules"]
|
||||
}
|
||||
23
packages/lib/types/next-auth.d.ts
vendored
Normal file
23
packages/lib/types/next-auth.d.ts
vendored
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
3
packages/tsconfig/process-env.d.ts
vendored
3
packages/tsconfig/process-env.d.ts
vendored
@ -2,6 +2,9 @@ declare namespace NodeJS {
|
||||
export interface ProcessEnv {
|
||||
NEXT_PUBLIC_SITE_URL?: string;
|
||||
|
||||
NEXT_PRIVATE_GOOGLE_CLIENT_ID?: string;
|
||||
NEXT_PRIVATE_GOOGLE_CLIENT_SECRET?: string;
|
||||
|
||||
NEXT_PRIVATE_DATABASE_URL: string;
|
||||
|
||||
NEXT_PUBLIC_STRIPE_COMMUNITY_PLAN_MONTHLY_PRICE_ID: string;
|
||||
|
||||
@ -19,6 +19,8 @@
|
||||
"NEXT_PRIVATE_DATABASE_URL",
|
||||
"NEXT_PRIVATE_NEXT_AUTH_SECRET",
|
||||
"NEXT_PUBLIC_SUBSCRIPTIONS_ENABLED",
|
||||
"NEXT_PRIVATE_GOOGLE_CLIENT_ID",
|
||||
"NEXT_PRIVATE_GOOGLE_CLIENT_SECRET",
|
||||
"NEXT_PRIVATE_SMTP_TRANSPORT",
|
||||
"NEXT_PRIVATE_MAILCHANNELS_API_KEY",
|
||||
"NEXT_PRIVATE_MAILCHANNELS_ENDPOINT",
|
||||
|
||||
Reference in New Issue
Block a user