diff --git a/src/backends/git-gateway/API.js b/src/backends/git-gateway/API.js index 10737bb35a61..91221f03a71f 100644 --- a/src/backends/git-gateway/API.js +++ b/src/backends/git-gateway/API.js @@ -10,6 +10,22 @@ export default class API extends GithubAPI { this.repoURL = ""; } + hasWriteAccess() { + return this.getBranch() + .then(() => true) + .catch(error => { + if (error.status === 401) { + if (error.message === "Bad credentials") { + throw new Error("Git Gateway Error: Please ask your site administrator to reissue the Git Gateway token."); + } else { + return false; + } + } else { + console.error("Problem fetching repo data from GitHub"); + throw error; + } + }); + } getRequestHeaders(headers = {}) { return this.tokenPromise() diff --git a/src/backends/git-gateway/implementation.js b/src/backends/git-gateway/implementation.js index cd8f029cc17d..447c5324d570 100644 --- a/src/backends/git-gateway/implementation.js +++ b/src/backends/git-gateway/implementation.js @@ -75,7 +75,16 @@ export default class GitGateway extends GitHubBackend { } else { throw new Error("You don't have sufficient permissions to access Netlify CMS"); } - }); + }) + .then(userData => + this.api.hasWriteAccess().then(canWrite => { + if (canWrite) { + return userData; + } else { + throw new Error("You don't have sufficient permissions to access Netlify CMS"); + } + }) + ); } logout() {