Skip to content

Commit

Permalink
Add login button
Browse files Browse the repository at this point in the history
  • Loading branch information
pomber committed Feb 7, 2019
1 parent 1d8ac9b commit 1ec578b
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 12 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
36 changes: 34 additions & 2 deletions src/github.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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);
});
});
}
53 changes: 43 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 1ec578b

Please sign in to comment.