Skip to content

Commit 80d9bba

Browse files
committed
Fixed the authorization for the PUT api after testing failure
1 parent e3e94d2 commit 80d9bba

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

routes/api/v1/API.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const {
1919
setSearchUserInCache,
2020
} = require('../../../src/cache')
2121
// import middlewares
22-
const {authenticateAPI} = require('../../../src/auth')
22+
const {authenticatePutAPI, authenticateGetAPI} = require('../../../src/auth')
2323
const {updateValidator, searchValidator} = require('../../../src/validator')
2424

2525
/**
@@ -208,7 +208,7 @@ router.get('/randomCodes', async (req, res, next) => {
208208
**/
209209
router.put(
210210
'/rateCode',
211-
authenticateAPI,
211+
authenticatePutAPI,
212212
updateValidator,
213213
async (req, res, next) => {
214214
try {
@@ -305,7 +305,7 @@ router.put(
305305
**/
306306
router.get(
307307
'/searchUser',
308-
authenticateAPI,
308+
authenticateGetAPI,
309309
searchValidator,
310310
async (req, res, next) => {
311311
try {

src/auth.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const {validateToken} = require('./engine')
22

3-
async function authenticateAPI(req, res, next) {
3+
async function authenticateGetAPI(req, res, next) {
44
const {authorization} = req.headers
55

66
if (authorization && authorization.startsWith('Bearer ')) {
@@ -24,6 +24,28 @@ async function authenticateAPI(req, res, next) {
2424
}
2525
}
2626

27+
async function authenticatePutAPI(req, res, next) {
28+
const {codeRatingEngineToken} = req.body
29+
30+
if (codeRatingEngineToken === undefined) {
31+
return res.status(401).json({
32+
status: 401,
33+
message: 'Invalid Token !! Please send the valid token',
34+
})
35+
} else {
36+
const tokenFound = await validateToken(codeRatingEngineToken)
37+
if (Array.isArray(tokenFound) && tokenFound.length > 0) {
38+
next()
39+
} else {
40+
return res.status(401).json({
41+
status: 401,
42+
message: 'Invalid Token !! Please send the valid token',
43+
})
44+
}
45+
}
46+
}
47+
2748
module.exports = {
28-
authenticateAPI,
49+
authenticateGetAPI,
50+
authenticatePutAPI,
2951
}

test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe('testing the code rating engine APIs', () => {
4141
request(app)
4242
.get('/api/v1/searchUser')
4343
.query({username: SEARCH_USER})
44-
.send({codeRatingEngineToken: token})
44+
.set('Authorization', `Bearer ${token}`)
4545
.expect(200)
4646
.end((err, res) => {
4747
if (err) done(err)

0 commit comments

Comments
 (0)