Skip to content
Open
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
29 changes: 29 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: CI

on:
push:
branches: [main]
pull_request:

jobs:
verify:
runs-on: ubuntu-latest

steps:
- name: Check out repository
uses: actions/checkout@v4

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: npm

- name: Install dependencies
run: npm ci

- name: Run tests
run: npm test -- --runInBand --passWithNoTests

- name: Build client
run: npm run build --workspace client
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ PORT=4000
MONGO_URI=mongodb://tasks:pass1234@ac-j3acrgb-shard-00-00.lueesfz.mongodb.net:27017,ac-j3acrgb-shard-00-01.lueesfz.mongodb.net:27017,ac-j3acrgb-shard-00-02.lueesfz.mongodb.net:27017/?ssl=true&replicaSet=atlas-6to6iy-shard-0&authSource=admin&appName=Cluster0
JWT_SECRET=4a498d90a0f923ae4b3f1f65571ff25985d17e2f3286e8e90383e449f68a40a8
JWT_EXPIRES_IN=7d
CSRF_SECRET=******a-separate-long-random-secret
```

## What was added on top of Task 3
Expand Down
28 changes: 28 additions & 0 deletions client/src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,31 @@ export const api = axios.create({
baseURL: 'http://localhost:4000/api',
withCredentials: true
})

let csrfToken
const csrfExemptUrls = ['/auth/login', '/auth/register', '/csrf-token']

api.interceptors.request.use(async config => {
const method = (config.method || 'get').toLowerCase()
const url = config.url || ''
if (['get', 'head', 'options'].includes(method) ||
csrfExemptUrls.some(path => url.includes(path))) {
return config
}

if (!csrfToken) {
const { data } = await api.get('/csrf-token')
csrfToken = data.csrfToken
}

config.headers.set('x-csrf-token', csrfToken)
return config
})

api.interceptors.response.use(response => {
const url = response.config.url || ''
if (url.includes('/auth/login') || url.includes('/auth/register') || url.includes('/auth/logout')) {
csrfToken = undefined
}
return response
})
Loading