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.
199 lines
6.6 KiB
199 lines
6.6 KiB
2 years ago
|
import type { Api } from 'nocodb-sdk'
|
||
2 years ago
|
import type { Actions } from '~/composables/useGlobal/types'
|
||
1 year ago
|
import { defineNuxtRouteMiddleware, message, navigateTo, useApi, useGlobal, useRoles } from '#imports'
|
||
|
import { extractSdkResponseErrorMsg } from '~/utils'
|
||
2 years ago
|
|
||
2 years ago
|
/**
|
||
|
* Global auth middleware
|
||
|
*
|
||
|
* On page transitions, this middleware checks if the target route requires authentication.
|
||
|
* If the user is not signed in, the user is redirected to the sign in page.
|
||
|
* If the user is signed in and attempts to access a route that does not require authentication (i.e. signin/signup pages),
|
||
|
* the user is redirected to the home page.
|
||
|
*
|
||
|
* By default, we assume that auth is required
|
||
2 years ago
|
* If not required, mark the page as requiresAuth: false
|
||
|
*
|
||
|
* @example
|
||
2 years ago
|
* ```
|
||
|
* definePageMeta({
|
||
|
* requiresAuth: false
|
||
|
* })
|
||
|
* ```
|
||
|
*
|
||
|
* If auth should be circumvented completely mark the page as public
|
||
2 years ago
|
*
|
||
|
* @example
|
||
|
* ```
|
||
|
* definePageMeta({
|
||
2 years ago
|
* public: true
|
||
|
* })
|
||
2 years ago
|
* ```
|
||
|
*/
|
||
2 years ago
|
export default defineNuxtRouteMiddleware(async (to, from) => {
|
||
2 years ago
|
const state = useGlobal()
|
||
2 years ago
|
|
||
2 years ago
|
const { api } = useApi({ useGlobalInstance: true })
|
||
2 years ago
|
|
||
1 year ago
|
const { allRoles, loadRoles } = useRoles()
|
||
2 years ago
|
|
||
1 year ago
|
/** If baseHostname defined block home page access under subdomains, and redirect to workspace page */
|
||
|
if (
|
||
|
state.appInfo.value.baseHostName &&
|
||
|
!location.hostname?.startsWith(`${state.appInfo.value.mainSubDomain}.`) &&
|
||
|
to.path === '/'
|
||
|
) {
|
||
|
return navigateTo(`/${location.hostname.split('.')[0]}`)
|
||
|
}
|
||
|
|
||
2 years ago
|
/** if user isn't signed in and google auth is enabled, try to check if sign-in data is present */
|
||
1 year ago
|
if (!state.signedIn.value && state.appInfo.value.googleAuthEnabled) {
|
||
|
await tryGoogleAuth(api, state.signIn)
|
||
|
}
|
||
2 years ago
|
|
||
10 months ago
|
/** if not signedIn try token population based on short-lived-token */
|
||
10 months ago
|
if (!state.signedIn.value) await tryShortTokenAuth(api, state.signIn)
|
||
10 months ago
|
|
||
2 years ago
|
/** if public allow all visitors */
|
||
2 years ago
|
if (to.meta.public) return
|
||
|
|
||
2 years ago
|
/** if shared base allow without validating */
|
||
1 year ago
|
if (to.params.typeOrId === 'base') return
|
||
2 years ago
|
|
||
1 year ago
|
/** if auth is required or unspecified (same `as required) and user is not signed in, redirect to signin page */
|
||
2 years ago
|
if ((to.meta.requiresAuth || typeof to.meta.requiresAuth === 'undefined') && !state.signedIn.value) {
|
||
2 years ago
|
/** If this is the first usern navigate to signup page directly */
|
||
|
if (state.appInfo.value.firstUser) {
|
||
1 year ago
|
const query = to.fullPath !== '/' && to.fullPath.match(/^\/(?!\?)/) ? { continueAfterSignIn: to.fullPath } : {}
|
||
|
if (query.continueAfterSignIn) {
|
||
|
localStorage.setItem('continueAfterSignIn', query.continueAfterSignIn)
|
||
|
}
|
||
|
|
||
1 year ago
|
return navigateTo({
|
||
|
path: '/signup',
|
||
1 year ago
|
query,
|
||
1 year ago
|
})
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
/** try generating access token using refresh token */
|
||
|
await state.refreshToken()
|
||
|
|
||
|
/** if user is still not signed in, redirect to signin page */
|
||
1 year ago
|
if (!state.signedIn.value) {
|
||
|
return navigateTo({
|
||
|
path: '/signin',
|
||
|
query: to.fullPath !== '/' && to.fullPath.match(/^\/(?!\?)/) ? { continueAfterSignIn: to.fullPath } : {},
|
||
|
})
|
||
|
}
|
||
2 years ago
|
} else if (to.meta.requiresAuth === false && state.signedIn.value) {
|
||
1 year ago
|
if (to.query?.logout) {
|
||
|
await state.signOut(true)
|
||
|
return navigateTo('/signin')
|
||
|
}
|
||
|
|
||
2 years ago
|
/**
|
||
|
* if user was turned away from non-auth page but also came from a non-auth page (e.g. user went to /signin and reloaded the page)
|
||
|
* redirect to home page
|
||
|
*
|
||
|
* else redirect back to the page they were coming from
|
||
|
*/
|
||
2 years ago
|
if (from.meta.requiresAuth === false) {
|
||
|
return navigateTo('/')
|
||
|
} else {
|
||
|
return navigateTo(from.path)
|
||
|
}
|
||
2 years ago
|
} else {
|
||
2 years ago
|
/** If page is limited to certain users verify the user have the roles */
|
||
1 year ago
|
if (to.meta.allowedRoles && to.meta.allowedRoles.every((role) => !allRoles.value?.[role])) {
|
||
2 years ago
|
message.error("You don't have enough permission to access the page.")
|
||
|
return navigateTo('/')
|
||
|
}
|
||
|
|
||
1 year ago
|
/** if users are accessing the bases without having enough permissions, redirect to My Projects page */
|
||
|
if (to.params.baseId && from.params.baseId !== to.params.baseId) {
|
||
1 year ago
|
await loadRoles()
|
||
2 years ago
|
|
||
1 year ago
|
if (state.user.value?.roles?.guest) {
|
||
1 year ago
|
message.error("You don't have enough permission to access the base.")
|
||
2 years ago
|
|
||
2 years ago
|
return navigateTo('/')
|
||
|
}
|
||
|
}
|
||
2 years ago
|
}
|
||
|
})
|
||
2 years ago
|
|
||
2 years ago
|
/**
|
||
|
* If present, try using google auth data to sign user in before navigating to the next page
|
||
|
*/
|
||
2 years ago
|
async function tryGoogleAuth(api: Api<any>, signIn: Actions['signIn']) {
|
||
2 years ago
|
if (window.location.search && /\bscope=|\bstate=/.test(window.location.search) && /\bcode=/.test(window.location.search)) {
|
||
1 year ago
|
let extraProps: any = {}
|
||
2 years ago
|
try {
|
||
1 year ago
|
let authProvider = 'google'
|
||
|
if (window.location.search.includes('state=github')) {
|
||
|
authProvider = 'github'
|
||
|
} else if (window.location.search.includes('state=oidc')) {
|
||
|
authProvider = 'oidc'
|
||
|
}
|
||
1 year ago
|
|
||
1 year ago
|
// `extra` prop is used in our cloud implementation, so we are keeping it
|
||
2 years ago
|
const {
|
||
1 year ago
|
data: { token, extra },
|
||
1 year ago
|
} = await api.instance.post(`/auth/${authProvider}/genTokenByCode${window.location.search}`)
|
||
2 years ago
|
|
||
1 year ago
|
// if extra prop is null/undefined set it as an empty object as fallback
|
||
1 year ago
|
extraProps = extra || {}
|
||
1 year ago
|
|
||
2 years ago
|
signIn(token)
|
||
2 years ago
|
} catch (e: any) {
|
||
|
message.error(await extractSdkResponseErrorMsg(e))
|
||
2 years ago
|
}
|
||
|
|
||
|
const newURL = window.location.href.split('?')[0]
|
||
1 year ago
|
window.history.pushState(
|
||
|
'object',
|
||
|
document.title,
|
||
1 year ago
|
`${extraProps?.continueAfterSignIn ? `${newURL}#/?continueAfterSignIn=${extraProps.continueAfterSignIn}` : newURL}`,
|
||
1 year ago
|
)
|
||
|
window.location.reload()
|
||
2 years ago
|
}
|
||
|
}
|
||
10 months ago
|
|
||
|
/**
|
||
10 months ago
|
* If short-token present, try using it to generate long-living token before navigating to the next page
|
||
10 months ago
|
*/
|
||
|
async function tryShortTokenAuth(api: Api<any>, signIn: Actions['signIn']) {
|
||
|
if (window.location.search && /\bshort-token=/.test(window.location.search)) {
|
||
|
let extraProps: any = {}
|
||
|
try {
|
||
|
// `extra` prop is used in our cloud implementation, so we are keeping it
|
||
10 months ago
|
const { data } = await api.instance.post(
|
||
10 months ago
|
`/auth/long-lived-token`,
|
||
10 months ago
|
{},
|
||
|
{
|
||
|
headers: {
|
||
10 months ago
|
'xc-short-token': window.location.search.split('=')[1],
|
||
10 months ago
|
} as any,
|
||
|
},
|
||
|
)
|
||
|
|
||
10 months ago
|
const { token, extra } = data
|
||
|
|
||
10 months ago
|
// if extra prop is null/undefined set it as an empty object as fallback
|
||
|
extraProps = extra || {}
|
||
|
|
||
|
signIn(token)
|
||
|
} catch (e: any) {
|
||
|
message.error(await extractSdkResponseErrorMsg(e))
|
||
|
}
|
||
|
|
||
|
const newURL = window.location.href.split('?')[0]
|
||
|
window.history.pushState(
|
||
|
'object',
|
||
|
document.title,
|
||
|
`${extraProps?.continueAfterSignIn ? `${newURL}#/?continueAfterSignIn=${extraProps.continueAfterSignIn}` : newURL}`,
|
||
|
)
|
||
|
window.location.reload()
|
||
|
}
|
||
|
}
|