Skip to content

Commit

Permalink
#170 - on landing media home page, if token has expired, it will take…
Browse files Browse the repository at this point in the history
… it back to avni web app home and hence to login screen
  • Loading branch information
petmongrels committed Mar 6, 2024
1 parent 335a1c6 commit 3f2bf23
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion client/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ build-app-rwb-prod:

build-app-rwb-staging:
$(call _create_env,rwb.staging)
npm run build
npm run build
5 changes: 2 additions & 3 deletions client/components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Disclosure } from "@headlessui/react";
import Box from "@mui/material/Box";
import SvgIcon, { SvgIconProps } from "@mui/material/SvgIcon";
import Link from "next/link";
import {getAppHomeUrl} from "@/utils/ConfigUtil";

function HomeIcon(props: SvgIconProps) {
return (
Expand All @@ -15,8 +16,6 @@ export default function Navbar() {
return (
<Disclosure as="nav" className="navbg-color">
{() => {
const env = process.env.NEXT_PUBLIC_ENVIRONMENT;
const homeUrl:any = env === "dev" ? process.env.NEXT_PUBLIC_WEBAPP_BASE_URL : "/";
return (
<>
<div className="relative flex justify-between h-16">
Expand All @@ -37,7 +36,7 @@ export default function Navbar() {
<HomeIcon
sx={{fontSize: 30}}
onClick={() => {
window.location.href = homeUrl;
window.location.href = getAppHomeUrl();
}}
/>
</Box>
Expand Down
1 change: 1 addition & 0 deletions client/env-templates/development.template
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ NEXT_PUBLIC_ETL=http://localhost:8022/etl
NEXT_PUBLIC_MEDIA_VIEWER=http://localhost:3010/media-viewer
NEXT_PUBLIC_WEB=http://localhost:8021
NEXT_PUBLIC_ENVIRONMENT=dev
NEXT_PUBLIC_WEBAPP_BASE_URL=http://localhost:6010
6 changes: 3 additions & 3 deletions client/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import ImageList from '@/components/ImageList';
import Navbar from '@/components/Navbar';
import {redirectIfNotValid} from '@/utils/helpers';

import {storeToken} from "../utils/helpers";
import {useRouter} from 'next/router';
import {isDevMode} from "@/utils/ConfigUtil";


export default function Home() {
const env = process.env.NEXT_PUBLIC_ENVIRONMENT;
const router = useRouter();
if (env === "dev") {
if (isDevMode()) {
//useSearchParams doesn't seem to work
if (router.asPath.includes("/?authToken=")) {
const authToken = router.asPath.replace("/?authToken=", "");
Expand Down
8 changes: 8 additions & 0 deletions client/utils/ConfigUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function getAppHomeUrl() {
return isDevMode() ? `${process.env.NEXT_PUBLIC_WEBAPP_BASE_URL}` : "/";
}

export function isDevMode() {
const env = process.env.NEXT_PUBLIC_ENVIRONMENT;
return env === "dev";
}
8 changes: 4 additions & 4 deletions client/utils/helpers.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from 'axios';
import jwt from 'jwt-decode';
import jwt_decode from 'jwt-decode';
import {imageType} from '../model/ImageType';
import {getAppHomeUrl} from "@/utils/ConfigUtil";

const AuthTokenName = "authToken";

Expand All @@ -16,9 +16,9 @@ export const validateAccessToken = () => {

const tokenData: any = jwt(token);
const timeNowInSeconds = Math.floor(Date.now() / 1000);
const expired = tokenData.exp >= timeNowInSeconds;
const valid = tokenData.exp >= timeNowInSeconds;
console.log("Token expires in :", tokenData.exp - timeNowInSeconds)
return expired;
return valid;
} catch (err) {
console.log('Error occurred--', err);
return false;
Expand All @@ -38,7 +38,7 @@ export const redirectIfNotValid = function() {
}

if (!validateAccessToken()) {
window.location.href = `${process.env.NEXT_PUBLIC_WEBAPP_BASE_URL}`;
window.location.href = getAppHomeUrl();
return;
}
}
Expand Down

0 comments on commit 3f2bf23

Please sign in to comment.