@@ -203,7 +203,17 @@ async function getUserCount() {
203
203
}
204
204
}
205
205
206
- async function getSelfUser ( ) {
206
+ function showToaster ( message ) {
207
+ const toaster = document . querySelector ( '.toast__message' ) ;
208
+ toaster . innerText = message ;
209
+ toaster . classList . add ( 'toast--show' ) ;
210
+
211
+ setTimeout ( ( ) => {
212
+ toaster . classList . remove ( 'toast--show' ) ;
213
+ } , 3000 ) ;
214
+ }
215
+
216
+ async function getIsSuperUser ( ) {
207
217
try {
208
218
const res = await fetch ( `${ API_BASE_URL } /users/self` , {
209
219
method : 'GET' ,
@@ -213,11 +223,27 @@ async function getSelfUser() {
213
223
} ,
214
224
} ) ;
215
225
226
+ if ( res . status === 500 ) {
227
+ showToaster ( 'Something went wrong. Internal Server Error!' ) ;
228
+ setTimeout ( ( ) => {
229
+ window . location . href = '/index.html' ;
230
+ } , 3000 ) ;
231
+ return ;
232
+ }
233
+
234
+ if ( res . status === 401 ) {
235
+ showToaster ( 'You are not logged-in. Please login!' ) ;
236
+ setTimeout ( ( ) => {
237
+ window . location . href = '/index.html' ;
238
+ } , 3000 ) ;
239
+ return ;
240
+ }
241
+
216
242
const self_user = await res . json ( ) ;
217
- return self_user ;
243
+ return { isSuperUser : self_user ?. roles [ SUPER_USER ] } ;
218
244
} catch ( err ) {
219
245
console . error ( 'Error in fetching self user ' + err ) ;
220
- return { } ;
246
+ return { error : err , isSuperUser : false } ;
221
247
}
222
248
}
223
249
@@ -319,4 +345,4 @@ async function fillData(identityLogs, next, prev) {
319
345
}
320
346
}
321
347
322
- export { getIdentityLogs , getSelfUser , fillData , getUserCount } ;
348
+ export { getIdentityLogs , getIsSuperUser , fillData , getUserCount } ;
0 commit comments