Skip to content

Commit

Permalink
🩹 Fix the dev:authtoken script (#4622)
Browse files Browse the repository at this point in the history
Fix the `dev:authtoken` script
  • Loading branch information
thesan authored Nov 8, 2023
1 parent a0fb1ae commit bd94c3e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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\"",
Expand Down
4 changes: 2 additions & 2 deletions packages/server/src/auth/model/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Member | null> => {
Expand Down
28 changes: 22 additions & 6 deletions packages/server/src/auth/scripts/generateToken.ts
Original file line number Diff line number Diff line change
@@ -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')
}

0 comments on commit bd94c3e

Please sign in to comment.