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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 81 additions and 30 deletions

View File

@ -46,7 +46,14 @@ export default class API {
])(req);
user = () => this.request('/user');
hasWriteAccess = () => this.request(this.repoURL).then(res => res.ok);
hasWriteAccess = async () => {
const response = await this.request(this.repoURL);
if (response.status === 404) {
throw Error('Repo not found');
}
return response.ok;
};
isFile = ({ type }) => type === 'commit_file';
processFile = file => ({

View File

@ -1,5 +1,6 @@
import semaphore from 'semaphore';
import { flow, trimStart } from 'lodash';
import { stripIndent } from 'common-tags';
import {
CURSOR_COMPATIBILITY_SYMBOL,
filterByPropExtension,
@ -61,7 +62,7 @@ export default class Bitbucket {
return this.authenticate(user);
}
authenticate(state) {
async authenticate(state) {
this.token = state.token;
this.refreshToken = state.refresh_token;
this.api = new API({
@ -71,13 +72,25 @@ export default class Bitbucket {
api_root: this.api_root,
});
return this.api.user().then(user =>
this.api.hasWriteAccess(user).then(isCollab => {
if (!isCollab)
throw new Error('Your BitBucker user account does not have access to this repo.');
return Object.assign({}, user, { token: state.token, refresh_token: state.refresh_token });
}),
);
const user = await this.api.user();
const isCollab = await this.api.hasWriteAccess(user).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 Bitbucket account with access.
`;
throw error;
});
// Unauthorized user
if (!isCollab) {
throw new Error('Your BitBucket user account does not have access to this repo.');
}
// Autorized user
return { ...user, token: state.token, refresh_token: state.refresh_token };
}
getRefreshedAccessToken() {

View File

@ -19,6 +19,7 @@
"build": "cross-env NODE_ENV=production webpack"
},
"dependencies": {
"common-tags": "^1.8.0",
"js-base64": "^2.4.8",
"semaphore": "^1.1.0"
},

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() {

View File

@ -1,5 +1,6 @@
import trimStart from 'lodash/trimStart';
import semaphore from 'semaphore';
import { stripIndent } from 'common-tags';
import { CURSOR_COMPATIBILITY_SYMBOL } from 'netlify-cms-lib-util';
import AuthenticationPage from './AuthenticationPage';
import API from './API';
@ -39,7 +40,7 @@ export default class GitLab {
return this.authenticate(user);
}
authenticate(state) {
async authenticate(state) {
this.token = state.token;
this.api = new API({
token: this.token,
@ -47,15 +48,25 @@ export default class GitLab {
repo: this.repo,
api_root: this.api_root,
});
return this.api.user().then(user =>
this.api.hasWriteAccess(user).then(isCollab => {
// Unauthorized user
if (!isCollab)
throw new Error('Your GitLab user account does not have access to this repo.');
// Authorized user
return Object.assign({}, user, { token: state.token });
}),
);
const user = await this.api.user();
const isCollab = await this.api.hasWriteAccess(user).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 GitLab account with access.
`;
throw error;
});
// Unauthorized user
if (!isCollab) {
throw new Error('Your GitLab user account does not have access to this repo.');
}
// Authorized user
return { ...user, token: state.token };
}
logout() {

View File

@ -76,6 +76,7 @@ export function loginUser(credentials) {
dispatch(authenticate(user));
})
.catch(error => {
console.error(error);
dispatch(
notifSend({
message: `${error.message}`,

View File

@ -9,6 +9,7 @@ injectGlobal`
.notif__container {
z-index: 10000;
white-space: pre-wrap;
}
`;

View File

@ -2618,6 +2618,10 @@ common-tags@1.4.0:
dependencies:
babel-runtime "^6.18.0"
common-tags@^1.8.0:
version "1.8.0"
resolved "https://registry.yarnpkg.com/common-tags/-/common-tags-1.8.0.tgz#8e3153e542d4a39e9b10554434afaaf98956a937"
commondir@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/commondir/-/commondir-1.0.1.tgz#ddd800da0c66127393cca5950ea968a3aaf1253b"