diff --git a/website/src/api/github.js b/website/src/api/github.js index 435e097b..ce2b66df 100644 --- a/website/src/api/github.js +++ b/website/src/api/github.js @@ -4,55 +4,65 @@ const AxiosInstance = axios.create({ headers: { 'Accept': 'application/vnd.github.v3+json' }, }); -async function getAccessToken(code) { - +function bindApiCall({ url, config, errorMessage }) { try { - const getAccessToken = await AxiosInstance.get('https://github.com/login/oauth/access_token', { + const apiCall = AxiosInstance.get(url, { + ...config, + validateStatus: function (status) { + return status < 500; // Resolve only if the status code is less than 500 + } + }) + + return apiCall + } catch (error) { + console.log(errorMessage, error.message) + } +} + +async function getAccessToken(code) { + const accessToken = await bindApiCall({ + url: 'https://github.com/login/oauth/access_token', + config: { params: { code, client_id: 'cd8f9be2562bfc8d6cfc', client_secret: '2509358055095d52dfd7331d072f378e7f16940f', }, - validateStatus: function (status) { - return status < 500; // Resolve only if the status code is less than 500 - } - }) + errorMessage: 'error getAccessToken' + } + }) - return getAccessToken.data; - } catch (error) { - console.log("error getAccessToken", error.message) - } + return accessToken.data } export async function getUser(access_token) { - try { - const getUserLogin = await AxiosInstance.get("https://api.github.com/user", { + const user = await bindApiCall({ + url: 'https://api.github.com/user', + config: { headers: { Authorization: `token ${access_token}` }, - validateStatus: function (status) { - return status < 500; // Resolve only if the status code is less than 500 - } - }) + }, + errorMessage: 'error getUser' + }) - return { - login: getUserLogin.data.login, - status: getUserLogin.status - } - } catch (error) { - console.log("error getUser", error.message) + return { + login: user.data?.login, + error: user.data?.error_description, + status: user.status } + } export async function checkUserCollaboratorStatus(code) { const { access_token } = await getAccessToken(code) const { login } = await getUser(access_token) - try { - const isUserCollaborator = await AxiosInstance.get(`https://docs-access.dagger.io/u/${login}`) - return { - status: isUserCollaborator.status, - access_token - } - } catch (error) { - console.log("error checkUserCollaboratorStatus", error.message); + const isUserCollaborator = await bindApiCall({ + url: `https://docs-access.dagger.io/u/${login}`, + errorMessage: 'error checkUserCollaboratorStatus' + }) + + return { + isAllowed: isUserCollaborator.data, + access_token } } \ No newline at end of file diff --git a/website/src/components/DocPageAuthentication.js b/website/src/components/DocPageAuthentication.js index 2f8f6667..8264aaeb 100644 --- a/website/src/components/DocPageAuthentication.js +++ b/website/src/components/DocPageAuthentication.js @@ -6,7 +6,7 @@ export default function DocAuthentication() { return (
Please Sign in to Github in order to get access to the doc
+Please Sign In to Github to get access to the doc