feat: use AuthenticatedHTTPFactory for refreshToken (#1546)

Co-authored-by: Dominik Pschenitschni <mail@celement.de>
Reviewed-on: https://kolaente.dev/vikunja/frontend/pulls/1546
Reviewed-by: konrad <k@knt.li>
Co-authored-by: Dominik Pschenitschni <dpschen@noreply.kolaente.de>
Co-committed-by: Dominik Pschenitschni <dpschen@noreply.kolaente.de>
This commit is contained in:
Dominik Pschenitschni 2022-02-18 19:14:38 +00:00 committed by konrad
parent 026ee3e8da
commit 8df73c973b

View file

@ -1,4 +1,4 @@
import {HTTPFactory} from '@/http-common' import {AuthenticatedHTTPFactory} from '@/http-common'
import {AxiosResponse} from 'axios' import {AxiosResponse} from 'axios'
let savedToken: string | null = null let savedToken: string | null = null
@ -6,8 +6,6 @@ let savedToken: string | null = null
/** /**
* Saves a token while optionally saving it to lacal storage. This is used when viewing a link share: * Saves a token while optionally saving it to lacal storage. This is used when viewing a link share:
* It enables viewing multiple link shares indipendently from each in multiple tabs other without overriding any other open ones. * It enables viewing multiple link shares indipendently from each in multiple tabs other without overriding any other open ones.
* @param token
* @param persist
*/ */
export const saveToken = (token: string, persist: boolean) => { export const saveToken = (token: string, persist: boolean) => {
savedToken = token savedToken = token
@ -18,7 +16,6 @@ export const saveToken = (token: string, persist: boolean) => {
/** /**
* Returns a saved token. If there is one saved in memory it will use that before anything else. * Returns a saved token. If there is one saved in memory it will use that before anything else.
* @returns {string|null}
*/ */
export const getToken = (): string | null => { export const getToken = (): string | null => {
if (savedToken !== null) { if (savedToken !== null) {
@ -39,16 +36,11 @@ export const removeToken = () => {
/** /**
* Refreshes an auth token while ensuring it is updated everywhere. * Refreshes an auth token while ensuring it is updated everywhere.
* @returns {Promise<AxiosResponse<any>>}
*/ */
export async function refreshToken(persist: boolean): Promise<AxiosResponse> { export async function refreshToken(persist: boolean): Promise<AxiosResponse> {
const HTTP = HTTPFactory() const HTTP = AuthenticatedHTTPFactory()
try { try {
const response = await HTTP.post('user/token', null, { const response = await HTTP.post('user/token')
headers: {
Authorization: `Bearer ${getToken()}`,
},
})
saveToken(response.data.token, persist) saveToken(response.data.token, persist)
return response return response