Skip to content

Commit

Permalink
Sonar issues (#80)
Browse files Browse the repository at this point in the history
* Fix-Sonar Issues

* Fix-Sonar Issues

* Fix-This assertion is unnecessary since it does not change the type of the expression.

* Fix-The object passed as the value prop to the Context provider changes every render. To fix this consider wrapping it in a useMemo hook.

* Fix:Sonar Issues

* Fix:Sonar Issues

* Resolved coderabbitai suggestions

* Fix:Sonar Issues
  • Loading branch information
vidyaaKhandekar authored Dec 12, 2024
1 parent d6a9202 commit 93e6bd1
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 43 deletions.
8 changes: 4 additions & 4 deletions src/components/common/input/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ interface Option {

// Define the props for the CustomSelect component
interface CustomSelectProps {
label: string;
label?: string;
options: Option[];
placeholder?: string;
width?: string;
height?: string;
border?: string;
labelStyle?: React.CSSProperties; // Optional style for the label

placeholderStyle?: React.CSSProperties; // Optional style for the placeholder
}

Expand Down Expand Up @@ -46,8 +46,8 @@ const CustomSelect: React.FC<CustomSelectProps> = ({
paddingLeft: 2, // Remove padding from the select input
}}
>
{options.map((option, index) => (
<option key={index} value={option.value}>
{options.map((option) => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))}
Expand Down
21 changes: 13 additions & 8 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import App from "./App";
import "./components/common/i18n";

import keycloak from "./keycloak";
import { ReactKeycloakProvider } from "@react-keycloak/web";
createRoot(document.getElementById("root")!).render(
<ReactKeycloakProvider authClient={keycloak}>
<StrictMode>
<App />
</StrictMode>
</ReactKeycloakProvider>
);

const rootElement = document.getElementById("root");
if (rootElement) {
createRoot(rootElement).render(
<ReactKeycloakProvider authClient={keycloak}>
<StrictMode>
<App />
</StrictMode>
</ReactKeycloakProvider>
);
} else {
console.error("Root element not found.");
}
6 changes: 3 additions & 3 deletions src/services/auth/asyncStorage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const saveToken = async (token: string, refreshToken: string) => {

// Function to retrieve the token
export const getToken = async (): Promise<{
token: string | unknown;
token: string;
refreshToken: string | null;
} | null> => {
try {
Expand Down Expand Up @@ -47,9 +47,9 @@ interface JwtPayload {

export const getTokenData = async (): Promise<JwtPayload | null> => {
const tokenResponse = await getToken();
if (tokenResponse && tokenResponse.token) {
if (tokenResponse?.token) {
try {
const decoded = jwtDecode<JwtPayload>(tokenResponse.token as string); // Type assertion here
const decoded = jwtDecode<JwtPayload>(tokenResponse.token);
return decoded;
} catch (e) {
console.error("Error decoding token:", e);
Expand Down
21 changes: 10 additions & 11 deletions src/services/auth/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ export const loginUser = async (loginData: object) => {
export const logoutUser = async () => {
const accessToken = localStorage.getItem("authToken");
const refreshToken = localStorage.getItem("refreshToken");
if (!accessToken || !refreshToken) {
throw new Error("No active session found");
}
try {
const response = await axios.post(
`${apiBaseUrl}/auth/logout`,
Expand All @@ -112,18 +115,14 @@ export const logoutUser = async () => {
},
}
);
localStorage.removeItem("authToken");
localStorage.removeItem("refreshToken");

return response.data;
} catch (error: unknown) {
if (axios.isAxiosError(error) && error.response) {
// Handle the error with specific type if it's an Axios error
return Promise.reject(error.response.data);
} else {
// For other types of errors (like network errors)
return Promise.reject(new Error("Network Error"));
if (response) {
localStorage.removeItem("authToken");
localStorage.removeItem("refreshToken");
}

return response.data as { success: boolean; message: string };
} catch (error) {
handleError(error);
}
};

Expand Down
40 changes: 23 additions & 17 deletions src/utils/context/checkToken.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import React, { createContext, useState, useEffect, ReactNode } from "react";
import React, {
createContext,
useState,
useEffect,
ReactNode,
useMemo,
} from "react";
import { getToken } from "../../services/auth/asyncStorage";

// Define types for the context
Expand Down Expand Up @@ -58,7 +64,6 @@ const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
const [userData, setUserData] = useState<UserData | null>(null);
const [documents, setDocuments] = useState<string[]>([]);

// Function to check token and update login status
const checkToken = async () => {
try {
const token = await getToken();
Expand Down Expand Up @@ -88,22 +93,23 @@ const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
checkToken();
}, []);

const contextValue = useMemo(
() => ({
isLoggedIn,
checkToken,
setIsLoggedIn,
userData,
documents,
updateUserData,
updateApplicationId,
applicationId,
removeContextData,
}),
[isLoggedIn, userData, documents, applicationId] // Dependencies
);

return (
<AuthContext.Provider
value={{
isLoggedIn,
checkToken,
setIsLoggedIn,
userData,
documents,
updateUserData,
updateApplicationId,
applicationId,
removeContextData,
}}
>
{children}
</AuthContext.Provider>
<AuthContext.Provider value={contextValue}>{children}</AuthContext.Provider>
);
};

Expand Down

0 comments on commit 93e6bd1

Please sign in to comment.