improvement(auth): verbose error message when GitHub repo unavailable (#1831)

This commit is contained in:
Shawn Erquhart
2018-11-02 10:29:11 -04:00
committed by GitHub
parent a0cfa1a92f
commit b60c94aa8f
8 changed files with 81 additions and 30 deletions

View File

@ -1,5 +1,6 @@
import trimStart from 'lodash/trimStart';
import semaphore from 'semaphore';
import { stripIndent } from 'common-tags';
import AuthenticationPage from './AuthenticationPage';
import API from './API';
@ -35,7 +36,7 @@ export default class GitHub {
return this.authenticate(user);
}
authenticate(state) {
async authenticate(state) {
this.token = state.token;
this.api = new API({
token: this.token,
@ -45,16 +46,28 @@ export default class GitHub {
squash_merges: this.squash_merges,
initialWorkflowStatus: this.options.initialWorkflowStatus,
});
return this.api.user().then(user =>
this.api.hasWriteAccess().then(isCollab => {
// Unauthorized user
if (!isCollab)
throw new Error('Your GitHub user account does not have access to this repo.');
// Authorized user
user.token = state.token;
return user;
}),
);
const user = await this.api.user();
const isCollab = await this.api.hasWriteAccess().catch(error => {
error.message = stripIndent`
Repo "${this.repo}" not found.
Please ensure the repo information is spelled correctly.
If the repo is private, make sure you're logged into a GitHub account with access.
If your repo is under an organization, ensure the organization has granted access to Netlify
CMS.
`;
throw error;
});
// Unauthorized user
if (!isCollab) {
throw new Error('Your GitHub user account does not have access to this repo.');
}
// Authorized user
return { ...user, token: state.token };
}
logout() {