Skip to content

Commit

Permalink
Merge pull request #24 from KBandipo/@gh-12
Browse files Browse the repository at this point in the history
Fix: Collabocate client & API gives success response for access token's 401 error
  • Loading branch information
Akpjunior94 authored Nov 18, 2024
2 parents 866f41b + ca9b7ff commit 714f2ab
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
19 changes: 19 additions & 0 deletions @api-server/api/app.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { Request } from 'express';
import { unauthorizedErr } from '../lib/errors/Errors';

export const getIssuesService = async () => {
const response = await fetch(`${process.env.REPO_API_URL}/issues`, {
headers: {
Authorization: `Bearer ${process.env.GITHUB_PERSONAL_ACCESS_TOKEN}`,
},
});
if (response.status === 401) {
unauthorizedErr("Unauthorized: Can't access this resource");
}
const data = await response.json();
return data;
}
Expand All @@ -23,6 +27,11 @@ export const createIssueService = async (req: Request) => {
body: body + '\n\n' + '#' + '\n' + '> Submitted via **Collabocate** [[GitHubSync]](https://github.com/collabo-community/collabocate)',
}),
});

if (response.status === 401) {
unauthorizedErr("Unauthorized: Can't access this resource");
}

const data = await response.json();
return data;
}
Expand All @@ -33,6 +42,11 @@ export const getPullRequestsService = async () => {
Authorization: `Bearer ${process.env.GITHUB_PERSONAL_ACCESS_TOKEN}`,
},
});

if (response.status === 401) {
unauthorizedErr("Unauthorized: Can't access this resource");
}

const data = await response.json();
return data;
}
Expand All @@ -44,6 +58,11 @@ export const getRepositoriesService = async () => {
},
}
);

if (response.status === 401) {
unauthorizedErr("Unauthorized: Can't access this resource");
}

const data = await response.json();
return data;
}
14 changes: 12 additions & 2 deletions @api-server/lib/errors/Errors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CustomError, HttpCode } from './CustomError';
import { CustomError, HttpCode} from './CustomError';

// --- Error Classes ---
class NotFoundError extends CustomError {
Expand All @@ -10,6 +10,14 @@ class NotFoundError extends CustomError {
class BadRequestError extends CustomError {
constructor(message: string){
super(message, HttpCode.BAD_REQUEST)

}
}


class UnauthorizedError extends CustomError {
constructor(message: string){
super(message, HttpCode.UNAUTHORIZED)
}
}

Expand All @@ -18,11 +26,13 @@ class BadRequestError extends CustomError {
interface ThrowError {
notFoundErr: (message: string) => void;
badRequestErr: (message: string) => void;
unauthorizedErr: (message: string) => void;
}

const throwError: ThrowError = {
notFoundErr: (message) => { throw new NotFoundError(message); },
badRequestErr: (message) => { throw new BadRequestError(message); },
unauthorizedErr: (message) => { throw new UnauthorizedError(message); },
};

export const { notFoundErr, badRequestErr } = throwError;
export const { notFoundErr, badRequestErr, unauthorizedErr } = throwError;
6 changes: 6 additions & 0 deletions @dev-client/views/@plugin_githubsync/githubsync.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ submitIssueForm.addEventListener('submit', async (e) => {
},
body: JSON.stringify({ title, body }),
});

if (response.status === 401){
displayToastrMessage.innerHTML = 'Unauthorized: Can\'t access this resource';
return;
}

const data = await response.json();
console.log(data);
issueTitleInput.value = '';
Expand Down

0 comments on commit 714f2ab

Please sign in to comment.