From d1e1e7e1043b08c020fa2ac747a2f8bbe6a16cff Mon Sep 17 00:00:00 2001 From: mbaharip <62494292+mbahArip@users.noreply.github.com> Date: Sun, 7 May 2023 17:58:21 +0700 Subject: [PATCH] Change site password using sha256 hash --- src/config/site.config.js | 8 +++++--- src/pages/_app.tsx | 19 +++++++------------ 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/config/site.config.js b/src/config/site.config.js index 72e535c..74d2826 100644 --- a/src/config/site.config.js +++ b/src/config/site.config.js @@ -10,10 +10,12 @@ const config = { // Fav icon of the site // Also used as the logo of the site on the navbar siteIcon: "/favicon.svg", - // - privateIndex: false, + // Add site page password protection + // If this is set, the site will be protected with the password + // NOTE: The files are still accessible via direct link, but the site is not. + privateIndex: true, indexPassword: - "ac0f794f72d4366a57a2a4122ce04321:8b2f087de80d0d7faa89b5372df51268", + "640e3e38dd31aec254f214ba38541a82ddb615e73ce9c6129f33ef549b154ab9", // Navbar menu links navbarLinks: [ { diff --git a/src/pages/_app.tsx b/src/pages/_app.tsx index 01d9126..8c59a70 100644 --- a/src/pages/_app.tsx +++ b/src/pages/_app.tsx @@ -19,7 +19,7 @@ import { LayoutContext } from "context/layoutContext"; import { ThemeContext } from "context/themeContext"; import fetcher from "utils/swrFetch"; import siteConfig from "config/site.config"; -import { decrypt, encrypt } from "utils/encryptionHelper"; +import { hashToken, verifyHash } from "utils/hashHelper"; const exo2 = Exo_2({ weight: ["300", "400", "600", "700"], @@ -57,14 +57,8 @@ export default function App({ Component, pageProps }: AppProps) { const adminPassword = localStorage.getItem("sitePassword"); if (siteConfig.privateIndex) { - const sitePassword = decrypt(siteConfig.indexPassword); - if (adminPassword) { - const decryptedPassword = decrypt(adminPassword); - if (decryptedPassword === sitePassword) { - setIsUnlocked(true); - } else { - setIsUnlocked(false); - } + if (adminPassword === siteConfig.indexPassword) { + setIsUnlocked(true); } else { setIsUnlocked(false); } @@ -205,15 +199,16 @@ export default function App({ Component, pageProps }: AppProps) {
{ e.preventDefault(); - const sitePassword = decrypt( + const verify = verifyHash( + passwordInput, siteConfig.indexPassword, ); - if (passwordInput === sitePassword) { + if (verify) { toast("Welcome back!"); setIsUnlocked(true); localStorage.setItem( "sitePassword", - encrypt(passwordInput), + hashToken(passwordInput), ); setPasswordInput(""); } else {