From 07d5950a175c572d0c323da1562f3c1482601600 Mon Sep 17 00:00:00 2001 From: louisduhalberruer Date: Wed, 19 Jun 2024 14:29:44 +0200 Subject: [PATCH] FIX: Auth session should be persisted after refresh (closes #163) --- frontend/src/components/Menu.js | 6 ++++-- frontend/src/hyperglosae.js | 37 ++++++++++++++++++++++++++++----- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/frontend/src/components/Menu.js b/frontend/src/components/Menu.js index a2f0da73..5c41680e 100644 --- a/frontend/src/components/Menu.js +++ b/frontend/src/components/Menu.js @@ -28,7 +28,9 @@ function Menu({backend}) { } function Authentication({backend}) { - const [credentials, setCredentials] = useState(); + const [credentials, setCredentials] = useState({ + ...backend.credentials + }); let handleSubmit = (e) => { e.preventDefault(); @@ -37,7 +39,7 @@ function Authentication({backend}) { .then(() => setCredentials(credentials)); }; - if (credentials) return ( + if (credentials.name) return ( {credentials.name} diff --git a/frontend/src/hyperglosae.js b/frontend/src/hyperglosae.js index 4f8ed66b..dc0b564d 100644 --- a/frontend/src/hyperglosae.js +++ b/frontend/src/hyperglosae.js @@ -2,10 +2,30 @@ import {Buffer} from 'buffer'; const service = 'http://localhost:5984/hyperglosae'; -function Hyperglosae(logger) { +const LOCALSTORAGE_BASIC_AUTH_KEY = 'hyperglosae-basic-auth'; +function Hyperglosae(logger) { this.credentials = {}; + let persistBasicAuth = () => { + if (!this.credentials) return; + localStorage.setItem(LOCALSTORAGE_BASIC_AUTH_KEY, JSON.stringify(this.credentials)); + }; + + let retrieveBasicAuth = () => { + if (localStorage.getItem(LOCALSTORAGE_BASIC_AUTH_KEY)) { + try { + const basicAuth = JSON.parse(localStorage.getItem(LOCALSTORAGE_BASIC_AUTH_KEY)); + this.credentials = {...this.credentials, ...basicAuth}; + } catch { + console.error('Unable to parse basic auth'); + localStorage.removeItem(LOCALSTORAGE_BASIC_AUTH_KEY); + } + } + }; + + retrieveBasicAuth(); + this.getView = ({view, id, options = []}) => fetch(`${ service @@ -24,10 +44,11 @@ function Hyperglosae(logger) { .then(x => x.json()); let basicAuthentication = ({force}) => { - let {name, password} = this.credentials; - if (!force && !name && !password) return ({}); + retrieveBasicAuth(); + let { base64} = this.credentials; + if (!force && !base64) return ({}); return ({ - 'Authorization': 'Basic ' + Buffer.from(`${name}:${password}`).toString('base64') + 'Authorization': 'Basic ' + base64 }); }; @@ -46,7 +67,12 @@ function Hyperglosae(logger) { }); this.authenticate = ({name, password}) => { - this.credentials = {name, password}; + this.credentials = { + base64: Buffer.from(`${name}:${password}`).toString('base64'), + name + }; + persistBasicAuth(); + return fetch(`${service}`, { method: 'GET', headers: basicAuthentication({force: true}) @@ -55,6 +81,7 @@ function Hyperglosae(logger) { .then(x => { if (x.reason) { this.credentials = {}; + localStorage.removeItem(LOCALSTORAGE_BASIC_AUTH_KEY); logger(x.reason); throw new Error(x.reason); }