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

[backend] Implement JWT authentication #34

Merged
merged 7 commits into from
Mar 23, 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
3 changes: 3 additions & 0 deletions .github/workflows/backend-service-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ jobs:
with:
node-version: 18.x

- name: Create .env file
run: echo "JWT_SECRET=${{ secrets.JWT_SECRET }}" > .env

- name: Start service in docker
run: |
npm run docker:up
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/frontend-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ jobs:
with:
service-id: ${{ secrets.MY_RENDER_SERVICE_ID_FE }}
api-key: ${{ secrets.MY_RENDER_API_KEY }}
wait-for-success: true
5 changes: 5 additions & 0 deletions .github/workflows/frontend-service-tests-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ jobs:
with:
node-version: 18.x

- name: Create .env file
run: |
cd ./backend
echo "JWT_SECRET=${{ secrets.JWT_SECRET }}" > .env

- name: Start service in docker
run: |
cd ./backend
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/frontend-service-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ jobs:
with:
node-version: 18.x

- name: Create .env file
run: |
cd ./backend
echo "JWT_SECRET=${{ secrets.JWT_SECRET }}" > .env

- name: Start service in docker
run: |
cd ./backend
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/playwright-production-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: Playwright e2e Production Tests

on:
workflow_dispatch:
schedule:
- cron: '0 12 * * *'

defaults:
run:
Expand Down
3 changes: 3 additions & 0 deletions backend/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ services:
POSTGRES_USER: test
POSTGRES_PASSWORD: test
POSTGRES_DB: test
env_file:
- .env
ports:
- "5432:5432"
app:
Expand All @@ -16,4 +18,5 @@ services:
- "5000:5000"
environment:
DATABASE_URL: "postgresql://test:test@postgres:5432/test"

depends_on: [postgres]
109 changes: 102 additions & 7 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"devDependencies": {
"@types/cors": "2.8.17",
"@types/express": "4.17.21",
"@types/jsonwebtoken": "^9.0.6",
"@types/node": "20.10.6",
"@types/supertest": "6.0.2",
"@vitest/coverage-istanbul": "1.2.1",
Expand All @@ -41,6 +42,7 @@
"@prisma/client": "5.7.1",
"cors": "2.8.5",
"dotenv": "16.3.2",
"express": "4.18.2"
"express": "4.18.2",
"jsonwebtoken": "^9.0.2"
}
}
3 changes: 2 additions & 1 deletion backend/src/__mocks__/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { PrismaClient } from '@prisma/client'
import { beforeEach } from 'vitest'
import { mockDeep, mockReset } from 'vitest-mock-extended'

// 2

beforeEach(() => {
mockReset(prisma)
})


// 3
const prisma = mockDeep<PrismaClient>()
export default prisma
20 changes: 20 additions & 0 deletions backend/src/authenticateToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import jwt from "jsonwebtoken";

function authenticateToken(req, res, next) {
const authHeader = req.headers["authorization"];
const token = authHeader && authHeader.split(" ")[1];

if (!token) {
return res.status(401).send({ error: "unauthorized" });
}

jwt.verify(token, process.env.JWT_SECRET, (err, user) => {
if (err) {
return res.status(403).send({ error: "forbidden" });
}
req.user = user;
next();
});
}

export default authenticateToken;
Loading
Loading