Fix token in storage not getting renewed
This commit is contained in:
parent
e2aca53253
commit
8664c4f88c
2 changed files with 21 additions and 22 deletions
|
@ -1,7 +1,7 @@
|
||||||
import {HTTPFactory} from '@/http-common'
|
import {HTTPFactory} from '@/http-common'
|
||||||
|
import {AxiosResponse} from 'axios'
|
||||||
|
|
||||||
let savedToken = null
|
let savedToken: string | null = null
|
||||||
let persisted = false
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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:
|
||||||
|
@ -9,10 +9,9 @@ let persisted = false
|
||||||
* @param token
|
* @param token
|
||||||
* @param persist
|
* @param persist
|
||||||
*/
|
*/
|
||||||
export const saveToken = (token, persist = true) => {
|
export const saveToken = (token: string, persist: boolean) => {
|
||||||
savedToken = token
|
savedToken = token
|
||||||
if (persist) {
|
if (persist) {
|
||||||
persisted = true
|
|
||||||
localStorage.setItem('token', token)
|
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 a saved token. If there is one saved in memory it will use that before anything else.
|
||||||
* @returns {string|null}
|
* @returns {string|null}
|
||||||
*/
|
*/
|
||||||
export const getToken = () => {
|
export const getToken = (): string | null => {
|
||||||
if (savedToken !== null) {
|
if (savedToken !== null) {
|
||||||
return savedToken
|
return savedToken
|
||||||
}
|
}
|
||||||
|
@ -42,7 +41,7 @@ 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>>}
|
* @returns {Promise<AxiosResponse<any>>}
|
||||||
*/
|
*/
|
||||||
export const refreshToken = () => {
|
export const refreshToken = (persist: boolean): Promise<AxiosResponse> => {
|
||||||
const HTTP = HTTPFactory()
|
const HTTP = HTTPFactory()
|
||||||
return HTTP.post('user/token', null, {
|
return HTTP.post('user/token', null, {
|
||||||
headers: {
|
headers: {
|
||||||
|
@ -50,7 +49,7 @@ export const refreshToken = () => {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
.then(r => {
|
.then(r => {
|
||||||
saveToken(r.data.token, persisted)
|
saveToken(r.data.token, persist)
|
||||||
return Promise.resolve(r)
|
return Promise.resolve(r)
|
||||||
})
|
})
|
||||||
.catch(e => {
|
.catch(e => {
|
|
@ -1,5 +1,5 @@
|
||||||
import { HTTPFactory } from '@/http-common'
|
import {HTTPFactory} from '@/http-common'
|
||||||
import { ERROR_MESSAGE, LOADING } from '../mutation-types'
|
import {ERROR_MESSAGE, LOADING} from '../mutation-types'
|
||||||
import UserModel from '../../models/user'
|
import UserModel from '../../models/user'
|
||||||
import {getToken, refreshToken, removeToken, saveToken} from '@/helpers/auth'
|
import {getToken, refreshToken, removeToken, saveToken} from '@/helpers/auth'
|
||||||
|
|
||||||
|
@ -30,6 +30,8 @@ export default {
|
||||||
if (info.settings) {
|
if (info.settings) {
|
||||||
state.settings = defaultSettings(info.settings)
|
state.settings = defaultSettings(info.settings)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
state.isLinkShareAuth = info.id < 0
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
setUserSettings(state, settings) {
|
setUserSettings(state, settings) {
|
||||||
|
@ -58,7 +60,7 @@ export default {
|
||||||
// Logs a user in with a set of credentials.
|
// Logs a user in with a set of credentials.
|
||||||
login(ctx, credentials) {
|
login(ctx, credentials) {
|
||||||
const HTTP = HTTPFactory()
|
const HTTP = HTTPFactory()
|
||||||
ctx.commit(LOADING, true, { root: true })
|
ctx.commit(LOADING, true, {root: true})
|
||||||
|
|
||||||
// Delete an eventually preexisting old token
|
// Delete an eventually preexisting old token
|
||||||
removeToken()
|
removeToken()
|
||||||
|
@ -75,10 +77,9 @@ export default {
|
||||||
return HTTP.post('login', data)
|
return HTTP.post('login', data)
|
||||||
.then(response => {
|
.then(response => {
|
||||||
// Save the token to local storage for later use
|
// Save the token to local storage for later use
|
||||||
saveToken(response.data.token)
|
saveToken(response.data.token, true)
|
||||||
|
|
||||||
// Tell others the user is autheticated
|
// Tell others the user is autheticated
|
||||||
ctx.commit('isLinkShareAuth', false)
|
|
||||||
ctx.dispatch('checkAuth')
|
ctx.dispatch('checkAuth')
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
})
|
})
|
||||||
|
@ -93,7 +94,7 @@ export default {
|
||||||
return Promise.reject(e)
|
return Promise.reject(e)
|
||||||
})
|
})
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
ctx.commit(LOADING, false, { root: true })
|
ctx.commit(LOADING, false, {root: true})
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
// Registers a new user and logs them in.
|
// Registers a new user and logs them in.
|
||||||
|
@ -110,18 +111,18 @@ export default {
|
||||||
})
|
})
|
||||||
.catch(e => {
|
.catch(e => {
|
||||||
if (e.response && e.response.data && e.response.data.message) {
|
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)
|
return Promise.reject(e)
|
||||||
})
|
})
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
ctx.commit(LOADING, false, { root: true })
|
ctx.commit(LOADING, false, {root: true})
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
openIdAuth(ctx, { provider, code }) {
|
openIdAuth(ctx, {provider, code}) {
|
||||||
const HTTP = HTTPFactory()
|
const HTTP = HTTPFactory()
|
||||||
ctx.commit(LOADING, true, { root: true })
|
ctx.commit(LOADING, true, {root: true})
|
||||||
|
|
||||||
const data = {
|
const data = {
|
||||||
code: code,
|
code: code,
|
||||||
|
@ -132,10 +133,9 @@ export default {
|
||||||
return HTTP.post(`/auth/openid/${provider}/callback`, data)
|
return HTTP.post(`/auth/openid/${provider}/callback`, data)
|
||||||
.then(response => {
|
.then(response => {
|
||||||
// Save the token to local storage for later use
|
// Save the token to local storage for later use
|
||||||
saveToken(response.data.token)
|
saveToken(response.data.token, true)
|
||||||
|
|
||||||
// Tell others the user is autheticated
|
// Tell others the user is autheticated
|
||||||
ctx.commit('isLinkShareAuth', false)
|
|
||||||
ctx.dispatch('checkAuth')
|
ctx.dispatch('checkAuth')
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
})
|
})
|
||||||
|
@ -143,10 +143,10 @@ export default {
|
||||||
return Promise.reject(e)
|
return Promise.reject(e)
|
||||||
})
|
})
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
ctx.commit(LOADING, false, { root: true })
|
ctx.commit(LOADING, false, {root: true})
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
linkShareAuth(ctx, { hash, password }) {
|
linkShareAuth(ctx, {hash, password}) {
|
||||||
const HTTP = HTTPFactory()
|
const HTTP = HTTPFactory()
|
||||||
return HTTP.post('/shares/' + hash + '/auth', {
|
return HTTP.post('/shares/' + hash + '/auth', {
|
||||||
password: password,
|
password: password,
|
||||||
|
@ -222,7 +222,7 @@ export default {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
refreshToken()
|
refreshToken(!ctx.state.isLinkShareAuth)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
ctx.dispatch('checkAuth')
|
ctx.dispatch('checkAuth')
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Reference in a new issue