Skip to content

Commit

Permalink
feat: 752 update error handling after permission failure
Browse files Browse the repository at this point in the history
  • Loading branch information
mutambaraf committed Nov 29, 2022
1 parent 5a5dd07 commit 8b3e142
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
19 changes: 15 additions & 4 deletions apps/user-office-backend/src/middlewares/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,21 @@ export default function factory() {
const decodedUser = req.user;
if (decodedUser) {
if (accessTokenId) {
res.locals.agent = await getUserWithRoleFromAccessTokenId(
accessTokenId
);
next();
await getUserWithRoleFromAccessTokenId(accessTokenId)
.then((accessPermissions) => {
if (!accessPermissions) {
return res.status(401).send('Unauthorized token');
}
res.locals.agent = accessPermissions;
next();
})
.catch((e) => {
logger.logException(
defaultErrorMessage + 'unauthorized token',
e
);
res.status(401).send(defaultErrorMessage + 'unauthorized token');
});
} else {
baseContext.queries.user
.getAgent(decodedUser.user.id)
Expand Down
29 changes: 15 additions & 14 deletions apps/user-office-frontend/src/context/DownloadContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -215,27 +215,28 @@ export const DownloadContextProvider: React.FC = ({ children }) => {
.then(async (response) => {
await delayInTest();
if (response.status !== 200) {
if (response.status === 401) {
const reason =
'Your session has expired, you will need to log in again through the external homepage';
enqueueSnackbar(reason, {
variant: 'error',
className: 'snackbar-error',
autoHideDuration: 10000,
});
handleLogout();
}

return Promise.reject(await response.text());
}

await promptDownload(response);
cleanUpDownload(id);
})
.catch((e) => {
if (e.includes('EXTERNAL_TOKEN_INVALID')) {
enqueueSnackbar(
'Your session has expired, you will need to log in again through the external homepage',
{
variant: 'error',
className: 'snackbar-error',
autoHideDuration: 10000,
}
);
handleLogout();
} else if (e.name !== 'AbortError') {
.catch((error) => {
if (error !== 'EXTERNAL_TOKEN_INVALID' && error.name !== 'AbortError') {
enqueueSnackbar('Failed to download file', { variant: 'error' });
}

})
.finally(() => {
cleanUpDownload(id);
});

Expand Down

0 comments on commit 8b3e142

Please sign in to comment.