Add login button

This commit is contained in:
Rodrigo Pombo 2019-02-07 13:51:37 -03:00
parent 1d8ac9b307
commit 1ec578ba25
4 changed files with 83 additions and 12 deletions

View File

@ -4,6 +4,7 @@
"private": true,
"dependencies": {
"diff": "^4.0.1",
"netlify-auth-providers": "^1.0.0-alpha5",
"prismjs": "^1.15.0",
"react": "^16.8.1",
"react-dom": "^16.8.1",

View File

@ -1,6 +1,19 @@
import netlify from "netlify-auth-providers";
const TOKEN_KEY = "github-token";
function getHeaders() {
const token = window.localStorage.getItem(TOKEN_KEY);
return token ? { Authorization: `bearer ${token}` } : {};
}
export function isLoggedIn() {
return !!window.localStorage.getItem(TOKEN_KEY);
}
async function getContent(repo, sha, path) {
const contentResponse = await fetch(
`https://api.github.com/repos/${repo}/contents${path}?ref=${sha}`
`https://api.github.com/repos/${repo}/contents${path}?ref=${sha}`,
{ headers: getHeaders() }
);
if (!contentResponse.ok) {
@ -13,7 +26,8 @@ async function getContent(repo, sha, path) {
export async function getHistory(repo, sha, path, top = 10) {
const commitsResponse = await fetch(
`https://api.github.com/repos/${repo}/commits?sha=${sha}&path=${path}`
`https://api.github.com/repos/${repo}/commits?sha=${sha}&path=${path}`,
{ headers: getHeaders() }
);
if (!commitsResponse.ok) {
throw commitsResponse;
@ -48,3 +62,21 @@ export async function getHistory(repo, sha, path, top = 10) {
return commits;
}
export function auth() {
return new Promise((resolve, reject) => {
var authenticator = new netlify({
site_id: "ccf3a0e2-ac06-4f37-9b17-df1dd41fb1a6"
});
authenticator.authenticate({ provider: "github", scope: "repo" }, function(
err,
data
) {
if (err) {
reject(err);
}
window.localStorage.setItem(TOKEN_KEY, data.token);
resolve(data);
});
});
}

View File

@ -1,4 +1,4 @@
import { getHistory } from "./github";
import { getHistory, auth, isLoggedIn } from "./github";
import { getLanguage, getLanguageDependencies } from "./language-detector";
const [repo, sha, path] = getParams();
@ -20,17 +20,50 @@ if (!repo) {
loadLanguage(lang)
])
.then(([commits, app]) => {
if (!commits.length) {
throw new Error("No commits for this file? Maybe the path is wrong");
}
app.render(commits, root, lang);
})
.catch(error => {
if (error.status === 403) {
message.innerHTML =
"<p>GitHub API rate limit exceeded for your IP (60 requests per hour).</p><p>I need to add authentication.</p>";
} else {
console.error(error);
message.innerHTML = `<p>Unexpected error. Check the console.</p>`;
}
});
.catch(handleError);
}
function handleError(error) {
const message = document.getElementById("message");
if (error.status === 403) {
message.innerHTML =
"<p>GitHub API rate limit exceeded for your IP (60 requests per hour).</p><p>Log in with GitHub for more</p>";
const button = document.createElement("button");
button.textContent = "Login";
button.onclick = () => {
auth()
.then(data => {
window.location.reload(false);
})
.catch(console.error);
};
message.appendChild(button);
} else if (error.status === 404) {
message.innerHTML = `<p>File not found</p>${
isLoggedIn() ? "" : "<p>Is it from a private repo? Log in with GitHub"
}`;
if (!isLoggedIn) {
const button = document.createElement("button");
button.textContent = "Login";
button.onclick = () => {
auth()
.then(data => {
window.location.reload(false);
})
.catch(console.error);
};
message.appendChild(button);
}
} else {
console.error(error);
message.innerHTML = `<p>Unexpected error. Check the console.</p>`;
}
}
function loadLanguage(lang) {

View File

@ -6439,6 +6439,11 @@ neo-async@^2.5.0:
resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.6.0.tgz#b9d15e4d71c6762908654b5183ed38b753340835"
integrity sha512-MFh0d/Wa7vkKO3Y3LlacqAEeHK0mckVqzDieUKTT+KGxi+zIpeVsFxymkIiRpbpDziHc290Xr9A1O4Om7otoRA==
netlify-auth-providers@^1.0.0-alpha5:
version "1.0.0-alpha5"
resolved "https://registry.yarnpkg.com/netlify-auth-providers/-/netlify-auth-providers-1.0.0-alpha5.tgz#f0ce642fe5534f04a1d539ca847c907dd20819c8"
integrity sha512-V4tqW60NEOYdd7QUWotB+XeMbw/kayi4Sbm67hSMWibXHG7xiRUp6+VEB8CmBt7/kb3HTw7+mQSwF7YR9hRaSQ==
nice-try@^1.0.4:
version "1.0.5"
resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366"