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"> <script setup lang="ts">
import { onMounted } from '@vue/runtime-core' import { onMounted } from '@vue/runtime-core'
import type { RuleObject } from 'ant-design-vue/es/form' import type { RuleObject } from 'ant-design-vue/es/form'
import type { Router } from 'vue-router'
import { import {
definePageMeta, definePageMeta,
iconMap, iconMap,
@ -10,11 +11,10 @@ import {
useApi, useApi,
useGlobal, useGlobal,
useI18n, useI18n,
useRouter,
useSidebar, useSidebar,
validateEmail, validateEmail,
useRouter,
} from '#imports' } from '#imports'
import { Router } from 'vue-router'
definePageMeta({ definePageMeta({
requiresAuth: false, requiresAuth: false,
@ -81,10 +81,10 @@ async function signIn() {
api.auth.signin(form).then(async ({ token }) => { api.auth.signin(form).then(async ({ token }) => {
_signIn(token!) _signIn(token!)
const redirectTo = sessionStorage.getItem('redirectTo') ?? '/'
const redirectTo = sessionStorage.getItem('redirectTo')
sessionStorage.removeItem('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!) signIn(token!)
const redirectTo = sessionStorage.getItem('redirectTo') const redirectTo = sessionStorage.getItem('redirectTo') ?? '/'
sessionStorage.removeItem('redirectTo') if(/^https?:\/\//.test(redirectTo)) location.href = redirectTo
await navigateTo(redirectTo ?? '/') else
await navigateTo(redirectTo)
$e('a:auth:sign-up') $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 getSwaggerHtml from './template/swaggerHtml';
import getRedocHtml from './template/redocHtml'; import getRedocHtml from './template/redocHtml';
const dashboardUrl = (process.env.NC_PUBLIC_URL || '') + (process.env.NC_DASHBOARD_URL || '/dashboard');
@Controller() @Controller()
export class ApiDocsController { export class ApiDocsController {
constructor(private readonly apiDocsService: ApiDocsService) {} constructor(private readonly apiDocsService: ApiDocsService) {}
@ -33,11 +35,11 @@ export class ApiDocsController {
@Get('/api/v1/db/meta/projects/:projectId/swagger') @Get('/api/v1/db/meta/projects/:projectId/swagger')
swaggerHtml(@Param('projectId') projectId: string, @Response() res) { 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') @Get('/api/v1/db/meta/projects/:projectId/redoc')
redocHtml(@Param('projectId') projectId: string, @Response() res) { 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 ({ export default ({
ncSiteUrl, ncSiteUrl,
dashboardUrl,
}: { }: {
ncSiteUrl: string; ncSiteUrl: string;
dashboardUrl: string;
}): string => `<!DOCTYPE html> }): string => `<!DOCTYPE html>
<html> <html>
<head> <head>
@ -39,6 +41,12 @@ export default ({
xhttp.setRequestHeader("xc-auth", initialLocalStorage && initialLocalStorage.token); xhttp.setRequestHeader("xc-auth", initialLocalStorage && initialLocalStorage.token);
xhttp.onload = function () { xhttp.onload = function () {
if (xmlhttp.status == 401) {
location.href = '${dashboardUrl}#/signIn?redirectTo=' + location.href;
return;
}
const swaggerJson = this.responseText; const swaggerJson = this.responseText;
const swagger = JSON.parse(swaggerJson); const swagger = JSON.parse(swaggerJson);
Redoc.init(swagger, { Redoc.init(swagger, {

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

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

Loading…
Cancel
Save