File tree Expand file tree Collapse file tree 3 files changed +62
-0
lines changed Expand file tree Collapse file tree 3 files changed +62
-0
lines changed Original file line number Diff line number Diff line change
1
+ function CreateApiKeyHandler ( db ) {
2
+ async function checkIfApiKeyExists ( ctx ) {
3
+ const { apikey } = ctx . query
4
+ if ( ! apikey ) {
5
+ ctx . status = 400
6
+ ctx . body = { error : 'apikey query param not provided' }
7
+ return
8
+ }
9
+ const users = await db . readAll ( 'users' )
10
+
11
+ const apiKeyExists = users . map ( ( user ) => {
12
+ return user . apiKey === apikey
13
+ } )
14
+
15
+ if ( apiKeyExists . includes ( true ) ) {
16
+ ctx . status = 200
17
+ return
18
+ }
19
+
20
+ ctx . status = 404
21
+ }
22
+
23
+ return {
24
+ checkIfApiKeyExists
25
+ }
26
+ }
27
+
28
+ module . exports = CreateApiKeyHandler
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ const database = require('./database')
8
8
const CreateUserHandler = require ( './handlers/users' )
9
9
const CreateAuthHandler = require ( './handlers/auth' )
10
10
const CreateFeedbackHandler = require ( './handlers/feedbacks' )
11
+ const CreateApiKeyHandler = require ( './handlers/apikey' )
11
12
12
13
const app = new Koa ( )
13
14
const router = new Router ( )
@@ -23,11 +24,13 @@ app.use(cors())
23
24
const feedbacksHandler = CreateFeedbackHandler ( database )
24
25
const usersHandler = CreateUserHandler ( database )
25
26
const authHandler = CreateAuthHandler ( database )
27
+ const apiKeyHandler = CreateApiKeyHandler ( database )
26
28
27
29
router . get ( '/' , ( ctx ) => {
28
30
ctx . status = 200
29
31
ctx . body = { message : new Date ( ) }
30
32
} )
33
+ router . head ( '/apikey/exists' , apiKeyHandler . checkIfApiKeyExists )
31
34
router . post ( '/auth/register' , usersHandler . create )
32
35
router . post ( '/auth/login' , authHandler . login )
33
36
router . get ( '/users/me' , authMiddleware , usersHandler . getLoggerUser )
Original file line number Diff line number Diff line change 42
42
}
43
43
]
44
44
},
45
+ {
46
+ "name" : " Apikey" ,
47
+ "item" : [
48
+ {
49
+ "name" : " Checar se a apikey existe" ,
50
+ "request" : {
51
+ "method" : " HEAD" ,
52
+ "header" : [],
53
+ "url" : {
54
+ "raw" : " http://localhost:3000/apikey/exists?apikey=fcd5015c-10d3-4e9c-b395-ec7ed8850165" ,
55
+ "protocol" : " http" ,
56
+ "host" : [
57
+ " localhost"
58
+ ],
59
+ "port" : " 3000" ,
60
+ "path" : [
61
+ " apikey" ,
62
+ " exists"
63
+ ],
64
+ "query" : [
65
+ {
66
+ "key" : " apikey" ,
67
+ "value" : " fcd5015c-10d3-4e9c-b395-ec7ed8850165"
68
+ }
69
+ ]
70
+ }
71
+ },
72
+ "response" : []
73
+ }
74
+ ]
75
+ },
45
76
{
46
77
"name" : " Auth" ,
47
78
"item" : [
You can’t perform that action at this time.
0 commit comments