Skip to content

Commit 0c87244

Browse files
committed
Updated: Secure localStorage with cryptojs
1 parent 8e7472a commit 0c87244

File tree

6 files changed

+36
-10
lines changed

6 files changed

+36
-10
lines changed

frontend/package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
},
1111
"dependencies": {
1212
"axios": "^1.3.5",
13+
"crypto-js": "^4.1.1",
1314
"lodash": "^4.17.21",
1415
"react": "^18.2.0",
1516
"react-dom": "^18.2.0",

frontend/src/components/Footer.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default function Footer() {
88
By
99
<a href="https://github.com/fhmiibrhimdev"> Fahmi Ibrahim</a>
1010
</div>
11-
<div className="footer-right">0.1.7</div>
11+
<div className="footer-right">0.1.8</div>
1212
</footer>
1313
);
1414
}

frontend/src/components/Navigation.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,18 @@ export default function Navigation() {
4242
handleClickOutsideDropdown
4343
);
4444
};
45-
}, []);
45+
}, [navigate]);
4646

4747
const fetchData = async () => {
4848
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
49-
await axios.get(`${appConfig.baseURL}/user`).then((response) => {
49+
await axios.get(`${appConfig.baseurlAPI}/user`).then((response) => {
5050
setUser(response.data);
5151
});
5252
};
5353

5454
const logoutHandler = async () => {
5555
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
56-
await axios.post(`${appConfig.baseURL}/logout`).then(() => {
56+
await axios.post(`${appConfig.baseurlAPI}/logout`).then(() => {
5757
localStorage.removeItem("token");
5858
navigate("/");
5959
});

frontend/src/config/appConfig.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const appConfig = {
22
debounceTimeout: 750,
3-
expirationTime: 60 * 60 * 1000,
3+
expirationTime: 60 * 60 * 1000, // set 1 hours
44
baseURL: "http://127.0.0.1:8000",
55
baseurlAPI: "http://127.0.0.1:8000/api",
66
};

frontend/src/pages/Auth/Session.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,43 @@
11
import appConfig from "../../config/appConfig";
2+
import CryptoJS from "crypto-js";
23

34
export function setTokenWithExpiration(key, token) {
45
const now = new Date();
56
const expirationTime = now.getTime() + appConfig.expirationTime;
6-
const item = token + "|" + expirationTime;
7+
8+
const keyStr = "sangat-aman-12345";
9+
const iv = CryptoJS.lib.WordArray.random(16);
10+
const encryptedToken = CryptoJS.AES.encrypt(token, keyStr, {
11+
iv: iv,
12+
}).toString();
13+
14+
const item = encryptedToken + "|" + expirationTime + "|" + iv.toString();
715
localStorage.setItem(key, item);
816
}
917

1018
export function getTokenWithExpiration(key) {
1119
const item = localStorage.getItem(key);
20+
1221
if (!item) {
1322
return null;
1423
}
15-
const [token, expirationTime] = item.split("|");
16-
const now = new Date();
17-
if (now.getTime() > expirationTime) {
24+
25+
const parts = item.split("|");
26+
const encryptedToken = parts[0];
27+
const expirationTime = parts[1];
28+
const iv = parts[2];
29+
30+
const keyStr = "sangat-aman-12345";
31+
const decryptedToken = CryptoJS.AES.decrypt(encryptedToken, keyStr, {
32+
iv: CryptoJS.enc.Hex.parse(iv),
33+
}).toString(CryptoJS.enc.Utf8);
34+
35+
if (parseInt(expirationTime) < new Date().getTime()) {
1836
localStorage.removeItem(key);
1937
return null;
2038
}
21-
return token;
39+
40+
return decryptedToken;
2241
}
2342

2443
export function getTokenExpirationTime(key) {

0 commit comments

Comments
 (0)