Files
baserow/web-frontend/modules/core/store/auth.js
T

181 lines
5.1 KiB
JavaScript

import jwtDecode from 'jwt-decode'
import AuthService from '@baserow/modules/core/services/auth'
import { setToken, unsetToken } from '@baserow/modules/core/utils/auth'
import { unsetGroupCookie } from '@baserow/modules/core/utils/group'
export const state = () => ({
refreshing: false,
token: null,
user: null,
webSocketId: null,
})
export const mutations = {
SET_USER_DATA(state, { token, user }) {
state.token = token
state.token_data = jwtDecode(token)
state.user = user
},
CLEAR_USER_DATA(state) {
state.token = null
state.user = null
},
SET_REFRESHING(state, refreshing) {
state.refreshing = refreshing
},
SET_WEB_SOCKET_ID(state, id) {
state.webSocketId = id
},
}
export const actions = {
/**
* Authenticate a user by his email and password. If successful commit the
* token to the state and start the refresh timeout to stay authenticated.
*/
async login({ commit, dispatch }, { email, password }) {
const { data } = await AuthService(this.$client).login(email, password)
setToken(data.token, this.app)
commit('SET_USER_DATA', data)
dispatch('startRefreshTimeout')
},
/**
* Register a new user and immediately authenticate. If successful commit the
* token to the state and start the refresh timeout to stay authenticated.
*/
async register(
{ commit, dispatch },
{
email,
name,
password,
language,
groupInvitationToken = null,
templateId = null,
}
) {
const { data } = await AuthService(this.$client).register(
email,
name,
password,
language,
true,
groupInvitationToken,
templateId
)
setToken(data.token, this.app)
commit('SET_USER_DATA', data)
dispatch('startRefreshTimeout')
},
/**
* Logs off the user by removing the token as a cookie and clearing the user
* data.
*/
async logoff({ commit, dispatch }) {
unsetToken(this.app)
unsetGroupCookie(this.app)
commit('CLEAR_USER_DATA')
await dispatch('group/clearAll', {}, { root: true })
await dispatch('group/unselect', {}, { root: true })
},
/**
* Refresh the existing token. If successful commit the new token and start a
* new refresh timeout. If unsuccessful the existing cookie and user data is
* cleared.
*/
async refresh({ commit, state, dispatch }, token) {
try {
const { data } = await AuthService(this.$client).refresh(token)
setToken(data.token, this.app)
commit('SET_USER_DATA', data)
dispatch('startRefreshTimeout')
} catch (error) {
// If the server can't be reached because of a network error we want to
// fail hard so that the correct error message is shown.
if (error.response === undefined) {
throw error
}
// The token could not be refreshed, this means the token is no longer
// valid and the user not logged in anymore.
unsetToken(this.app)
commit('CLEAR_USER_DATA')
// @TODO we might want to do something here, trigger some event, show
// show the user a login popup or redirect to the login page.
}
},
/**
* Because the token expires within a configurable time, we need to keep
* refreshing the token before that happens. This process may only happen in
* the browser because that is where we measure if the user still has the
* application open.
*/
startRefreshTimeout({ getters, commit, dispatch }) {
if (!process.browser) return
clearTimeout(this.refreshTimeout)
commit('SET_REFRESHING', true)
// The token expires within a given time. When 80% of that time has expired we want
// to fetch a new token.
this.refreshTimeout = setTimeout(() => {
dispatch('refresh', getters.token)
commit('SET_REFRESHING', false)
}, Math.floor((getters.tokenExpireSeconds / 100) * 80) * 1000)
},
/**
* The web socket id is generated by the backend when connecting to the real time
* updates web socket. This id will be added to each AJAX request so the backend
* knows not to send any real time changes to the sender.
*/
setWebSocketId({ commit }, webSocketId) {
commit('SET_WEB_SOCKET_ID', webSocketId)
},
}
export const getters = {
isAuthenticated(state) {
return !!state.user
},
isRefreshing(state) {
return state.refreshing
},
token(state) {
return state.token
},
webSocketId(state) {
return state.webSocketId
},
getName(state) {
return state.user ? state.user.first_name : ''
},
getUsername(state) {
return state.user ? state.user.username : ''
},
getUserId(state) {
return state.user ? state.user.id : null
},
isStaff(state) {
return state.user ? state.user.is_staff : false
},
/**
* Returns the amount of seconds it will take before the tokes expires.
* @TODO figure out what happens if the browser and server time are not in
* sync.
*/
tokenExpireSeconds(state) {
const now = Math.ceil(new Date().getTime() / 1000)
return state.token_data.exp - now
},
}
export default {
namespaced: true,
state,
getters,
actions,
mutations,
}