diff --git a/src/beacon/app/api/auth/[...nextauth]/route.ts b/src/beacon/app/api/auth/[...nextauth]/route.ts index 1b06e23c44..0a4c217bc5 100644 --- a/src/beacon/app/api/auth/[...nextauth]/route.ts +++ b/src/beacon/app/api/auth/[...nextauth]/route.ts @@ -1,8 +1,6 @@ import NextAuth from 'next-auth'; import { authOptions } from '@/lib/auth'; -const handler = (req: any, res: any) => { - return NextAuth(req, res, authOptions); -}; +const handler = NextAuth(authOptions); export { handler as GET, handler as POST }; diff --git a/src/beacon/app/login/page.tsx b/src/beacon/app/login/page.tsx index d53e13b405..0b75d848c4 100644 --- a/src/beacon/app/login/page.tsx +++ b/src/beacon/app/login/page.tsx @@ -136,7 +136,7 @@ export default function LoginPage() { authService.clearAllAuthData() // Clean up the URL router.replace('/login') - } else if (status === 'authenticated') { + } else if (status === 'authenticated' && !isTransitioning) { // User is authenticated, let middleware or client redirect them const isAirqoAdmin = session?.user?.organization === 'AirQo' && (session?.user?.privilege?.toLowerCase()?.includes('admin') || @@ -149,7 +149,7 @@ export default function LoginPage() { router.push("/dashboard/devices") } } - }, [router, status, session]) + }, [router, status, session, isTransitioning]) /** * Validates email format diff --git a/src/beacon/lib/auth.ts b/src/beacon/lib/auth.ts index e9cce31d75..2c7afc1bb8 100644 --- a/src/beacon/lib/auth.ts +++ b/src/beacon/lib/auth.ts @@ -12,15 +12,16 @@ const normalizeNextAuthUrl = () => { const currentEnv = config.environment; const rawUrl = process.env.NEXTAUTH_URL; - // Check if NEXTAUTH_URL is missing or incorrectly set to localhost in staging/production + // Check if NEXTAUTH_URL is missing, localhost, or incorrectly set to production on staging const isLocalhostUrl = !rawUrl || rawUrl.includes('localhost') || rawUrl.includes('127.0.0.1'); + const isProdUrlOnStaging = rawUrl === 'https://beacon.airqo.net' && currentEnv === 'staging'; if (currentEnv === 'staging') { - if (isLocalhostUrl) { + if (isLocalhostUrl || isProdUrlOnStaging) { process.env.NEXTAUTH_URL = 'https://staging-beacon.airqo.net'; } } else if (currentEnv === 'production') { - if (isLocalhostUrl) { + if (isLocalhostUrl || rawUrl === 'https://staging-beacon.airqo.net') { process.env.NEXTAUTH_URL = 'https://beacon.airqo.net'; } } else { @@ -335,3 +336,4 @@ export const authOptions: NextAuthOptions = { debug: false, }; + diff --git a/src/beacon/lib/config.js b/src/beacon/lib/config.js index 58ae3217f1..75f4251712 100755 --- a/src/beacon/lib/config.js +++ b/src/beacon/lib/config.js @@ -40,12 +40,14 @@ const getEnvironment = () => { if (nextAuthUrl.includes('stage') || nextAuthUrl.includes('staging') || nextAuthUrl.includes('beacon-stage')) { return 'staging'; } + + const serviceName = (process.env.SERVICE_NAME || '').toLowerCase(); + const podHostName = (process.env.HOSTNAME || '').toLowerCase(); + if (serviceName.includes('stage') || podHostName.includes('stage')) { + return 'staging'; + } + if (nextAuthUrl.includes('localhost') || nextAuthUrl.includes('127.0.0.1')) { - const serviceName = (process.env.SERVICE_NAME || '').toLowerCase(); - const podHostName = (process.env.HOSTNAME || '').toLowerCase(); - if (serviceName.includes('stage') || podHostName.includes('stage')) { - return 'staging'; - } return 'development'; }