Skip to content

Commit

Permalink
update authentication request
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoanle396 committed Aug 1, 2024
1 parent 3ccb952 commit 2d7bbc9
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ TYPEORM_DATABASE=
## LOG
LOG_DIR=logs

## CRAWL
AUTH_TOKEN=

# FRONTEND
FE_PORT=3000
NEXT_PUBLIC_API_URL=api
7 changes: 6 additions & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ on:
push:
branches:
- demo
env:
AUTH_TOKEN: ${{ vars.AUTH_TOKEN }}

jobs:
handle-commit:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -47,4 +50,6 @@ jobs:
run: |
echo $output
echo "$output"
curl "$output"
curl --request GET \
--url "$output" \
--header "Authorization: Bearer $AUTH_TOKEN"
5 changes: 4 additions & 1 deletion apps/server/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ TYPEORM_PASSWORD=<your database password>
TYPEORM_DATABASE=<your database name>

## logging
LOG_DIR=<log directory>
LOG_DIR=<log directory>

## Crawl
AUTH_TOKEN=
3 changes: 2 additions & 1 deletion apps/server/src/routes/crawl.route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CrawlController } from '@/controllers/crawl.controller'
import { Routes } from '@/decorator/routes'
import { authenticationGithub } from '@/shared/middleware-github'
import { Router } from 'express'

export class CrawlRoute implements Routes {
Expand All @@ -12,6 +13,6 @@ export class CrawlRoute implements Routes {
}

private initializeRoute() {
this.router.get('/', this.crawlController.crawl)
this.router.get('/', authenticationGithub, this.crawlController.crawl)
}
}
21 changes: 21 additions & 0 deletions apps/server/src/shared/middleware-github.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { NextFunction, Request, Response } from 'express'

function authenticationGithub(req: Request, res: Response, next: NextFunction) {
try {
const token = (req.headers.authorization ?? '').split(' ')[1]
if (token === process.env.AUTH_TOKEN) {
next()
}
return res.status(403).send({
code: 403,
message: 'Token is invalid!',
})
} catch (error) {
return res.status(403).send({
code: 500,
message: error?.message ?? 'Something went wrong',
})
}
}

export { authenticationGithub }
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ services:
- TYPEORM_PASSWORD=${TYPEORM_PASSWORD}
- TYPEORM_DATABASE=${TYPEORM_DATABASE}
- LOG_DIR=${LOG_DIR}
- AUTH_TOKEN=${AUTH_TOKEN}
- NODE_ENV=${NODE_ENV}
ports:
- ${PORT}:${PORT}
Expand Down

0 comments on commit 2d7bbc9

Please sign in to comment.