From 9c0cf0b47c53288195560a50bd5cd8604310d6dc Mon Sep 17 00:00:00 2001 From: Jordan Wong <42422209+JorWo@users.noreply.github.com> Date: Mon, 12 Feb 2024 12:43:05 -1000 Subject: [PATCH] Create API Handshake (#2) --- ui/api-handshake.js | 31 +++++++++++++++++++++++++++++++ ui/package.json | 1 + ui/src/middleware.ts | 4 +--- ui/tailwind.config.ts | 2 +- 4 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 ui/api-handshake.js diff --git a/ui/api-handshake.js b/ui/api-handshake.js new file mode 100644 index 00000000..26b5e06c --- /dev/null +++ b/ui/api-handshake.js @@ -0,0 +1,31 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ +const axios = require('axios'); +const env = require('@next/env'); + +env.loadEnvConfig(process.cwd()); + +const apiBaseUrl = process.env.NEXT_PUBLIC_API_2_1_BASE_URL; +const apiHandshakeEnabled = process.env.API_HANDSHAKE_ENABLED ?? true; + +const errorMessage = +`*************************** +APPLICATION FAILED TO START +*************************** + +Description: + +Could not connect to the API server. + +Action: + +Start the UH Groupings API first +or check the connection configuration and +fix any possible problems.\n`; + +if (apiHandshakeEnabled) { + axios.get(`${apiBaseUrl}/`) + .catch(() => { + console.log(errorMessage); + process.exit(1); + }); +} diff --git a/ui/package.json b/ui/package.json index 823844cb..9f2ba453 100644 --- a/ui/package.json +++ b/ui/package.json @@ -3,6 +3,7 @@ "version": "3.0", "private": true, "scripts": { + "predev": "node api-handshake.js", "dev": "next dev -p 8080", "build": "next build", "start": "next start", diff --git a/ui/src/middleware.ts b/ui/src/middleware.ts index 4a3676ce..81c7dbcb 100644 --- a/ui/src/middleware.ts +++ b/ui/src/middleware.ts @@ -3,8 +3,6 @@ import { NextResponse, NextRequest } from 'next/server'; import { getIronSession } from 'iron-session'; import { SessionData, SessionOptions } from './access/Session'; import Role from './access/Role'; -import { isDeepStrictEqual } from 'util'; -import { AnonymousUser } from './access/User'; /** * Next.js middleware function that is called upon visiting a route that matches the config. @@ -15,7 +13,7 @@ export const middleware = async (req: NextRequest) => { const session = await getIronSession(cookies(), SessionOptions); const { user } = session; - if (!user || isDeepStrictEqual(user, AnonymousUser)) { + if (!user) { return NextResponse.redirect(new URL('/uhgroupings', req.url)); } if (req.url.endsWith('/admin') && !user.roles.includes(Role.ADMIN)) { diff --git a/ui/tailwind.config.ts b/ui/tailwind.config.ts index 2bed1a01..5f0d127f 100644 --- a/ui/tailwind.config.ts +++ b/ui/tailwind.config.ts @@ -49,7 +49,7 @@ const config = { 'md': '768px', 'lg': '992px', 'xl': '1200px', - '2xl': "1400px" + '2xl': '1400px' } }, plugins: [