@@ -3,25 +3,19 @@ const database = require("../database/sqlite");
3
3
const logger = require ( "../utilities/logger" ) ;
4
4
const { discordLinkTemplate, clientId, redirectUri, use_caching, cache_size, use_given_table, checkGuild, guildId} = require ( "../configuration/config" ) ;
5
5
const dHelper = require ( "../utilities/discordhelper" ) ;
6
- const { checkAccess} = require ( "../utilities/tokenutils" ) ;
7
- const path = require ( "path" ) ;
6
+ const { validateToken} = require ( "../utilities/tokenutils" ) ;
8
7
const CacheManager = require ( "../utilities/cache_managing" ) ;
9
8
const { setGivenTo, getGivenBySS14Id, setGivenToZeroAll, setGivenDiscordTo, getGivenByDiscordId, getUserByDiscordId,
10
9
getUserBySS14Id } = require ( '../database/sqlite' ) ;
11
10
const { checkInGuild} = require ( "../utilities/discordhelper" ) ;
12
11
13
12
const userCache = new CacheManager ( cache_size ) ;
14
13
15
- router . get ( "/check" , async ( req , res ) => {
16
- if ( ! req . query . api_token )
17
- return res . status ( 401 ) . send ( "Unauthorized" )
18
-
19
- if ( ! checkAccess ( req . query . api_token ) )
20
- return res . status ( 401 ) . send ( "Unauthorized" )
14
+ router . use ( validateToken ) ;
21
15
22
- if ( ! req . query . userid ) {
16
+ router . get ( "/check" , async ( req , res ) => {
17
+ if ( ! req . query . userid )
23
18
return res . status ( 400 ) . json ( { error : "No user id provided" } ) ;
24
- }
25
19
26
20
if ( use_caching ) {
27
21
const user = userCache . get ( req . query . userid ) ;
@@ -56,12 +50,6 @@ router.get("/check", async (req, res) => {
56
50
57
51
// generate auth link
58
52
router . get ( '/link' , async ( req , res ) => {
59
- if ( ! req . query . api_token )
60
- return res . status ( 401 ) . send ( "Unauthorized" )
61
-
62
- if ( ! checkAccess ( req . query . api_token ) )
63
- return res . status ( 401 ) . send ( "Unauthorized" )
64
-
65
53
if ( ! req . query . userid ) {
66
54
return res . status ( 400 ) . json ( { error : "No user ID provided" } ) ;
67
55
}
@@ -76,15 +64,8 @@ router.get('/link', async (req, res) => {
76
64
} ) ;
77
65
78
66
router . get ( '/roles' , async ( req , res ) => {
79
- if ( ! req . query . api_token )
80
- return res . status ( 401 ) . send ( "Unauthorized" )
81
-
82
- if ( ! checkAccess ( req . query . api_token ) )
83
- return res . status ( 401 ) . send ( "Unauthorized" )
84
-
85
- if ( ! req . query . userid ) {
67
+ if ( ! req . query . userid )
86
68
return res . status ( 400 ) . json ( { error : "No user ID provided" } ) ;
87
- }
88
69
89
70
if ( ! req . query . guildid ) {
90
71
return res . status ( 400 ) . json ( { error : 'No guild ID provided' } ) ;
@@ -111,20 +92,13 @@ router.get('/roles', async (req, res) => {
111
92
} )
112
93
113
94
router . get ( '/user' , async ( req , res ) => {
114
- if ( ! req . query . api_token )
115
- return res . status ( 401 ) . send ( "Unauthorized" )
116
-
117
- if ( ! checkAccess ( req . query . api_token ) )
118
- return res . status ( 401 ) . send ( "Unauthorized" )
119
-
120
- if ( ! req . query . method ) {
121
- return res . status ( 400 ) . json ( { error : "Method is not provided" } )
122
- }
123
-
95
+ if ( ! req . query . method )
96
+ return res . status ( 400 ) . json ( { error : "Method is not provided" } ) ;
124
97
125
98
if ( ! req . query . id ) {
126
99
return res . status ( 400 ) . json ( { error : "No user ID provided" } ) ;
127
100
}
101
+
128
102
const uid = req . query . id ;
129
103
130
104
if ( use_caching && req . query . method === 'ss14' ) {
@@ -154,7 +128,7 @@ router.get('/user', async (req, res) => {
154
128
}
155
129
156
130
if ( ! user ) {
157
- return res . status ( 400 ) . json ( { error : "Not Found" } ) ;
131
+ return res . status ( 404 ) . json ( { error : 'User not found' } ) ;
158
132
}
159
133
160
134
const { id, access_token, refresh_token, ...newUser } = user ;
@@ -163,12 +137,6 @@ router.get('/user', async (req, res) => {
163
137
} )
164
138
165
139
router . post ( '/given' , async ( req , res ) => {
166
- if ( ! req . query . api_token )
167
- return res . status ( 401 ) . send ( "Unauthorized" ) ;
168
-
169
- if ( ! checkAccess ( req . query . api_token ) )
170
- return res . status ( 401 ) . send ( "Unauthorized" ) ;
171
-
172
140
if ( ! use_given_table )
173
141
return res . status ( 405 ) . send ( "Given table is turned off" )
174
142
@@ -204,12 +172,6 @@ router.post('/given', async (req, res) => {
204
172
} )
205
173
206
174
router . get ( '/is_given' , async ( req , res ) => {
207
- if ( ! req . query . api_token )
208
- return res . status ( 401 ) . send ( "Unauthorized" )
209
-
210
- if ( ! checkAccess ( req . query . api_token ) )
211
- return res . status ( 401 ) . send ( "Unauthorized" )
212
-
213
175
if ( ! use_given_table )
214
176
return res . status ( 405 ) . send ( "Given table is turned off" )
215
177
@@ -250,12 +212,6 @@ router.get('/is_given', async (req, res) => {
250
212
} )
251
213
252
214
router . post ( '/wipe_given' , async ( req , res ) => {
253
- if ( ! req . query . api_token )
254
- return res . status ( 401 ) . send ( "Unauthorized" )
255
-
256
- if ( ! checkAccess ( req . query . api_token ) )
257
- return res . status ( 401 ) . send ( "Unauthorized" )
258
-
259
215
if ( ! use_given_table )
260
216
return res . status ( 405 ) . send ( "Given table is turned off" )
261
217
0 commit comments