Skip to content

Commit

Permalink
wrapping for auth
Browse files Browse the repository at this point in the history
  • Loading branch information
chumpy committed Dec 24, 2024
1 parent 7f11741 commit 6a41ce7
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 17 deletions.
21 changes: 21 additions & 0 deletions src/applications/accredited-representative-portal/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { apiRequest } from '@department-of-veterans-affairs/platform-utilities/api';

let user = null;

export async function fetchUser() {
try {
user = await apiRequest('/accredited_representative_portal/v0/user');
return user;
} catch (error) {
user = null;
throw error;
}
}

export function getUser() {
return user;
}

export function isAuthenticated() {
return user !== null;
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import React, { useState, useRef, useEffect } from 'react';
import { useSelector } from 'react-redux';
import React, { useState, useRef, useEffect, useContext } from 'react';
import PropTypes from 'prop-types';

import UserContext from '../../../../userContext';
import { SIGN_IN_URL, SIGN_OUT_URL } from '../../../../constants';
import {
selectUserProfile,
selectIsUserLoading,
} from '../../../../selectors/user';

const generateUniqueId = () =>
`account-menu-${Math.random()
.toString(36)
.substring(2, 11)}`;

const UserNav = ({ isMobile }) => {
const profile = useSelector(selectUserProfile);
const isLoading = useSelector(selectIsUserLoading);
const user = useContext(UserContext);
const profile = user?.profile;
const isLoading = !profile;
const uniqueId = useRef(generateUniqueId());

const [isDropdownOpen, setDropdownOpen] = useState(false);
Expand Down Expand Up @@ -130,12 +126,7 @@ const UserNav = ({ isMobile }) => {
};

UserNav.propTypes = {
isLoading: PropTypes.bool,
isMobile: PropTypes.bool,
profile: PropTypes.shape({
firstName: PropTypes.string,
lastName: PropTypes.string,
}),
};

export default UserNav;
31 changes: 28 additions & 3 deletions src/applications/accredited-representative-portal/routes.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import React from 'react';
import { createBrowserRouter, useNavigation, Outlet } from 'react-router-dom';
import {
createBrowserRouter,
useNavigation,
useLoaderData,
Navigate,
Outlet,
} from 'react-router-dom';

import { VaLoadingIndicator } from '@department-of-veterans-affairs/component-library/dist/react-bindings';
import App from './containers/App';
Expand All @@ -12,6 +18,9 @@ import POARequestDetailsPage, {
poaRequestLoader,
} from './containers/POARequestDetailsPage';
import ErrorMessage from './components/common/ErrorMessage';
import UserContext from './userContext';
import { userLoader } from './userLoader';
import { SIGN_IN_URL } from './constants';

const LoadingWrapper = () => {
const navigation = useNavigation();
Expand All @@ -23,6 +32,20 @@ const LoadingWrapper = () => {
return <Outlet />;
};

const AuthenticatedRoutes = () => {
const user = useLoaderData();

if (!user || !user.profile) {
return <Navigate to={SIGN_IN_URL} replace />;
}

return (
<UserContext.Provider value={user}>
<SignedInLayoutWrapper />
</UserContext.Provider>
);
};

const router = createBrowserRouter(
[
{
Expand All @@ -33,10 +56,12 @@ const router = createBrowserRouter(
element: <LandingPage />,
},
{
element: <SignedInLayoutWrapper />,
path: '/',
element: <LoadingWrapper />,
children: [
{
element: <LoadingWrapper />,
loader: userLoader,
element: <AuthenticatedRoutes />,
children: [
{
path: 'poa-requests',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createContext } from 'react';

const UserContext = createContext(null);

export default UserContext;
16 changes: 16 additions & 0 deletions src/applications/accredited-representative-portal/userLoader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { redirect } from 'react-router';
import { fetchUser, isAuthenticated } from './auth';
import { SIGN_IN_URL } from './constants';

export async function userLoader() {
try {
const user = await fetchUser();
if (!isAuthenticated()) {
redirect(SIGN_IN_URL);
}
return user;
} catch (error) {
redirect(SIGN_IN_URL);
return null;
}
}

0 comments on commit 6a41ce7

Please sign in to comment.