Skip to content

Commit

Permalink
chore: fixing sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
paulushcgcj committed Nov 4, 2024
1 parent 06f7012 commit 6bb2353
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 23 deletions.
4 changes: 2 additions & 2 deletions frontend/src/actions/userAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from '../constants/userConstants';
import { AppDispatch } from '../store';
import { UserClientRolesType } from '../types/UserRoleType';
import { useAuth } from '../contexts/AuthProvider';
import { useGetAuth } from '../contexts/AuthProvider';

const FAM_LOGIN_USER = 'famLoginUser';

Expand All @@ -16,7 +16,7 @@ export const getUserDetails = () => async (dispatch: AppDispatch) => {
type: USER_DETAILS_REQUEST
});
//first call the isCurrent and only after that extract the JSON
const { isLoggedIn } = useAuth();
const { isLoggedIn } = useGetAuth();

const userJSON = localStorage.getItem(FAM_LOGIN_USER); // Retrieve the JSON string from local storage
const user = userJSON ? JSON.parse(userJSON) : null; // Parse the JSON string to a JavaScript object
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/MyProfile/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import { useThemePreference } from '../../utils/ThemePreference';
import PanelSectionName from '../PanelSectionName';
import OrganizationSelection from '../OrganizationSelection';
import './MyProfile.scss';
import { useAuth } from '../../contexts/AuthProvider';
import { useGetAuth } from '../../contexts/AuthProvider';

const MyProfile = () => {
const { theme, setTheme } = useThemePreference();
const [goTo, setGoTo] = useState<boolean>(false);
const { logout, user: authUser } = useAuth();
const { logout, user: authUser } = useGetAuth();

const changeTheme = () => {
if (theme === 'g10') {
Expand Down
28 changes: 15 additions & 13 deletions frontend/src/contexts/AuthProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { createContext, useState, useContext, useEffect, ReactNode } from 'react';
import React, { createContext, useState, useContext, useEffect, useMemo, ReactNode } from 'react';
import { fetchAuthSession, signInWithRedirect, signOut } from "aws-amplify/auth";
import { parseToken, FamLoginUser } from "../services/AuthService";
import { extractGroups } from '../utils/famUtils';
Expand Down Expand Up @@ -61,7 +61,7 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
if(idToken){
return Promise.resolve(parseToken(idToken));
}
return Promise.reject('No user details found');
return Promise.reject(new Error('No user details found'));
};

const login = async (provider: string) => {
Expand All @@ -80,27 +80,29 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
window.location.href = '/'; // Optional redirect after logout
};

const contextValue = useMemo(() => ({
user,
userRoles,
isLoggedIn,
isLoading,
login,
logout,
userDetails
}), [user, userRoles, isLoggedIn, isLoading]);

return (
<AuthContext.Provider value={{ user, userRoles, isLoggedIn, isLoading, login, logout, userDetails }}>
<AuthContext.Provider value={contextValue}>
{children}
</AuthContext.Provider>
);
};

// This is a helper hook to use the Auth context more easily
// 5. Create a custom hook to consume the context safely
export const useAuth = (): AuthContextType => {
const context = useContext(AuthContext);
if (!context) {
throw new Error('useAuth must be used within an AuthProvider');
}
return context;
};

export const getAuth = () => {
export const useGetAuth = (): AuthContextType => {
const context = useContext(AuthContext);
if (!context) {
throw new Error('AuthProvider not found');
throw new Error('useGetAuth must be used within an AuthProvider');
}
return context;
};
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/routes/ProtectedRoute.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { Navigate, Outlet } from 'react-router-dom';
import { useAuth } from '../contexts/AuthProvider';
import { useGetAuth } from '../contexts/AuthProvider';
import { Loading } from "@carbon/react";

interface ProtectedRouteProps {
Expand All @@ -14,7 +14,7 @@ const ProtectedRoute: React.FC<ProtectedRouteProps> = ({
requiredRoles = [],
redirectTo = '/'
}) => {
const { isLoggedIn, isLoading, userRoles } = useAuth();
const { isLoggedIn, isLoading, userRoles } = useGetAuth();

if(isLoading) {
return <Loading className={'some-class'} withOverlay={true} />;
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/screens/DashboardRedirect/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from "react";
import { Navigate } from "react-router-dom";
import { Loading } from "@carbon/react";
import { useAuth } from "../../contexts/AuthProvider";
import { useGetAuth } from "../../contexts/AuthProvider";

const DashboardRedirect: React.FC = () => {
const { user } = useAuth();
const { user } = useGetAuth();
return (
<>
{user ? <Navigate to={"/opening"} replace /> : <Loading className={'some-class'} withOverlay={true} />}
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/screens/Landing/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import './Landing.scss';
import '../../custom.scss';
import { useLottie } from "lottie-react";
import silvaLottie from "../../assets/lotties/silva-logo-lottie-1.json"
import { useAuth } from "../../contexts/AuthProvider";
import { useGetAuth } from "../../contexts/AuthProvider";

const Landing: React.FC = () => {

const { login, isLoggedIn } = useAuth();
const { login, isLoggedIn } = useGetAuth();

// Adding the Lottie Loader and loading the View for lottie with initial options
const options = {
Expand Down

0 comments on commit 6bb2353

Please sign in to comment.