Skip to content

Commit

Permalink
Merge pull request #5 from City-of-Turku/feature/update-start-poll
Browse files Browse the repository at this point in the history
Feature/update start poll
  • Loading branch information
juhomakkonen authored Mar 21, 2024
2 parents 1e29083 + e8187c0 commit 4ea25ac
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@mui/material": "^5.13.0",
"@reduxjs/toolkit": "^1.9.5",
"bootstrap": "^4.6.0",
"crypto-js": "^4.2.0",
"dotenv": "^16.3.1",
"react": "^18.2.0",
"react-bootstrap": "^2.8.0",
Expand All @@ -44,6 +45,7 @@
"@babel/preset-typescript": "^7.21.5",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^14.0.0",
"@types/crypto-js": "^4.2.2",
"@types/jest": "^29.5.1",
"@types/react": "^18.2.6",
"@types/react-dom": "^18.2.4",
Expand Down
20 changes: 19 additions & 1 deletion src/components/Pages/HomePage/HomePage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { bindActionCreators } from '@reduxjs/toolkit';
import CryptoJS from 'crypto-js';
import React, { useEffect } from 'react';
import { Button } from 'react-bootstrap';
import { useIntl } from 'react-intl';
Expand All @@ -25,6 +26,8 @@ const HomePage = () => {

const intl = useIntl();

const secret = process.env.REACT_APP_DATA_SECRET ? process.env.REACT_APP_DATA_SECRET : '';

useEffect(() => {
fetchQuestions(setAllQuestions, setQuestionApiError);
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down Expand Up @@ -56,10 +59,25 @@ const HomePage = () => {
}
}, [firstQuestion, setFirstQuestion]);

const decryptString = (
cipher_text: string,
key: string | CryptoJS.lib.WordArray,
iv: CryptoJS.lib.WordArray,
) => {
const bytes = CryptoJS.AES.decrypt(cipher_text, key, { iv: iv, mode: CryptoJS.mode.CBC });
return bytes.toString(CryptoJS.enc.Utf8);
};

const handleClick = async () => {
const userValues = await startPoll(setIsLoggedIn);
const dataStr = userValues?.data[0];
const iv = userValues?.data[1];
const secretParse = window.atob(secret);
const key = CryptoJS.enc.Utf8.parse(secretParse);
const ivParsed = CryptoJS.enc.Base64.parse(iv);
const decrypted = decryptString(dataStr, key, ivParsed);
setUserId(userValues?.id);
setCsrfToken(userValues?.token);
setCsrfToken(decrypted);
};

return (
Expand Down

0 comments on commit 4ea25ac

Please sign in to comment.