@postxl/generators 1.28.0 → 1.30.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/backend-authentication/authentication.generator.js +5 -15
- package/dist/backend-authentication/authentication.generator.js.map +1 -1
- package/dist/backend-authentication/generators/authentication-module.generator.js +3 -4
- package/dist/backend-authentication/generators/authentication-module.generator.js.map +1 -1
- package/dist/backend-authentication/generators/authentication-service.generator.js +53 -242
- package/dist/backend-authentication/generators/authentication-service.generator.js.map +1 -1
- package/dist/backend-authentication/generators/authentication-types.generator.js +1 -13
- package/dist/backend-authentication/generators/authentication-types.generator.js.map +1 -1
- package/dist/backend-authentication/template/README.md +18 -23
- package/dist/backend-authentication/template/src/authentication.config.ts +0 -15
- package/dist/backend-authentication/template/src/authentication.mock.service.ts +0 -56
- package/dist/backend-core/generators/api-config.generator.js +4 -2
- package/dist/backend-core/generators/api-config.generator.js.map +1 -1
- package/dist/backend-core/generators/dotenv-example.generator.js +3 -0
- package/dist/backend-core/generators/dotenv-example.generator.js.map +1 -1
- package/dist/backend-core/generators/main.generator.js +10 -12
- package/dist/backend-core/generators/main.generator.js.map +1 -1
- package/dist/backend-core/modules/backend-module-xlport.generator.js +1 -1
- package/dist/backend-core/modules/backend-module-xlport.generator.js.map +1 -1
- package/dist/backend-core/template/apps/api/src/console.logger.ts +56 -0
- package/dist/backend-core/template/apps/api/src/e2e.ts +32 -23
- package/dist/backend-repositories/generators/model-repository.generator.js +20 -14
- package/dist/backend-repositories/generators/model-repository.generator.js.map +1 -1
- package/dist/backend-router-trpc/template/viewer.router.ts +6 -11
- package/dist/backend-update/model-update-service.generator.js +32 -16
- package/dist/backend-update/model-update-service.generator.js.map +1 -1
- package/dist/devops/devops.generator.d.ts +0 -1
- package/dist/devops/devops.generator.js +0 -1
- package/dist/devops/devops.generator.js.map +1 -1
- package/dist/devops/generators/configure-keycloak-sh.generator.js +3 -5
- package/dist/devops/generators/configure-keycloak-sh.generator.js.map +1 -1
- package/dist/devops/generators/docker-compose-yml.generator.js +1 -3
- package/dist/devops/generators/docker-compose-yml.generator.js.map +1 -1
- package/dist/devops/generators/jenkinsfile.generator.js +2 -12
- package/dist/devops/generators/jenkinsfile.generator.js.map +1 -1
- package/dist/frontend-core/template/src/context-providers/auth-context-provider.tsx +1 -1
- package/dist/types/generators/dto-types.generator.js +20 -4
- package/dist/types/generators/dto-types.generator.js.map +1 -1
- package/package.json +4 -4
- package/dist/backend-authentication/template/src/authentication.controller.ts +0 -105
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"configure-keycloak-sh.generator.js","sourceRoot":"","sources":["../../../src/devops/generators/configure-keycloak-sh.generator.ts"],"names":[],"mappings":";;AAEA,
|
|
1
|
+
{"version":3,"file":"configure-keycloak-sh.generator.js","sourceRoot":"","sources":["../../../src/devops/generators/configure-keycloak-sh.generator.ts"],"names":[],"mappings":";;AAEA,kEAgFC;AAhFD,SAAgB,2BAA2B,CAAC,EAAE,QAAQ,EAAiB;IACrE,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,OAAO,EAAE,CAAA;IACX,CAAC;IACD,OAAO,MAAM,CAAC;;;;EAId,QAAQ,CAAC,UAAU,CAAC,QAAQ,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE;;;;;;;;;;;;;;;;;;;;;;;;IAwBhE,QAAQ,CAAC,UAAU,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE;;;;;;;;;;;;;;;;;;;;;;;IAuB9C,QAAQ,CAAC,UAAU,CAAC,QAAQ,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,EAAE;;IAEpE,QAAQ,CAAC,UAAU,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE;;IAEtD,QAAQ,CAAC,UAAU,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE;;;;;;;;;;;;;;;;;;;;CAoB9D,CAAA;AACD,CAAC;AAED,MAAM,aAAa,GAAG;;;;;CAKrB,CAAA;AAED,MAAM,GAAG,GAAG;;;;CAIX,CAAA;AAED,MAAM,cAAc,GAAG;;;;;;;CAOtB,CAAA;AAED,MAAM,OAAO,GAAG;;;;;;;;;;;;;;;;;;;;;CAqBf,CAAA;AAED,MAAM,UAAU,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA6BlB,CAAA"}
|
|
@@ -39,7 +39,7 @@ const dbService = ({ username, password, database }) => `
|
|
|
39
39
|
timeout: 5s
|
|
40
40
|
retries: 5
|
|
41
41
|
`;
|
|
42
|
-
const keycloakService = ({ host, pathPrefix, clientId,
|
|
42
|
+
const keycloakService = ({ host, pathPrefix, clientId, realm }) => `
|
|
43
43
|
keycloak:
|
|
44
44
|
build:
|
|
45
45
|
context: ./
|
|
@@ -59,8 +59,6 @@ const keycloakService = ({ host, pathPrefix, clientId, clientSecret, realm }) =>
|
|
|
59
59
|
KEYCLOAK_ADMIN_PASSWORD: admin
|
|
60
60
|
KEYCLOAK_REALM: ${realm}
|
|
61
61
|
KEYCLOAK_CLIENT_ID: ${clientId}
|
|
62
|
-
KEYCLOAK_REDIRECT_URL: http://localhost:3001/auth/callback
|
|
63
|
-
KEYCLOAK_CLIENT_SECRET: ${clientSecret}
|
|
64
62
|
DEV_USERNAME: developer
|
|
65
63
|
DEV_PASSWORD: root
|
|
66
64
|
# KC_LOG_LEVEL: DEBUG
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"docker-compose-yml.generator.js","sourceRoot":"","sources":["../../../src/devops/generators/docker-compose-yml.generator.ts"],"names":[],"mappings":";;AAEA,8DAcC;AAdD,SAAgB,yBAAyB,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,SAAS,EAAiB;IAC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AC;;;;;;;;;uBASjD,QAAQ;2BACJ,QAAQ;qBACd,QAAQ;;;;;;;2CAOc,QAAQ,OAAO,QAAQ;;;;CAIjE,CAAA;AAED,MAAM,eAAe,GAAG,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,KAAK,EAAkB,EAAE,EAAE,CAAC;;;;;;;;;;;;;;sBAc7D,IAAI,GAAG,UAAU;4BACX,IAAI,GAAG,UAAU;;;wBAGrB,KAAK;4BACD,QAAQ;;;;;;;;;;;;CAYnC,CAAA;AAED,MAAM,aAAa,GAAG;;;;;;;;;;;;CAYrB,CAAA;AAED,MAAM,OAAO,GAAG;;;CAGf,CAAA"}
|
|
@@ -8,11 +8,6 @@ function generateJenkinsfile({ devOps: { database, keycloak, useS3 }, schema: {
|
|
|
8
8
|
? `
|
|
9
9
|
[envVar: 'DB_USERNAME', secretRef: 'op://PostXL service/PSQL Credentials for ${name}/username'],
|
|
10
10
|
[envVar: 'DB_PASSWORD', secretRef: 'op://PostXL service/PSQL Credentials for ${name}/password'],
|
|
11
|
-
`
|
|
12
|
-
: '';
|
|
13
|
-
const keycloakSecrets = keycloak
|
|
14
|
-
? `
|
|
15
|
-
[envVar: 'KEYCLOAK_CLIENT_SECRET', secretRef: 'op://PostXL service/Keycloak Client Secret for ${name}/password'],
|
|
16
11
|
`
|
|
17
12
|
: '';
|
|
18
13
|
const keycloakEnv = keycloak
|
|
@@ -20,8 +15,6 @@ function generateJenkinsfile({ devOps: { database, keycloak, useS3 }, schema: {
|
|
|
20
15
|
KEYCLOAK_URL = "https://auth.postxl.com"
|
|
21
16
|
KEYCLOAK_CLIENT_ID = "${slug}-client"
|
|
22
17
|
KEYCLOAK_REALM = "${slug}"
|
|
23
|
-
KEYCLOAK_REDIRECT_URL = "\${API_URL}/auth/callback"
|
|
24
|
-
KEYCLOAK_LOGOUT_REDIRECT_URL = "\${FRONTEND_URL}"
|
|
25
18
|
`
|
|
26
19
|
: '';
|
|
27
20
|
const dbEnv = database
|
|
@@ -32,10 +25,7 @@ function generateJenkinsfile({ devOps: { database, keycloak, useS3 }, schema: {
|
|
|
32
25
|
? `
|
|
33
26
|
-e KEYCLOAK_URL=\${KEYCLOAK_URL} \\
|
|
34
27
|
-e KEYCLOAK_CLIENT_ID=\${KEYCLOAK_CLIENT_ID} \\
|
|
35
|
-
-e
|
|
36
|
-
-e KEYCLOAK_REALM=\${KEYCLOAK_REALM} \\
|
|
37
|
-
-e KEYCLOAK_REDIRECT_URL=\${KEYCLOAK_REDIRECT_URL} \\
|
|
38
|
-
-e KEYCLOAK_LOGOUT_REDIRECT_URL=\${KEYCLOAK_LOGOUT_REDIRECT_URL} \\`
|
|
28
|
+
-e KEYCLOAK_REALM=\${KEYCLOAK_REALM} \\`
|
|
39
29
|
: '';
|
|
40
30
|
const keycloakRunEnvFrontend = keycloak
|
|
41
31
|
? ` -e VITE_PUBLIC_KEYCLOAK_URL=\${KEYCLOAK_URL} \\
|
|
@@ -79,7 +69,7 @@ function generateJenkinsfile({ devOps: { database, keycloak, useS3 }, schema: {
|
|
|
79
69
|
-e AI_CONVERSATION_CACHE_MAX_SIZE=\${AI_CONVERSATION_CACHE_MAX_SIZE} \\
|
|
80
70
|
-e AI_MAX_INTERNAL_TOOL_STEPS=\${AI_MAX_INTERNAL_TOOL_STEPS} \\`;
|
|
81
71
|
return /*Jenkinsfile*/ `
|
|
82
|
-
def secrets = [${dbSecrets}
|
|
72
|
+
def secrets = [${dbSecrets}
|
|
83
73
|
[envVar: 'TURBO_TOKEN', secretRef: 'op://PostXL service/Turborepo Cache Token/password'],
|
|
84
74
|
]
|
|
85
75
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jenkinsfile.generator.js","sourceRoot":"","sources":["../../../src/devops/generators/jenkinsfile.generator.ts"],"names":[],"mappings":";;AAEA,
|
|
1
|
+
{"version":3,"file":"jenkinsfile.generator.js","sourceRoot":"","sources":["../../../src/devops/generators/jenkinsfile.generator.ts"],"names":[],"m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C,CAAC,YAAY,IAAI,GAAG;;;;;;;;sBAQvD,WAAW,KAAK,YAAY,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,YAAY,IAAI,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;uEA2BN,KAAK,GAAG,cAAc,GAAG,QAAQ;wCAChE,QAAQ;;;;;;+EAM+B,sBAAsB,CAAC,CAAC,CAAC,KAAK,sBAAsB,EAAE,CAAC,CAAC,CAAC,EAAE;;;;;;;;;;CAUzI,CAAA;AACD,CAAC"}
|
|
@@ -83,7 +83,7 @@ export function AuthProvider({ children }: Readonly<{ children: React.ReactNode
|
|
|
83
83
|
isError: isUserDataError,
|
|
84
84
|
} = useQuery(
|
|
85
85
|
trpc.viewer.viewer.queryOptions(
|
|
86
|
-
|
|
86
|
+
undefined,
|
|
87
87
|
// load user data when keycloak init complete and user is authenticated
|
|
88
88
|
{ enabled: loginState === 'loggedIn' },
|
|
89
89
|
),
|
|
@@ -9,13 +9,29 @@ function generateDtoTypes(context) {
|
|
|
9
9
|
|
|
10
10
|
export type ${genericModel}<ID extends ${idType}> = Record<string, unknown> & { id: ID }
|
|
11
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Keys of \`T\` whose value type permits \`null\` (i.e. fields that are nullable).
|
|
14
|
+
*/
|
|
15
|
+
type NullableKeys<T> = { [K in keyof T]-?: null extends T[K] ? K : never }[keyof T]
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Keys of \`T\` whose value type does not permit \`null\` (i.e. required fields).
|
|
19
|
+
*/
|
|
20
|
+
type NonNullableKeys<T> = { [K in keyof T]-?: null extends T[K] ? never : K }[keyof T]
|
|
21
|
+
|
|
12
22
|
/**
|
|
13
23
|
* A generic DTO for creating a new item.
|
|
24
|
+
*
|
|
25
|
+
* Nullable fields are made optional: when not provided, they default to \`null\`.
|
|
26
|
+
* This keeps existing create call sites stable when a new nullable field is added.
|
|
27
|
+
*
|
|
28
|
+
* \`id\`, \`createdAt\`, and \`updatedAt\` are stripped from both the non-nullable
|
|
29
|
+
* and the nullable side so that auto-populated timestamps are never settable via
|
|
30
|
+
* the create payload — even when a model declares them as nullable (\`Date | null\`).
|
|
14
31
|
*/
|
|
15
|
-
export type ${context.dto.create.name}<Model extends ${genericModel}<ID>, ID extends ${idType}> =
|
|
16
|
-
Model,
|
|
17
|
-
'id' | 'createdAt' | 'updatedAt'
|
|
18
|
-
> &
|
|
32
|
+
export type ${context.dto.create.name}<Model extends ${genericModel}<ID>, ID extends ${idType}> =
|
|
33
|
+
Omit<Pick<Model, NonNullableKeys<Model>>, 'id' | 'createdAt' | 'updatedAt'> &
|
|
34
|
+
Partial<Omit<Pick<Model, NullableKeys<Model>>, 'id' | 'createdAt' | 'updatedAt'>> &
|
|
19
35
|
Partial<${genericModel}<ID>>
|
|
20
36
|
|
|
21
37
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dto-types.generator.js","sourceRoot":"","sources":["../../../src/types/generators/dto-types.generator.ts"],"names":[],"mappings":";;AAEA,
|
|
1
|
+
{"version":3,"file":"dto-types.generator.js","sourceRoot":"","sources":["../../../src/types/generators/dto-types.generator.ts"],"names":[],"mappings":";;AAEA,4CA4CC;AA5CD,SAAgB,gBAAgB,CAAC,OAAqB;IACpD,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAA;IACtC,MAAM,YAAY,GAAG,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC,IAAI,CAAA;IAElD,OAAO,QAAQ,CAAC;gBACF,MAAM;;cAER,YAAY,eAAe,MAAM;;;;;;;;;;;;;;;;;;;;;;cAsBjC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,kBAAkB,YAAY,oBAAoB,MAAM;;;YAGjF,YAAY;;;;;cAKV,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,kBAAkB,YAAY,oBAAoB,MAAM;;;;;cAK/E,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,kBAAkB,YAAY,oBAAoB,MAAM;GAC1F,CAAA;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@postxl/generators",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.30.0",
|
|
4
4
|
"description": "Code generators for PXL - generates backend, frontend, Prisma schemas, and more",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -44,10 +44,10 @@
|
|
|
44
44
|
"@faker-js/faker": "9.9.0",
|
|
45
45
|
"@prisma/prisma-schema-wasm": "7.5.0-9.c6be8e68bf8e4a36534064f9323a343f2fcafe21",
|
|
46
46
|
"exceljs": "^4.4.0",
|
|
47
|
-
"@postxl/generator": "^1.4.
|
|
48
|
-
"@postxl/
|
|
47
|
+
"@postxl/generator": "^1.4.1",
|
|
48
|
+
"@postxl/utils": "^1.4.0",
|
|
49
49
|
"@postxl/ui-components": "^1.7.1",
|
|
50
|
-
"@postxl/
|
|
50
|
+
"@postxl/schema": "^1.10.1"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {},
|
|
53
53
|
"wallaby": {
|
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import '@fastify/cookie'
|
|
2
|
-
|
|
3
|
-
import { Controller, Get, HttpException, HttpStatus, Logger, Req, Res } from '@nestjs/common'
|
|
4
|
-
|
|
5
|
-
import type { FastifyReply, FastifyRequest } from 'fastify'
|
|
6
|
-
|
|
7
|
-
import { AuthenticationService } from './authentication.service'
|
|
8
|
-
import { authenticationCookieName } from './authentication.types'
|
|
9
|
-
import { unknownToErrorMessage } from './utils'
|
|
10
|
-
|
|
11
|
-
@Controller('auth')
|
|
12
|
-
export class AuthenticationController {
|
|
13
|
-
private readonly logger = new Logger(AuthenticationController.name)
|
|
14
|
-
private readonly cookieOptions: {
|
|
15
|
-
domain: string
|
|
16
|
-
path: string
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
constructor(private readonly authService: AuthenticationService) {
|
|
20
|
-
this.cookieOptions = {
|
|
21
|
-
domain: this.authService.cookieDomain,
|
|
22
|
-
path: '/',
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Authentication route that logs a user in using an access token and sets the identity cookie.
|
|
28
|
-
*/
|
|
29
|
-
@Get('callback')
|
|
30
|
-
async loginWithToken(
|
|
31
|
-
@Req() request: FastifyRequest,
|
|
32
|
-
@Res({ passthrough: true }) response: FastifyReply,
|
|
33
|
-
): Promise<HttpStatus> {
|
|
34
|
-
try {
|
|
35
|
-
const res = await this.authService.getAccessTokenFromURL(request.url)
|
|
36
|
-
|
|
37
|
-
if (!res.ok) {
|
|
38
|
-
this.logger.error(unknownToErrorMessage(res.error, 'Login failed'))
|
|
39
|
-
return HttpStatus.BAD_REQUEST
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (res.state?.redirectTo) {
|
|
43
|
-
response = response.setCookie(authenticationCookieName, res.accessToken, this.cookieOptions)
|
|
44
|
-
if (res.refreshToken) {
|
|
45
|
-
response = response.setCookie(`${authenticationCookieName}-refresh`, res.refreshToken, this.cookieOptions)
|
|
46
|
-
}
|
|
47
|
-
return response.redirect(res.state.redirectTo, 302)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return response.send({ ok: true, accessToken: res.accessToken })
|
|
51
|
-
} catch (e) {
|
|
52
|
-
this.logger.warn(unknownToErrorMessage(e, 'Error during exchange of token'))
|
|
53
|
-
throw new HttpException('User could not be authenticated', HttpStatus.UNAUTHORIZED)
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Logout route that removes subscriptions and cookies.
|
|
59
|
-
*/
|
|
60
|
-
@Get('logout')
|
|
61
|
-
async logout(
|
|
62
|
-
@Req() request: FastifyRequest,
|
|
63
|
-
@Res({ passthrough: true }) response: FastifyReply,
|
|
64
|
-
): Promise<HttpStatus> {
|
|
65
|
-
try {
|
|
66
|
-
// clear cookies
|
|
67
|
-
response = response.clearCookie(authenticationCookieName, this.cookieOptions)
|
|
68
|
-
response = response.clearCookie(`${authenticationCookieName}-refresh`, this.cookieOptions)
|
|
69
|
-
|
|
70
|
-
const res = await this.authService.getLogoutUrl(request)
|
|
71
|
-
// redirect to Keycloak logout page
|
|
72
|
-
return response.send(res)
|
|
73
|
-
} catch (e) {
|
|
74
|
-
this.logger.warn(unknownToErrorMessage(e, 'Error during retrieving logout url'))
|
|
75
|
-
throw new HttpException('Logout url could not be retrieved', HttpStatus.BAD_REQUEST)
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Refresh route that refreshes the access token and sets the identity cookie.
|
|
81
|
-
*/
|
|
82
|
-
@Get('refresh')
|
|
83
|
-
async refreshAccessToken(
|
|
84
|
-
@Req() request: FastifyRequest,
|
|
85
|
-
@Res({ passthrough: true }) response: FastifyReply,
|
|
86
|
-
): Promise<HttpStatus> {
|
|
87
|
-
try {
|
|
88
|
-
const res = await this.authService.refreshAccessToken(request)
|
|
89
|
-
|
|
90
|
-
if (!res.ok) {
|
|
91
|
-
this.logger.error(unknownToErrorMessage(res.error, 'Refreshing token failed'))
|
|
92
|
-
return HttpStatus.BAD_REQUEST
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
response = response.setCookie(authenticationCookieName, res.accessToken, this.cookieOptions)
|
|
96
|
-
if (res.refreshToken) {
|
|
97
|
-
response = response.setCookie(`${authenticationCookieName}-refresh`, res.refreshToken, this.cookieOptions)
|
|
98
|
-
}
|
|
99
|
-
return response.send({ ok: true, accessToken: res.accessToken })
|
|
100
|
-
} catch (e) {
|
|
101
|
-
this.logger.warn(unknownToErrorMessage(e, 'Error during refreshing token'))
|
|
102
|
-
throw new HttpException('Token could not be refreshed', HttpStatus.UNAUTHORIZED)
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|