Skip to content

Commit ddd06d1

Browse files
refactor(web): code-smells
1 parent ba2442a commit ddd06d1

File tree

4 files changed

+34
-29
lines changed

4 files changed

+34
-29
lines changed

web/src/context/AtlasProvider.tsx

+22-24
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ interface IAtlasProvider {
1818

1919
const Context = createContext<IAtlasProvider | undefined>(undefined);
2020

21-
// eslint-disable-next-line
22-
// @ts-ignore
23-
const atlasUri = import.meta.env.REACT_APP_ATLAS_URI ?? "";
21+
const atlasUri: string = import.meta.env.REACT_APP_ATLAS_URI ?? "";
22+
if (!atlasUri) {
23+
console.warn("REACT_APP_ATLAS_URI is not defined. Please check your environment variables.");
24+
}
2425

2526
const AtlasProvider: React.FC<{ children?: React.ReactNode }> = ({ children }) => {
2627
const { address } = useAccount();
@@ -75,27 +76,24 @@ const AtlasProvider: React.FC<{ children?: React.ReactNode }> = ({ children }) =
7576
/**
7677
* @description authorise user and enable authorised calls
7778
*/
78-
const authoriseUser = useCallback(() => {
79-
if (!address) return;
80-
setIsSigningIn(true);
81-
getNonce(atlasGqlClient, address)
82-
.then((nonce) => {
83-
const message = createMessage(address, chainId, nonce);
84-
signMessageAsync({ message }).then((signature) => {
85-
if (!isUndefined(signature)) {
86-
loginUser(atlasGqlClient, { signature, message })
87-
.then((token) => {
88-
setAuthToken(token);
89-
})
90-
.finally(() => setIsSigningIn(false));
91-
}
92-
});
93-
})
94-
.catch((err) => {
95-
// eslint-disable-next-line no-console
96-
console.log(`authorise user error : ${err?.message}`);
97-
setIsSigningIn(false);
98-
});
79+
const authoriseUser = useCallback(async () => {
80+
try {
81+
if (!address || !chainId) return;
82+
setIsSigningIn(true);
83+
const nonce = await getNonce(atlasGqlClient, address);
84+
const message = createMessage(address, nonce, chainId);
85+
const signature = await signMessageAsync({ message });
86+
87+
if (!signature) return;
88+
const token = await loginUser(atlasGqlClient, { message, signature });
89+
90+
setAuthToken(token);
91+
} catch (err: any) {
92+
// eslint-disable-next-line
93+
console.log("Authorize User Error : ", err?.message);
94+
} finally {
95+
setIsSigningIn(false);
96+
}
9997
}, [address, chainId, setAuthToken, signMessageAsync, atlasGqlClient]);
10098

10199
return (

web/src/utils/atlas/createMessage.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createSiweMessage } from "viem/siwe";
22

33
import { DEFAULT_CHAIN } from "consts/chains";
44

5-
export const createMessage = (address: `0x${string}`, chainId: number = DEFAULT_CHAIN, nonce: string) => {
5+
export const createMessage = (address: `0x${string}`, nonce: string, chainId: number = DEFAULT_CHAIN) => {
66
const domain = window.location.host;
77
const origin = window.location.origin;
88

web/src/utils/atlas/getNonce.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ export function getNonce(client: GraphQLClient, address: string): Promise<string
2424
.then((response) => response.nonce)
2525
.catch((errors) => {
2626
// eslint-disable-next-line no-console
27-
console.log("get nonce error : ", { errors });
28-
throw Error(`${errors?.response?.errors?.[0]?.message}`);
27+
console.log("Error fetching nonce for address:", address, { errors });
28+
const errorMessage = Array.isArray(errors?.response?.errors)
29+
? errors.response.errors[0]?.message
30+
: "Error fetching nonce";
31+
throw Error(errorMessage);
2932
}),
3033
{
3134
error: {

web/src/utils/atlas/loginUser.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ export function loginUser(client: GraphQLClient, authData: AuthoriseUserData): P
3232
.then(async (response) => response.login.accessToken)
3333
.catch((errors) => {
3434
// eslint-disable-next-line no-console
35-
console.log("authorise user error : ", { errors });
36-
throw Error(`${errors?.response?.errors?.[0]?.message}`);
35+
console.log("Authorization error:", { errors });
36+
37+
const errorMessage = Array.isArray(errors?.response?.errors)
38+
? errors.response.errors[0]?.message
39+
: "Unknown error";
40+
throw new Error(errorMessage);
3741
}),
3842
{
3943
pending: `Signing in User...`,

0 commit comments

Comments
 (0)