🏡 index : ~doyle/chartered.git

author Jordan Doyle <jordan@doyle.la> 2022-09-17 14:29:22.0 +01:00:00
committer Jordan Doyle <jordan@doyle.la> 2022-09-17 14:29:22.0 +01:00:00
commit
004a1c07ed7f8030d59ff6d5cca6f2df34230ff9 [patch]
tree
1c2d3ad81a77e2f08e8b70d91d7e629273239770
parent
bc61804d5b2e58ff064ffeeaee6224efd1278001
download
004a1c07ed7f8030d59ff6d5cca6f2df34230ff9.tar.gz

Automatically logout if a GET endpoint returns that a session is expired



Diff

 chartered-frontend/src/stores/auth.ts | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)

diff --git a/chartered-frontend/src/stores/auth.ts b/chartered-frontend/src/stores/auth.ts
index 77e6295..9bddc61 100644
--- a/chartered-frontend/src/stores/auth.ts
+++ a/chartered-frontend/src/stores/auth.ts
@@ -1,6 +1,5 @@
import { get, writable } from 'svelte/store';
import { goto } from '$app/navigation';
import { getErrorMessage } from '../util';

/**
 * The base URL of the chartered-web instance
@@ -79,14 +78,7 @@
        currentAuth.expires = Date.parse(json.expires);
        auth.set(currentAuth);
    } catch (e) {
        // if the user's token is invalid for whatever reason after trying to refresh it,
        // we should log them out. otherwise, we don't really know how to handle the error,
        // and we should just print the error out to the console
        if (getErrorMessage(e) === 'Expired auth token') {
            auth.set(null);
        } else {
            console.error('Failed to extend user session', e);
        }
        console.error('Failed to extend user session', e);
    }
}

@@ -200,7 +192,10 @@
    const json: T & Error = await result.json();

    // TODO: handle 404s
    if (json.error) {
    if (json.error === 'Expired auth token') {
        auth.set(null);
        throw new Error(json.error);
    } else if (json.error) {
        throw new Error(json.error);
    }