Skip to content

Commit 96c3cdf

Browse files
committed
chore: fix snyk errors
1 parent 77772de commit 96c3cdf

4 files changed

Lines changed: 19 additions & 15 deletions

File tree

src/features/auth/application/auth-store.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { createContext, useContext } from "react";
22
import { createStore, useStore } from "zustand";
33

4-
import { AUTH_KEY } from "@/features/auth/models/storage-keys";
4+
import { IS_AUTHENTICATED_STORAGE } from "@/features/auth/models/storage-keys";
55
import type { User } from "@/features/auth/models/user";
66
import { getUser } from "@/features/auth/providers/get-user";
77
import { AUTH_TOKEN_KEY } from "@/lib/http/ky-client";
88

99
import { loginUser, type ICredentials } from "../providers/login-user";
1010

1111
// could be also https://www.npmjs.com/package/zustand-persist lib for advanced use cases
12-
const isLoggedIn = () => localStorage.getItem(AUTH_KEY) === "true";
12+
const isLoggedIn = () =>
13+
localStorage.getItem(IS_AUTHENTICATED_STORAGE) === "true";
1314

1415
interface IStore {
1516
isAuthenticated: boolean;
@@ -71,15 +72,15 @@ export const initializeAuthStore = (preloadedState: Partial<IStore> = {}) => {
7172
localStorage.setItem(AUTH_TOKEN_KEY, token);
7273
const user = await getUser();
7374

74-
localStorage.setItem(AUTH_KEY, "true");
75+
localStorage.setItem(IS_AUTHENTICATED_STORAGE, "true");
7576

7677
set({
7778
isAuthenticated: true,
7879
state: "finished",
7980
user,
8081
});
8182
} catch (e) {
82-
localStorage.setItem(AUTH_KEY, "false");
83+
localStorage.setItem(IS_AUTHENTICATED_STORAGE, "false");
8384

8485
set({
8586
isAuthenticated: false,
@@ -96,7 +97,7 @@ export const initializeAuthStore = (preloadedState: Partial<IStore> = {}) => {
9697
});
9798

9899
return new Promise((resolve) => setTimeout(resolve, 500)).then(() => {
99-
localStorage.setItem(AUTH_KEY, "false");
100+
localStorage.setItem(IS_AUTHENTICATED_STORAGE, "false");
100101
localStorage.removeItem(AUTH_TOKEN_KEY);
101102
set({
102103
isAuthenticated: false,
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
// deepcode ignore HardcodedNonCryptoSecret: localStorage key name, not a secret value
2-
export const AUTH_KEY = "fake_store_is_authenticated";
1+
export const IS_AUTHENTICATED_STORAGE = "fake_store_is_authenticated";

src/features/authv2/application/AuthProvider.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { PropsWithChildren } from "react";
44
import { useEffect } from "react";
55
import { fromPromise } from "xstate";
66

7-
import { AUTH_KEY } from "@/features/auth/models/storage-keys";
7+
import { IS_AUTHENTICATED_STORAGE } from "@/features/auth/models/storage-keys";
88
import { getUser } from "@/features/auth/providers/get-user";
99
import { loginUser } from "@/features/auth/providers/login-user";
1010
import { AuthContext } from "@/features/authv2/application/auth-context";
@@ -18,12 +18,14 @@ import { sleep } from "@/lib/sleep";
1818

1919
export const AuthProvider = ({ children }: PropsWithChildren) => {
2020
const checkAuthStatus = () => {
21-
return Promise.resolve(localStorage.getItem(AUTH_KEY) === "true");
21+
return Promise.resolve(
22+
localStorage.getItem(IS_AUTHENTICATED_STORAGE) === "true"
23+
);
2224
};
2325

2426
const logout = async () => {
2527
await sleep(500);
26-
localStorage.setItem(AUTH_KEY, "false");
28+
localStorage.setItem(IS_AUTHENTICATED_STORAGE, "false");
2729
};
2830

2931
const authActor = useActorRef(
@@ -33,7 +35,7 @@ export const AuthProvider = ({ children }: PropsWithChildren) => {
3335
getUser: fromPromise(getUser),
3436
loginUser: fromPromise(async ({ input }) => {
3537
await loginUser(input);
36-
localStorage.setItem(AUTH_KEY, "true");
38+
localStorage.setItem(IS_AUTHENTICATED_STORAGE, "true");
3739
}),
3840
getRoles: fromPromise(getRoles),
3941
logout: fromPromise(logout),

src/features/authv2/application/storage-machine.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { assign, fromPromise, setup } from "xstate";
22

3-
import { AUTH_KEY } from "@/features/auth/models/storage-keys";
3+
import { IS_AUTHENTICATED_STORAGE } from "@/features/auth/models/storage-keys";
44

55
interface StorageMachineContext {
66
isAuthenticated: boolean;
@@ -13,7 +13,9 @@ type StorageMachineEvents =
1313
export type StorageMachineType = typeof storageMachine;
1414

1515
const checkAuthStatus = fromPromise(() => {
16-
return Promise.resolve(localStorage.getItem(AUTH_KEY) === "true");
16+
return Promise.resolve(
17+
localStorage.getItem(IS_AUTHENTICATED_STORAGE) === "true"
18+
);
1719
});
1820

1921
export const storageMachine = setup({
@@ -27,11 +29,11 @@ export const storageMachine = setup({
2729
actions: {
2830
setAuthStorage: ({ event }) => {
2931
if (event.type === "SET_AUTHENTICATED") {
30-
localStorage.setItem(AUTH_KEY, String(event.value));
32+
localStorage.setItem(IS_AUTHENTICATED_STORAGE, String(event.value));
3133
}
3234
},
3335
clearAuthStorage: () => {
34-
localStorage.setItem(AUTH_KEY, "false");
36+
localStorage.setItem(IS_AUTHENTICATED_STORAGE, "false");
3537
},
3638
},
3739
}).createMachine({

0 commit comments

Comments
 (0)