From 5ed804b417286d3827a3c5a61db20ac076d41aa8 Mon Sep 17 00:00:00 2001 From: mertmit Date: Tue, 3 Oct 2023 20:26:42 +0530 Subject: [PATCH] feat: continue after signin --- .../components/general/ShareProject.vue | 30 +++++++++++-- .../nc-gui/composables/useApi/interceptors.ts | 2 + packages/nc-gui/middleware/auth.global.ts | 25 +++++++++-- packages/nc-gui/pages/forgot-password.vue | 13 +++++- packages/nc-gui/pages/signin.vue | 27 ++++++++++-- packages/nc-gui/pages/signup/[[token]].vue | 14 ++++++- .../nocodb/src/modules/jobs/jobs.module.ts | 42 +++++++++---------- .../export-import/duplicate.controller.ts | 4 +- 8 files changed, 119 insertions(+), 38 deletions(-) diff --git a/packages/nc-gui/components/general/ShareProject.vue b/packages/nc-gui/components/general/ShareProject.vue index 6bdc068be3..6831024e7c 100644 --- a/packages/nc-gui/components/general/ShareProject.vue +++ b/packages/nc-gui/components/general/ShareProject.vue @@ -1,5 +1,5 @@ diff --git a/packages/nc-gui/composables/useApi/interceptors.ts b/packages/nc-gui/composables/useApi/interceptors.ts index a130f0fb69..e8f26da9be 100644 --- a/packages/nc-gui/composables/useApi/interceptors.ts +++ b/packages/nc-gui/composables/useApi/interceptors.ts @@ -23,8 +23,10 @@ export function addAxiosInterceptors(api: Api) { if (!config.url?.endsWith('/user/me') && !config.url?.endsWith('/admin/roles')) { if (route.value && route.value.params && route.value.params.typeOrId === 'base') { config.headers['xc-shared-base-id'] = route.value.params.baseId + delete config.headers['xc-auth'] } else if (route.value && route.value.params && route.value.params.typeOrId === 'ERD') { config.headers['xc-shared-erd-id'] = route.value.params.erdUuid + delete config.headers['xc-auth'] } } diff --git a/packages/nc-gui/middleware/auth.global.ts b/packages/nc-gui/middleware/auth.global.ts index 745169b8d7..5c6ca2b9d1 100644 --- a/packages/nc-gui/middleware/auth.global.ts +++ b/packages/nc-gui/middleware/auth.global.ts @@ -58,14 +58,22 @@ export default defineNuxtRouteMiddleware(async (to, from) => { if ((to.meta.requiresAuth || typeof to.meta.requiresAuth === 'undefined') && !state.signedIn.value) { /** If this is the first usern navigate to signup page directly */ if (state.appInfo.value.firstUser) { - return navigateTo('/signup') + return navigateTo({ + path: '/signup', + query: to.fullPath !== '/' && to.fullPath.match(/^\/(?!\?)/) ? { continueAfterSignIn: to.fullPath } : {}, + }) } /** try generating access token using refresh token */ await state.refreshToken() /** if user is still not signed in, redirect to signin page */ - if (!state.signedIn.value) return navigateTo('/signin') + if (!state.signedIn.value) { + return navigateTo({ + path: '/signin', + query: to.fullPath !== '/' && to.fullPath.match(/^\/(?!\?)/) ? { continueAfterSignIn: to.fullPath } : {}, + }) + } } else if (to.meta.requiresAuth === false && state.signedIn.value) { if (to.query?.logout) { await state.signOut(true) @@ -108,6 +116,7 @@ export default defineNuxtRouteMiddleware(async (to, from) => { */ async function tryGoogleAuth(api: Api, signIn: Actions['signIn']) { if (window.location.search && /\bscope=|\bstate=/.test(window.location.search) && /\bcode=/.test(window.location.search)) { + let extraProps: any = {} try { let authProvider = 'google' if (window.location.search.includes('state=github')) { @@ -115,16 +124,24 @@ async function tryGoogleAuth(api: Api, signIn: Actions['signIn']) { } else if (window.location.search.includes('state=oidc')) { authProvider = 'oidc' } + const { - data: { token }, + data: { token, extra }, } = await api.instance.post(`/auth/${authProvider}/genTokenByCode${window.location.search}`) + 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, newURL) + window.history.pushState( + 'object', + document.title, + `${extraProps.continueAfterSignIn ? `${newURL}#/?continueAfterSignIn=${extraProps.continueAfterSignIn}` : newURL}`, + ) + window.location.reload() } } diff --git a/packages/nc-gui/pages/forgot-password.vue b/packages/nc-gui/pages/forgot-password.vue index 3bf38416a2..1d65786aab 100644 --- a/packages/nc-gui/pages/forgot-password.vue +++ b/packages/nc-gui/pages/forgot-password.vue @@ -6,6 +6,8 @@ definePageMeta({ requiresAuth: false, }) +const route = useRoute() + const { api, isLoading, error } = useApi({ useGlobalInstance: true }) const { t } = useI18n() @@ -48,6 +50,13 @@ async function resetPassword() { function resetError() { if (error.value) error.value = null } + +function navigateSignIn() { + navigateTo({ + path: '/signin', + query: route.query, + }) +} @@ -99,7 +108,7 @@ function resetError() {
{{ $t('msg.info.signUp.alreadyHaveAccount') }} - {{ $t('general.signIn') }} + {{ $t('general.signIn') }}
diff --git a/packages/nc-gui/pages/signin.vue b/packages/nc-gui/pages/signin.vue index 33adce9d93..31cc73eb4e 100644 --- a/packages/nc-gui/pages/signin.vue +++ b/packages/nc-gui/pages/signin.vue @@ -18,6 +18,8 @@ definePageMeta({ title: 'title.headLogin', }) +const route = useRoute() + const { signIn: _signIn, appInfo } = useGlobal() const { api, isLoading, error } = useApi({ useGlobalInstance: true }) @@ -63,13 +65,30 @@ async function signIn() { api.auth.signin(form).then(async ({ token }) => { _signIn(token!) - await navigateTo('/') + await navigateTo({ + path: '/', + query: route.query, + }) }) } function resetError() { if (error.value) error.value = null } + +function navigateSignUp() { + navigateTo({ + path: '/signup', + query: route.query, + }) +} + +function navigateForgotPassword() { + navigateTo({ + path: '/forgot-password', + query: route.query, + }) +}