Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧑‍💻 Launch manifest on dev mode without token secret key #241

Merged
merged 3 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 1 addition & 23 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,3 @@
# Examples

This folders hosts examples of Manifest implementations for **contributors**.

## How to work with npm link

[NPM Link](https://docs.npmjs.com/cli/v10/commands/npm-link) is a great tool to develop packages on real-world situations without having to publish them.

### Example with NPM manifest package

First go to `packages/core/manifest` and run this command to link the "manifest" package.

```
sudo npm link
```

the go to a folder that as `manifest` as a dependency in its `package.json` and run

```
npm link manifest
```

The local "manifest" package will now replace the dependency.

Attention: for some reason the `nodemon` binary gets removed this way. Try to add it if the `npm run manifest` command is not working: `npm i nodemon`
This folder hosts examples of Manifest implementations.
1 change: 0 additions & 1 deletion examples/main-demo/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@

node_modules
Binary file modified examples/main-demo/manifest/backend.db
Binary file not shown.
1 change: 0 additions & 1 deletion packages/core/manifest/.env.contribution
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
TOKEN_SECRET_KEY=ReplaceThisToWhatever
NODE_ENV=contribution
4 changes: 2 additions & 2 deletions packages/core/manifest/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class AuthService {
return {
token: jwt.sign(
{ email: signupUserDto.email, entitySlug },
this.configService.get('TOKEN_SECRET_KEY')
this.configService.get('tokenSecretKey')
)
}
}
Expand Down Expand Up @@ -142,7 +142,7 @@ export class AuthService {
try {
decoded = jwt.verify(
token?.replace('Bearer ', ''),
this.configService.get('TOKEN_SECRET_KEY')
this.configService.get('tokenSecretKey')
) as jwt.JwtPayload
} catch {
return Promise.resolve({ user: null, entitySlug: null })
Expand Down
2 changes: 1 addition & 1 deletion packages/core/manifest/src/auth/tests/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe('AuthService', () => {

const { jwtToken, entitySlug } = jwt.sign(
'[email protected]',
configService.get('TOKEN_SECRET_KEY')
configService.get('tokenSecretKey')
)

expect(await authService.getUserFromToken(jwtToken)).toMatchObject({
Expand Down
11 changes: 8 additions & 3 deletions packages/core/manifest/src/config/general.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { DEFAULT_PORT } from '../constants'
import { DEFAULT_PORT, DEFAULT_TOKEN_SECRET_KEY } from '../constants'

export default (): { port: number | string; nodeEnv: string } => {
export default (): {
port: number | string
nodeEnv: string
tokenSecretKey: string
} => {
return {
port: process.env.PORT || DEFAULT_PORT,
nodeEnv: process.env.NODE_ENV || 'development'
nodeEnv: process.env.NODE_ENV || 'development',
tokenSecretKey: process.env.TOKEN_SECRET_KEY || DEFAULT_TOKEN_SECRET_KEY
}
}
1 change: 1 addition & 0 deletions packages/core/manifest/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const DEFAULT_SEED_COUNT: number = 50
export const DEFAULT_MAX_MANY_TO_MANY_RELATIONS: number = 5
export const DUMMY_FILE_NAME: string = 'dummy-invoice.pdf'
export const DUMMY_IMAGE_NAME: string = 'dummy-image.jpg'
export const DEFAULT_TOKEN_SECRET_KEY: string = 'REPLACE_ME'

// Uploads.
export const STORAGE_PATH: string = 'public/storage'
Expand Down
11 changes: 10 additions & 1 deletion packages/core/manifest/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as express from 'express'
import * as livereload from 'livereload'
import { join } from 'path'
import { AppModule } from './app.module'
import { DEFAULT_PORT } from './constants'
import { DEFAULT_PORT, DEFAULT_TOKEN_SECRET_KEY } from './constants'
import { OpenApiService } from './open-api/services/open-api.service'

async function bootstrap() {
Expand All @@ -26,6 +26,15 @@ async function bootstrap() {
const isProduction: boolean = configService.get('NODE_ENV') === 'production'
const isTest: boolean = configService.get('NODE_ENV') === 'test'

if (
isProduction &&
configService.get('tokenSecretKey') === DEFAULT_TOKEN_SECRET_KEY
) {
throw new Error(
'Token secret key not defined. Please set a custom token secret key to run in production environment adding TOKEN_SECRET_KEY in your env file.'
)
}

// Reload the browser when server files change.
if (!isProduction && !isTest) {
const liveReloadServer = livereload.createServer()
Expand Down
Loading