mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.4 KiB
52 lines
1.4 KiB
2 years ago
|
import { message } from 'ant-design-vue'
|
||
2 years ago
|
import { Api } from 'nocodb-sdk'
|
||
2 years ago
|
import type { Actions, State } from './types'
|
||
2 years ago
|
import { getI18n } from '~/plugins/a.i18n'
|
||
2 years ago
|
|
||
2 years ago
|
export function useGlobalActions(state: State): Actions {
|
||
2 years ago
|
/** detached api instance, will not trigger global loading */
|
||
|
const api = new Api()
|
||
2 years ago
|
|
||
2 years ago
|
const { t } = getI18n().global
|
||
|
|
||
2 years ago
|
/** Sign out by deleting the token from localStorage */
|
||
|
const signOut: Actions['signOut'] = () => {
|
||
|
state.token.value = null
|
||
|
state.user.value = null
|
||
|
}
|
||
|
|
||
|
/** Sign in by setting the token in localStorage */
|
||
|
const signIn: Actions['signIn'] = async (newToken) => {
|
||
|
state.token.value = newToken
|
||
|
|
||
2 years ago
|
if (state.jwtPayload.value) {
|
||
2 years ago
|
state.user.value = {
|
||
2 years ago
|
id: state.jwtPayload.value.id,
|
||
|
email: state.jwtPayload.value.email,
|
||
|
firstname: state.jwtPayload.value.firstname,
|
||
|
lastname: state.jwtPayload.value.lastname,
|
||
|
roles: state.jwtPayload.value.roles,
|
||
2 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/** manually try to refresh token */
|
||
|
const refreshToken = async () => {
|
||
2 years ago
|
api.instance
|
||
2 years ago
|
.post('/auth/refresh-token', null, {
|
||
|
withCredentials: true,
|
||
|
})
|
||
|
.then((response) => {
|
||
|
if (response.data?.token) {
|
||
|
signIn(response.data.token)
|
||
|
}
|
||
|
})
|
||
|
.catch((err) => {
|
||
2 years ago
|
message.error(err.message || t('msg.error.youHaveBeenSignedOut'))
|
||
2 years ago
|
signOut()
|
||
|
})
|
||
|
}
|
||
|
|
||
|
return { signIn, signOut, refreshToken }
|
||
|
}
|