Browse Source

feat: redirect to login screen if token missing ( swagger ui )

Signed-off-by: Pranav C <pranavxc@gmail.com>
feat/after-login-navigate-to-req-page
Pranav C 2 years ago
parent
commit
9676640ee4
  1. 10
      packages/nc-gui/pages/signin.vue
  2. 7
      packages/nc-gui/pages/signup/[[token]].vue
  3. 6
      packages/nocodb/src/controllers/api-docs/api-docs.controller.ts
  4. 8
      packages/nocodb/src/controllers/api-docs/template/redocHtml.ts
  5. 9
      packages/nocodb/src/controllers/api-docs/template/swaggerHtml.ts

10
packages/nc-gui/pages/signin.vue

@ -1,6 +1,7 @@
<script setup lang="ts">
import { onMounted } from '@vue/runtime-core'
import type { RuleObject } from 'ant-design-vue/es/form'
import type { Router } from 'vue-router'
import {
definePageMeta,
iconMap,
@ -10,11 +11,10 @@ import {
useApi,
useGlobal,
useI18n,
useRouter,
useSidebar,
validateEmail,
useRouter,
} from '#imports'
import { Router } from 'vue-router'
definePageMeta({
requiresAuth: false,
@ -81,10 +81,10 @@ async function signIn() {
api.auth.signin(form).then(async ({ token }) => {
_signIn(token!)
const redirectTo = sessionStorage.getItem('redirectTo')
const redirectTo = sessionStorage.getItem('redirectTo') ?? '/'
sessionStorage.removeItem('redirectTo')
await navigateTo(redirectTo ?? '/')
if (/^https?:\/\//.test(redirectTo)) location.href = redirectTo
else await navigateTo(redirectTo)
})
}

7
packages/nc-gui/pages/signup/[[token]].vue

@ -90,9 +90,10 @@ async function signUp() {
signIn(token!)
const redirectTo = sessionStorage.getItem('redirectTo')
sessionStorage.removeItem('redirectTo')
await navigateTo(redirectTo ?? '/')
const redirectTo = sessionStorage.getItem('redirectTo') ?? '/'
if(/^https?:\/\//.test(redirectTo)) location.href = redirectTo
else
await navigateTo(redirectTo)
$e('a:auth:sign-up')
})

6
packages/nocodb/src/controllers/api-docs/api-docs.controller.ts

@ -15,6 +15,8 @@ import { ApiDocsService } from '../../services/api-docs/api-docs.service';
import getSwaggerHtml from './template/swaggerHtml';
import getRedocHtml from './template/redocHtml';
const dashboardUrl = (process.env.NC_PUBLIC_URL || '') + (process.env.NC_DASHBOARD_URL || '/dashboard');
@Controller()
export class ApiDocsController {
constructor(private readonly apiDocsService: ApiDocsService) {}
@ -33,11 +35,11 @@ export class ApiDocsController {
@Get('/api/v1/db/meta/projects/:projectId/swagger')
swaggerHtml(@Param('projectId') projectId: string, @Response() res) {
res.send(getSwaggerHtml({ ncSiteUrl: process.env.NC_PUBLIC_URL || '' }));
res.send(getSwaggerHtml({ ncSiteUrl: process.env.NC_PUBLIC_URL || '',dashboardUrl }));
}
@Get('/api/v1/db/meta/projects/:projectId/redoc')
redocHtml(@Param('projectId') projectId: string, @Response() res) {
res.send(getRedocHtml({ ncSiteUrl: process.env.NC_PUBLIC_URL || '' }));
res.send(getRedocHtml({ ncSiteUrl: process.env.NC_PUBLIC_URL || '' , dashboardUrl }));
}
}

8
packages/nocodb/src/controllers/api-docs/template/redocHtml.ts vendored

@ -1,7 +1,9 @@
export default ({
ncSiteUrl,
dashboardUrl,
}: {
ncSiteUrl: string;
dashboardUrl: string;
}): string => `<!DOCTYPE html>
<html>
<head>
@ -39,6 +41,12 @@ export default ({
xhttp.setRequestHeader("xc-auth", initialLocalStorage && initialLocalStorage.token);
xhttp.onload = function () {
if (xmlhttp.status == 401) {
location.href = '${dashboardUrl}#/signIn?redirectTo=' + location.href;
return;
}
const swaggerJson = this.responseText;
const swagger = JSON.parse(swaggerJson);
Redoc.init(swagger, {

9
packages/nocodb/src/controllers/api-docs/template/swaggerHtml.ts vendored

@ -1,7 +1,9 @@
export default ({
ncSiteUrl,
dashboardUrl
}: {
ncSiteUrl: string;
dashboardUrl: string;
}): string => `<!DOCTYPE html>
<html>
<head>
@ -28,6 +30,12 @@ xmlhttp.open("GET", "./swagger.json");
xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xmlhttp.setRequestHeader("xc-auth", initialLocalStorage && initialLocalStorage.token);
xmlhttp.onload = function () {
if (xmlhttp.status == 401) {
location.href = '${dashboardUrl}#/signIn?redirectTo=' + location.href;
return;
}
const ui = SwaggerUIBundle({
// url: ,
@ -39,6 +47,7 @@ xmlhttp.onload = function () {
],
})
}
xmlhttp.send();

Loading…
Cancel
Save