Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Auth session should be persisted after refresh #185

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions features/support/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def sign_in(username, password)
end

def sign_out
execute_script("localStorage.removeItem('hyperglosae-basic-auth');")
refresh
end

Expand Down
6 changes: 4 additions & 2 deletions frontend/src/components/Menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -37,7 +39,7 @@ function Authentication({backend}) {
.then(() => setCredentials(credentials));
};

if (credentials) return (
if (credentials.name) return (
<Navbar.Text>
{credentials.name}
</Navbar.Text>
Expand Down
37 changes: 32 additions & 5 deletions frontend/src/hyperglosae.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
});
};

Expand All @@ -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})
Expand All @@ -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);
}
Expand Down
Loading