feat: google auth without schema change

This commit is contained in:
Doug Andrade
2023-06-13 01:53:12 -04:00
parent 05238f096b
commit 2b84636993
5 changed files with 84 additions and 12 deletions

View File

@ -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;
},
},
};

23
packages/lib/types/next-auth.d.ts vendored Normal file
View 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;
}
}