From 512ce16099b7a280fba738a0766e32a143b984a7 Mon Sep 17 00:00:00 2001 From: Rajgupta36 Date: Sun, 8 Jun 2025 18:49:44 +0530 Subject: [PATCH 1/7] handle empty auth credentials --- .../src/app/api/auth/[...nextauth]/route.ts | 23 ++++++++++++++----- frontend/src/app/login/page.tsx | 11 ++++++++- frontend/src/components/UserMenu.tsx | 8 ++++--- frontend/src/utils/constants.ts | 3 +++ frontend/src/utils/credentials.ts | 2 +- 5 files changed, 36 insertions(+), 11 deletions(-) diff --git a/frontend/src/app/api/auth/[...nextauth]/route.ts b/frontend/src/app/api/auth/[...nextauth]/route.ts index fb7f22c16a..32f338077c 100644 --- a/frontend/src/app/api/auth/[...nextauth]/route.ts +++ b/frontend/src/app/api/auth/[...nextauth]/route.ts @@ -2,6 +2,7 @@ import { gql } from '@apollo/client' import NextAuth from 'next-auth' import GitHubProvider from 'next-auth/providers/github' import { apolloClient } from 'server/apolloClient' +import { isAuthEnable } from 'utils/constants' import { GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET, @@ -9,18 +10,28 @@ import { NEXTAUTH_URL, } from 'utils/credentials' -const authOptions = { - providers: [ +const providers = [] + +if (isAuthEnable) { + providers.push( GitHubProvider({ - clientId: GITHUB_CLIENT_ID!, - clientSecret: GITHUB_CLIENT_SECRET!, - }), - ], + clientId: GITHUB_CLIENT_ID, + clientSecret: GITHUB_CLIENT_SECRET, + }) + ) +} + +const authOptions = { + providers, session: { strategy: 'jwt' as const, }, callbacks: { async signIn({ account }) { + if (!isAuthEnable && account?.provider === 'github') { + return false + } + if (account?.provider === 'github' && account.access_token) { try { const { data } = await apolloClient.mutate({ diff --git a/frontend/src/app/login/page.tsx b/frontend/src/app/login/page.tsx index 9083e9bdce..58c93e8906 100644 --- a/frontend/src/app/login/page.tsx +++ b/frontend/src/app/login/page.tsx @@ -7,7 +7,7 @@ import { addToast } from '@heroui/toast' import { useRouter } from 'next/navigation' import { useSession, signIn } from 'next-auth/react' import { useEffect } from 'react' -import { userAuthStatus } from 'utils/constants' +import { isAuthEnable, userAuthStatus } from 'utils/constants' export default function LoginPage() { const { status } = useSession() @@ -27,6 +27,15 @@ export default function LoginPage() { } }, [status, router]) + if (!isAuthEnable) { + return ( +
+ + Authentication is disabled +
+ ) + } + if (status === userAuthStatus.LOADING) { return (
diff --git a/frontend/src/components/UserMenu.tsx b/frontend/src/components/UserMenu.tsx index e448aa73ba..7222419db7 100644 --- a/frontend/src/components/UserMenu.tsx +++ b/frontend/src/components/UserMenu.tsx @@ -3,7 +3,7 @@ import { Dropdown, DropdownTrigger, DropdownMenu, DropdownItem, Skeleton } from '@heroui/react' import Image from 'next/image' import { useSession, signIn, signOut } from 'next-auth/react' -import { userAuthStatus } from 'utils/constants' +import { isAuthEnable, userAuthStatus } from 'utils/constants' export default function UserMenu() { const { data: session, status } = useSession() @@ -15,10 +15,12 @@ export default function UserMenu() {
) } - if (!session) { + + if (!session || !isAuthEnable) { return ( @@ -28,27 +42,41 @@ export default function UserMenu() { } return ( - - - User avatar - +
+ - - signOut({ callbackUrl: '/' })} - className="relative flex h-10 w-full cursor-pointer items-center justify-center gap-2 whitespace-pre rounded-md bg-[#87a1bc] px-4 py-2 text-sm font-medium text-black focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50 dark:bg-slate-900 dark:text-white" + {isOpen && ( +
- Sign out - - - + +
+ )} +
) }