Skip to content

Commit

Permalink
Create API Handshake (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
JorWo authored Feb 12, 2024
1 parent 5a74e15 commit 9c0cf0b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
31 changes: 31 additions & 0 deletions ui/api-handshake.js
Original file line number Diff line number Diff line change
@@ -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);
});
}
1 change: 1 addition & 0 deletions ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 1 addition & 3 deletions ui/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -15,7 +13,7 @@ export const middleware = async (req: NextRequest) => {
const session = await getIronSession<SessionData>(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)) {
Expand Down
2 changes: 1 addition & 1 deletion ui/tailwind.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const config = {
'md': '768px',
'lg': '992px',
'xl': '1200px',
'2xl': "1400px"
'2xl': '1400px'
}
},
plugins: [
Expand Down

0 comments on commit 9c0cf0b

Please sign in to comment.