Allow setting credentials directly via the Vuex store.

Until now, credentials were set indirectly by reading the browser's
cookie store. This change allows us to receive credentials via other
mechanisms, notably WebSockets.
This commit is contained in:
D. Berge
2024-05-01 10:13:14 +02:00
parent a9270157ea
commit 7bd2319cd7

View File

@@ -11,7 +11,7 @@ async function login ({commit, dispatch}, loginRequest) {
}
const res = await dispatch('api', [url, init]);
if (res && res.ok) {
await dispatch('setCredentials', true);
await dispatch('setCredentials', {force: true});
await dispatch('loadUserPreferences');
}
}
@@ -34,12 +34,12 @@ function cookieChanged (cookie) {
return browserCookie != cookie;
}
function setCredentials ({state, commit, getters, dispatch}, force = false) {
if (cookieChanged(state.cookie) || force) {
function setCredentials ({state, commit, getters, dispatch}, {force, token} = {}) {
if (token || force || cookieChanged(state.cookie)) {
try {
const cookie = browserCookie();
const decoded = cookie ? jwt_decode(cookie.split("=")[1]) : null;
commit('setCookie', cookie);
const decoded = (token ?? cookie) ? jwt_decode(token ?? cookie.split("=")[1]) : null;
commit('setCookie', (cookie ?? (token && ("JWT="+token))) || undefined);
commit('setUser', decoded);
} catch (err) {
if (err.name == "InvalidTokenError") {