1
0
mirror of https://gitlab.com/bramw/baserow.git synced 2024-11-21 23:37:55 +00:00
bramw_baserow/web-frontend/modules/core/middleware/authentication.js
2023-01-09 13:21:55 +00:00

26 lines
751 B
JavaScript

import {
getTokenIfEnoughTimeLeft,
setToken,
} from '@baserow/modules/core/utils/auth'
export default function ({ store, req, app, route, redirect }) {
// If nuxt generate or already authenticated, pass this middleware
if ((process.server && !req) || store.getters['auth/isAuthenticated']) return
// token can be in the query string (SSO) or in the cookies (previous session)
let refreshToken = route.query.token
if (refreshToken) {
setToken(app, refreshToken)
} else {
refreshToken = getTokenIfEnoughTimeLeft(app)
}
if (refreshToken) {
return store.dispatch('auth/refresh', refreshToken).catch((error) => {
if (error.response?.status === 401) {
return redirect({ name: 'login' })
}
})
}
}