diff --git a/packages/server/README.md b/packages/server/README.md index d1b47a6913..179aa02d2f 100644 --- a/packages/server/README.md +++ b/packages/server/README.md @@ -252,7 +252,7 @@ To run the API to develop locally: - [`yarn workspace server prisma db push`][prisma db:push]: Synchronize `schema.prisma` with the database schema. - [`yarn workspace server prisma migrate dev`][prisma migrate]: Create a database migration based on the changes to `schema.prisma`. - [`yarn workspace server prisma generate`][prisma generate]: Generate the Prisma clients. -- `yarn workspace server authtoken [member id]`: Generate an authentication token for the provided member. +- `yarn workspace server dev:authtoken`: Generate an authentication token for the provided member. - `yarn workspace server lint:fix`: Fix some code formatting issue. ### Adding support for more QN events diff --git a/packages/server/package.json b/packages/server/package.json index 6ee0e89a1d..d79d700e2f 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -19,7 +19,7 @@ "dev:api": "yarn nodemon ./src/common/scripts/startApi.ts", "dev:notify": "yarn nodemon ./src/notifier/scripts/notify.ts", "dev:mockEmail": "yarn ts-node ./src/notifier/scripts/mockEmail.ts", - "dev:authtoken": "yarn nodemon ./src/auth/scripts/generateToken.ts", + "dev:authtoken": "yarn ts-node ./src/auth/scripts/generateToken.ts", "dev:emails": "email dev --dir ./src/common/email-templates", "lint": "yarn lint:prettier --check && yarn lint:eslint --max-warnings=0", "lint:eslint": "eslint \"./{src,test}/**/*.ts\"", diff --git a/packages/server/src/auth/model/token.ts b/packages/server/src/auth/model/token.ts index a4ff9f26bd..4fb43952a9 100644 --- a/packages/server/src/auth/model/token.ts +++ b/packages/server/src/auth/model/token.ts @@ -23,10 +23,10 @@ const jwtToken = Yup.object({ iat: Yup.number() }) const AUTH_TOKEN_TTL = 3600_000 * 24 * 90 -export const createAuthToken = (memberId: number): string => { +export const createAuthToken = (memberId: number, secret = APP_SECRET_KEY): string => { const exp = Date.now() + AUTH_TOKEN_TTL const payload: AuthTokenPayload = { memberId, type: TokenType.Authentication, exp } - return sign(payload, APP_SECRET_KEY) + return sign(payload, secret) } export const getAuthenticatedMember = async (req: ExpressContext['req'] | undefined): Promise => { diff --git a/packages/server/src/auth/scripts/generateToken.ts b/packages/server/src/auth/scripts/generateToken.ts index aafdff619a..f7cd8f6120 100644 --- a/packages/server/src/auth/scripts/generateToken.ts +++ b/packages/server/src/auth/scripts/generateToken.ts @@ -1,10 +1,26 @@ +import readline from 'readline' + import { createAuthToken } from '@/auth/model/token' -import { prisma } from '@/common/prisma' +import { APP_SECRET_KEY } from '@/common/config' -const memberId = process.argv[2] ? Number(process.argv[2]) : 1 +run() -if (!prisma.member.findUnique({ where: { id: memberId } })) { - process.stderr.write(`There is no member with the id ${memberId}\n`) -} +async function run() { + const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, + terminal: false, + }) + + const memberId: number = await new Promise((res) => rl.question('Member id [1]:', (a) => res((a && Number(a)) || 1))) + const secret: string = await new Promise((res) => + rl.question(`App secret key [${APP_SECRET_KEY}]:`, (a) => res(a || APP_SECRET_KEY)) + ) -process.stdout.write(createAuthToken(Number(memberId)) + '\n') + rl.close() + + const token = createAuthToken(Number(memberId), secret) + '\n' + const httpHeaders = { Authorization: `Bearer ${token}` } + + process.stdout.write(JSON.stringify(httpHeaders, null, 2) + '\n') +}