Skip to content

Commit

Permalink
[backend] Implement JWT authentication (#34)
Browse files Browse the repository at this point in the history
* [backend] Implement JWT authentication

* [backend] chore: Adding secret

* [backend] chore: Adding secret

* [backend] feat: User unit tests

* [frontend] feat: Login functionality

* [playwright] fix: Updating tests for login

* [actions] chore: Update action for env file
  • Loading branch information
helloitsdave authored Mar 23, 2024
1 parent 1e906b2 commit d2f69b3
Show file tree
Hide file tree
Showing 30 changed files with 1,178 additions and 363 deletions.
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

2 comments on commit d2f69b3

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage for this commit

97.73%

Coverage Report
FileBranchesFuncsLinesUncovered Lines
src
   authenticateToken.ts100%100%100%
   index.ts96.77%90%97.33%165–167
   prisma.ts100%100%100%
src/__mocks__
   prisma.ts100%100%100%

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage for this commit

91.43%

Coverage Report
FileBranchesFuncsLinesUncovered Lines
src
   App.tsx0%0%0%1, 1, 10–19, 2, 20–27, 3–9
   NoteApp.tsx94.12%100%96.69%66–70
src/api
   apiService.ts90.91%100%100%18
src/components
   Header.tsx0%0%0%1, 1–9
   Login.tsx100%100%100%
   Note.tsx100%100%100%
   NoteForm.tsx100%100%100%
   NoteFormModal.tsx100%100%100%
   NoteGrid.tsx100%100%100%
   Spinner.tsx100%100%100%

Please sign in to comment.