From 8664c4f88ce033790c1ea9f7644e4fa0486069f2 Mon Sep 17 00:00:00 2001 From: kolaente Date: Sun, 25 Jul 2021 11:49:15 +0200 Subject: [PATCH] Fix token in storage not getting renewed --- src/helpers/{auth.js => auth.ts} | 13 ++++++------- src/store/modules/auth.js | 30 +++++++++++++++--------------- 2 files changed, 21 insertions(+), 22 deletions(-) rename src/helpers/{auth.js => auth.ts} (80%) diff --git a/src/helpers/auth.js b/src/helpers/auth.ts similarity index 80% rename from src/helpers/auth.js rename to src/helpers/auth.ts index beffc20c..53444ff1 100644 --- a/src/helpers/auth.js +++ b/src/helpers/auth.ts @@ -1,7 +1,7 @@ import {HTTPFactory} from '@/http-common' +import {AxiosResponse} from 'axios' -let savedToken = null -let persisted = false +let savedToken: string | null = null /** * Saves a token while optionally saving it to lacal storage. This is used when viewing a link share: @@ -9,10 +9,9 @@ let persisted = false * @param token * @param persist */ -export const saveToken = (token, persist = true) => { +export const saveToken = (token: string, persist: boolean) => { savedToken = token if (persist) { - persisted = true localStorage.setItem('token', token) } } @@ -21,7 +20,7 @@ export const saveToken = (token, persist = true) => { * Returns a saved token. If there is one saved in memory it will use that before anything else. * @returns {string|null} */ -export const getToken = () => { +export const getToken = (): string | null => { if (savedToken !== null) { return savedToken } @@ -42,7 +41,7 @@ export const removeToken = () => { * Refreshes an auth token while ensuring it is updated everywhere. * @returns {Promise>} */ -export const refreshToken = () => { +export const refreshToken = (persist: boolean): Promise => { const HTTP = HTTPFactory() return HTTP.post('user/token', null, { headers: { @@ -50,7 +49,7 @@ export const refreshToken = () => { }, }) .then(r => { - saveToken(r.data.token, persisted) + saveToken(r.data.token, persist) return Promise.resolve(r) }) .catch(e => { diff --git a/src/store/modules/auth.js b/src/store/modules/auth.js index 8ea06418..767ee2b1 100644 --- a/src/store/modules/auth.js +++ b/src/store/modules/auth.js @@ -1,5 +1,5 @@ -import { HTTPFactory } from '@/http-common' -import { ERROR_MESSAGE, LOADING } from '../mutation-types' +import {HTTPFactory} from '@/http-common' +import {ERROR_MESSAGE, LOADING} from '../mutation-types' import UserModel from '../../models/user' import {getToken, refreshToken, removeToken, saveToken} from '@/helpers/auth' @@ -30,6 +30,8 @@ export default { if (info.settings) { state.settings = defaultSettings(info.settings) } + + state.isLinkShareAuth = info.id < 0 } }, setUserSettings(state, settings) { @@ -58,7 +60,7 @@ export default { // Logs a user in with a set of credentials. login(ctx, credentials) { const HTTP = HTTPFactory() - ctx.commit(LOADING, true, { root: true }) + ctx.commit(LOADING, true, {root: true}) // Delete an eventually preexisting old token removeToken() @@ -75,10 +77,9 @@ export default { return HTTP.post('login', data) .then(response => { // Save the token to local storage for later use - saveToken(response.data.token) + saveToken(response.data.token, true) // Tell others the user is autheticated - ctx.commit('isLinkShareAuth', false) ctx.dispatch('checkAuth') return Promise.resolve() }) @@ -93,7 +94,7 @@ export default { return Promise.reject(e) }) .finally(() => { - ctx.commit(LOADING, false, { root: true }) + ctx.commit(LOADING, false, {root: true}) }) }, // Registers a new user and logs them in. @@ -110,18 +111,18 @@ export default { }) .catch(e => { if (e.response && e.response.data && e.response.data.message) { - ctx.commit(ERROR_MESSAGE, e.response.data.message, { root: true }) + ctx.commit(ERROR_MESSAGE, e.response.data.message, {root: true}) } return Promise.reject(e) }) .finally(() => { - ctx.commit(LOADING, false, { root: true }) + ctx.commit(LOADING, false, {root: true}) }) }, - openIdAuth(ctx, { provider, code }) { + openIdAuth(ctx, {provider, code}) { const HTTP = HTTPFactory() - ctx.commit(LOADING, true, { root: true }) + ctx.commit(LOADING, true, {root: true}) const data = { code: code, @@ -132,10 +133,9 @@ export default { return HTTP.post(`/auth/openid/${provider}/callback`, data) .then(response => { // Save the token to local storage for later use - saveToken(response.data.token) + saveToken(response.data.token, true) // Tell others the user is autheticated - ctx.commit('isLinkShareAuth', false) ctx.dispatch('checkAuth') return Promise.resolve() }) @@ -143,10 +143,10 @@ export default { return Promise.reject(e) }) .finally(() => { - ctx.commit(LOADING, false, { root: true }) + ctx.commit(LOADING, false, {root: true}) }) }, - linkShareAuth(ctx, { hash, password }) { + linkShareAuth(ctx, {hash, password}) { const HTTP = HTTPFactory() return HTTP.post('/shares/' + hash + '/auth', { password: password, @@ -222,7 +222,7 @@ export default { return } - refreshToken() + refreshToken(!ctx.state.isLinkShareAuth) .then(() => { ctx.dispatch('checkAuth') })