diff --git a/apps/server/src/modules/auth/next-auth-options.ts b/apps/server/src/modules/auth/next-auth-options.ts index 3c7508d8a..0e060d66d 100644 --- a/apps/server/src/modules/auth/next-auth-options.ts +++ b/apps/server/src/modules/auth/next-auth-options.ts @@ -4,6 +4,7 @@ import { PrismaAdapter } from '@auth/prisma-adapter'; import { BadRequestException, FactoryProvider, Logger } from '@nestjs/common'; import { verify } from '@node-rs/argon2'; import { Algorithm, sign, verify as jwtVerify } from '@node-rs/jsonwebtoken'; +import { assign, omit } from 'lodash-es'; import { nanoid } from 'nanoid'; import { NextAuthOptions } from 'next-auth'; import Credentials from 'next-auth/providers/credentials'; @@ -123,8 +124,24 @@ export const NextAuthOptionsProvider: FactoryProvider = { session: { strategy: config.node.prod ? 'database' : 'jwt', }, - // @ts-expect-error Third part library type mismatch - logger: console, + logger: { + debug(code, metadata) { + logger.debug(`${code}: ${JSON.stringify(metadata)}`); + }, + error(code, metadata) { + if (metadata instanceof Error) { + // @ts-expect-error assign code to error + metadata.code = code; + logger.error(metadata); + } else if (metadata.error instanceof Error) { + assign(metadata.error, omit(metadata, 'error'), { code }); + logger.error(metadata.error); + } + }, + warn(code) { + logger.warn(code); + }, + }, }; nextAuthOptions.providers.push( diff --git a/apps/server/src/modules/auth/next-auth.controller.ts b/apps/server/src/modules/auth/next-auth.controller.ts index 65907cf56..acb5ebb5a 100644 --- a/apps/server/src/modules/auth/next-auth.controller.ts +++ b/apps/server/src/modules/auth/next-auth.controller.ts @@ -131,12 +131,13 @@ export class NextAuthController { } if (redirect?.endsWith('api/auth/error?error=AccessDenied')) { + this.logger.log('Early access redirect headers ', req.headers); + this.metrics.authFailCounter(1, { + reason: 'no_early_access_permission', + }); if (!req.headers?.referer) { res.redirect('https://community.affine.pro/c/insider-general/'); } else { - this.metrics.authFailCounter(1, { - reason: 'no_early_access_permission', - }); res.status(403); res.json({ url: 'https://community.affine.pro/c/insider-general/',