From 4a97d5c3aec941c50ff5c619654f24077a103ee8 Mon Sep 17 00:00:00 2001 From: Cris Lom <36197748+Lombardoc4@users.noreply.github.com> Date: Tue, 19 Mar 2024 18:57:15 -0400 Subject: [PATCH] feat: FRON-33 shared secret, bearer token (#33) * feat: FRON-33 shared secret used in fetch calls --- .env.example | 3 ++- README.md | 8 +++++++- src/app/page.tsx | 2 +- src/components/Footer/index.tsx | 17 ++++++++++++++--- src/components/SelectionData/NextEvent.tsx | 4 ++++ src/components/TopNav/UserNav.tsx | 2 +- src/lib/helpers.tsx | 16 ++++++++++------ 7 files changed, 39 insertions(+), 13 deletions(-) diff --git a/.env.example b/.env.example index 16df694..567c429 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,5 @@ # DEVELOPMENT TOOLS # Ideally, don't add them to production deployment envs # Change to true if we want to log data -NEXT_PUBLIC_SHOW_LOGGER="false" \ No newline at end of file +# NEXT_PUBLIC_SHOW_LOGGER="false" +NEXT_PUBLIC_BEARER_TOKEN="my-secret-token" \ No newline at end of file diff --git a/README.md b/README.md index 3dfa599..15a6ee3 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ Table of Contents: - [Getting Started](#getting-started) - [Install dependencies](#install-dependencies) - [Run the development server](#run-the-development-server) + - [Connecting to the server](#connecting-to-the-server) - [Commit Message Convention](#commit-message-convention) - [Contribution Guidelines](#contribution-guidelines) - [Tests](#tests) @@ -44,9 +45,14 @@ You can start the server using this command: pnpm dev ``` -csd Open [http://localhost:3000](http://localhost:3000) with your browser. +### Connecting to the server + +Currently the standard is to run the backend locally. Follow instructions [here](https://github.com/Slick-Telemetry/backend/blob/dev/README.md) for setup. + +To make calls to API you need to duplicate the `.env.example` file to define a client-side bearer token + ### Commit Message Convention This project is using [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/), it is mandatory to use it to commit changes. diff --git a/src/app/page.tsx b/src/app/page.tsx index c522141..bc2940d 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,7 +1,7 @@ import Link from 'next/link'; import { BsPlusCircle } from 'react-icons/bs'; -import { NextEvent } from '../components/SelectionData/NextEvent'; +import { NextEvent } from '@/components/SelectionData'; export default function Home() { return ( diff --git a/src/components/Footer/index.tsx b/src/components/Footer/index.tsx index 0effd8f..e92bcd0 100644 --- a/src/components/Footer/index.tsx +++ b/src/components/Footer/index.tsx @@ -1,5 +1,16 @@ -const Footer = () => { - return
Footer
; -}; +import { fetchAPI } from '@/lib/helpers'; + +async function Footer() { + const serverStatus = await fetchAPI('health', true); + + return ( +
+

Footer

+

+ Server Status: {serverStatus.status || 'Offline'} +

+
+ ); +} export { Footer }; diff --git a/src/components/SelectionData/NextEvent.tsx b/src/components/SelectionData/NextEvent.tsx index cb72e2e..5647482 100644 --- a/src/components/SelectionData/NextEvent.tsx +++ b/src/components/SelectionData/NextEvent.tsx @@ -5,8 +5,10 @@ import { useAtom } from 'jotai'; import { formatDuration } from '@/lib/helpers'; import { formatSessionUrl } from '@/lib/transformers'; +import { fetchNextEvent } from '@/atoms/fetchCalls'; import { nextEventAtom, + nextEventEffect, nextEventLiveAtom, nextEventTimeAtom, } from '@/atoms/nextEvent'; @@ -15,6 +17,8 @@ export const NextEvent = () => { const [nextEvent] = useAtom(nextEventAtom); const [liveEvent] = useAtom(nextEventLiveAtom); const [nextEventCountdown] = useAtom(nextEventTimeAtom); + useAtom(fetchNextEvent); + useAtom(nextEventEffect); return (
diff --git a/src/components/TopNav/UserNav.tsx b/src/components/TopNav/UserNav.tsx index e6f8b8d..bf02f8c 100644 --- a/src/components/TopNav/UserNav.tsx +++ b/src/components/TopNav/UserNav.tsx @@ -9,7 +9,7 @@ const UserNav = () => { > diff --git a/src/lib/helpers.tsx b/src/lib/helpers.tsx index 7852698..341b837 100644 --- a/src/lib/helpers.tsx +++ b/src/lib/helpers.tsx @@ -117,9 +117,14 @@ export const fetchAPI = async ( endpoint: string, statusCheck: boolean = false, ) => { - const useServer = statusCheck || document.body.classList.contains('server'); + // const useServer = statusCheck || document.body.classList.contains('server'); // Headers for statusCheck so - const options = statusCheck ? { headers: { cache: 'no-store' } } : {}; + const options = { + headers: { + Authorization: `Bearer ${process.env.NEXT_PUBLIC_BEARER_TOKEN}`, + cache: statusCheck ? 'no-store' : 'force-cache', + }, + }; // Get dummy data or return false const dummy: string[] | DataConfigSchema['schedule'] | false = @@ -128,12 +133,11 @@ export const fetchAPI = async ( ] || false; // If we are not using the server return the dummy data - if (!useServer) { - return dummy; - } + // if (!useServer) { + // return dummy; + // } // Fetch from server - // console.log(`making fetch to: ${serverUrl}/${endpoint}`); const data = await fetch(`${serverUrl}/${endpoint}`, { ...options }) .then( (res) => {