Browse Source

Merge pull request #9294 from nocodb/nc-fix/login-redirect-bug

Nc fix/login redirect bug
pull/9267/head
Pranav C 3 months ago committed by GitHub
parent
commit
75cab324e9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 1
      packages/nc-gui/middleware/02.auth.global.ts
  2. 14
      packages/nc-gui/pages/index.vue
  3. 48
      packages/nc-gui/plugins/redirect.ts

1
packages/nc-gui/middleware/02.auth.global.ts

@ -80,6 +80,7 @@ export default defineNuxtRouteMiddleware(async (to, from) => {
/** if user is still not signed in, redirect to signin page */
if (!state.signedIn.value) {
localStorage.setItem('continueAfterSignIn', to.fullPath)
return navigateTo({
path: '/signin',
query: to.fullPath !== '/' && to.fullPath.match(/^\/(?!\?)/) ? { continueAfterSignIn: to.fullPath } : {},

14
packages/nc-gui/pages/index.vue

@ -91,20 +91,6 @@ watch(
// immediate watch, because if route is changed during page transition
// It will error out nuxt
onMounted(() => {
if (route.value.query?.continueAfterSignIn) {
localStorage.removeItem('continueAfterSignIn')
return navigateTo(route.value.query.continueAfterSignIn as string)
} else {
const continueAfterSignIn = localStorage.getItem('continueAfterSignIn')
localStorage.removeItem('continueAfterSignIn')
if (continueAfterSignIn) {
return navigateTo({
path: continueAfterSignIn,
query: route.value.query,
})
}
}
handleRouteTypeIdChange().then(() => {
if (sharedBaseId.value) {
if (!isUIAllowed('baseDuplicate')) {

48
packages/nc-gui/plugins/redirect.ts

@ -0,0 +1,48 @@
// this plugin is used to redirect user to the page they were trying to access before they were redirected to the login page
export default defineNuxtPlugin(function (nuxtApp) {
const router = useRouter()
const route = router.currentRoute
// watch for continueAfterSignIn query param and store it in localStorage so that it can be used after sign in
watch(
() => route.value.query?.continueAfterSignIn,
(continueAfterSignIn) => {
if (continueAfterSignIn) {
localStorage.setItem('continueAfterSignIn', continueAfterSignIn as string)
}
},
{
immediate: true,
},
)
// put inside app:created hook to ensure global state is available
nuxtApp.hooks.hook('app:created', () => {
const { token } = useGlobal()
watch(
() => token.value ?? (nuxtApp.$state as ReturnType<typeof useGlobal>)?.token?.value,
async (newToken, oldToken) => {
try {
if (newToken && newToken !== oldToken) {
try {
if (route.value.query?.continueAfterSignIn) {
await navigateTo(route.value.query.continueAfterSignIn as string)
} else {
const continueAfterSignIn = localStorage.getItem('continueAfterSignIn')
if (continueAfterSignIn) {
await navigateTo(continueAfterSignIn)
}
}
} finally {
localStorage.removeItem('continueAfterSignIn')
}
}
} catch (e) {
console.error(e)
}
},
{ immediate: true },
)
})
})
Loading…
Cancel
Save