fix: improve typesafety

This commit is contained in:
Mythie
2023-07-31 13:53:55 +10:00
parent 664357d166
commit 0fca2e9f4e
4 changed files with 36 additions and 21 deletions

View File

@@ -2,6 +2,10 @@
NEXTAUTH_URL="http://localhost:3000" NEXTAUTH_URL="http://localhost:3000"
NEXTAUTH_SECRET="secret" NEXTAUTH_SECRET="secret"
# [[AUTH OPTIONAL]]
NEXT_PRIVATE_GOOGLE_CLIENT_ID=""
NEXT_PRIVATE_GOOGLE_CLIENT_SECRET=""
# [[APP]] # [[APP]]
NEXT_PUBLIC_SITE_URL="http://localhost:3000" NEXT_PUBLIC_SITE_URL="http://localhost:3000"
NEXT_PUBLIC_APP_URL="http://localhost:3000" NEXT_PUBLIC_APP_URL="http://localhost:3000"

View File

@@ -2,7 +2,7 @@ import { PrismaAdapter } from '@next-auth/prisma-adapter';
import { compare } from 'bcrypt'; import { compare } from 'bcrypt';
import { AuthOptions, Session, User } from 'next-auth'; import { AuthOptions, Session, User } from 'next-auth';
import CredentialsProvider from 'next-auth/providers/credentials'; import CredentialsProvider from 'next-auth/providers/credentials';
import GoogleProvider from 'next-auth/providers/google'; import GoogleProvider, { GoogleProfile } from 'next-auth/providers/google';
import { prisma } from '@documenso/prisma'; import { prisma } from '@documenso/prisma';
@@ -41,20 +41,20 @@ export const NEXT_AUTH_OPTIONS: AuthOptions = {
} }
return { return {
id: String(user.id) as any, id: Number(user.id),
email: user.email, email: user.email,
name: user.name, name: user.name,
} satisfies User; } satisfies User;
}, },
}), }),
GoogleProvider({ GoogleProvider<GoogleProfile>({
clientId: process.env.NEXT_PRIVATE_GOOGLE_CLIENT_ID ?? '', clientId: process.env.NEXT_PRIVATE_GOOGLE_CLIENT_ID ?? '',
clientSecret: process.env.NEXT_PRIVATE_GOOGLE_CLIENT_SECRET ?? '', clientSecret: process.env.NEXT_PRIVATE_GOOGLE_CLIENT_SECRET ?? '',
allowDangerousEmailAccountLinking: true, allowDangerousEmailAccountLinking: true,
profile(profile) { profile(profile) {
return { return {
id: profile.sub as any, id: Number(profile.sub),
name: profile.name, name: profile.name || `${profile.given_name} ${profile.family_name}`.trim(),
email: profile.email, email: profile.email,
}; };
}, },
@@ -62,39 +62,42 @@ export const NEXT_AUTH_OPTIONS: AuthOptions = {
], ],
callbacks: { callbacks: {
async jwt({ token, user }) { async jwt({ token, user }) {
const dbUser = await prisma.user.findFirst({ if (!token.email) {
throw new Error('No email in token');
}
const retrievedUser = await prisma.user.findFirst({
where: { where: {
email: token.email as string, email: token.email,
}, },
}); });
if (!dbUser) { if (!retrievedUser) {
if (user) { return {
token.id = user?.id; ...token,
} id: user.id,
return token; };
} }
return { return {
id: dbUser.id, id: retrievedUser.id,
name: dbUser.name, name: retrievedUser.name,
email: dbUser.email, email: retrievedUser.email,
}; };
}, },
async session({ token, session }) { async session({ token, session }) {
console.log('session', { token, session }); if (token && token.email) {
if (token) { return {
const documensoSession = {
...session, ...session,
user: { user: {
id: Number(token.id), id: Number(token.id),
name: token.name, name: token.name,
email: token.email, email: token.email,
}, },
} as Session; } satisfies Session;
return documensoSession;
} }
return session; return session;
}, },
}, },

View File

@@ -0,0 +1,5 @@
{
"extends": "@documenso/tsconfig/react-library.json",
"include": ["."],
"exclude": ["dist", "build", "node_modules"]
}

View File

@@ -2,6 +2,9 @@ declare namespace NodeJS {
export interface ProcessEnv { export interface ProcessEnv {
NEXT_PUBLIC_SITE_URL?: string; NEXT_PUBLIC_SITE_URL?: string;
NEXT_PRIVATE_GOOGLE_CLIENT_ID?: string;
NEXT_PRIVATE_GOOGLE_CLIENT_SECRET?: string;
NEXT_PRIVATE_DATABASE_URL: string; NEXT_PRIVATE_DATABASE_URL: string;
NEXT_PUBLIC_STRIPE_COMMUNITY_PLAN_MONTHLY_PRICE_ID: string; NEXT_PUBLIC_STRIPE_COMMUNITY_PLAN_MONTHLY_PRICE_ID: string;