Skip to content

Commit

Permalink
Cloud authentication errors (#635)
Browse files Browse the repository at this point in the history
* Errors and authenticating should no longer take the window focus

* Clear errors and catch errors from GitHub authentication

* Rethrow errors when authenticating
  • Loading branch information
kraenhansen authored Jan 22, 2018
1 parent b9eb5f0 commit a0bba9f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 39 deletions.
49 changes: 20 additions & 29 deletions src/main/CloudManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,37 +78,26 @@ export class CloudManager {

public async authenticateWithGitHub() {
const endpoint = raas.getEndpoint();
try {
this.sendCloudStatus({
kind: 'authenticating',
waitingForUser: true,
endpoint,
});
const code = await github.authenticate();
this.sendCloudStatus({
kind: 'authenticating',
waitingForUser: false,
endpoint,
});
const response = await timeout<raas.user.IRaasAuthenticationResponse>(
CloudManager.AUTHENTICATION_TIMEOUT,
new Error(
`Request timed out (waited ${CloudManager.AUTHENTICATION_TIMEOUT} ms)`,
),
raas.user.authenticateWithGitHub(code),
);
raas.user.setToken(response.token);
this.refresh(true);
} catch (err) {
this.sendCloudStatus({
kind: 'error',
message: err.message,
endpoint,
});
}
this.sendCloudStatus({
kind: 'authenticating',
waitingForUser: true,
endpoint,
});
const code = await github.authenticate();
return this.authenticate(() => {
return raas.user.authenticateWithGitHub(code);
});
}

public async authenticateWithEmail(email: string, password: string) {
return this.authenticate(() => {
return raas.user.authenticateWithEmail(email, password);
});
}

public async authenticate(
performAuthentication: () => Promise<raas.user.IRaasAuthenticationResponse>,
) {
const endpoint = raas.getEndpoint();
try {
this.sendCloudStatus({
Expand All @@ -121,7 +110,7 @@ export class CloudManager {
new Error(
`Request timed out (waited ${CloudManager.AUTHENTICATION_TIMEOUT} ms)`,
),
raas.user.authenticateWithEmail(email, password),
performAuthentication(),
);
raas.user.setToken(response.token);
this.refresh(true);
Expand All @@ -131,6 +120,8 @@ export class CloudManager {
message: err.message,
endpoint,
});
// Allow callers to catch this error too, by rethrowing
throw err;
}
}

Expand Down
16 changes: 10 additions & 6 deletions src/ui/CloudAuthentication/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class CloudAuthenticationContainer extends React.Component<

protected onAuthenticateWithEmail = async () => {
try {
this.setState({ isLoading: true });
this.setState({ isLoading: true, error: undefined });
await main.authenticateWithEmail(this.state.email, this.state.password);
this.setState({ isLoading: false });
// Close this window ..
Expand All @@ -54,11 +54,15 @@ class CloudAuthenticationContainer extends React.Component<
};

protected onAuthenticateWithGitHub = async () => {
this.setState({ isLoading: true });
await main.authenticateWithGitHub();
this.setState({ isLoading: false });
// Close this window ..
window.close();
try {
this.setState({ isLoading: true, error: undefined });
await main.authenticateWithGitHub();
this.setState({ isLoading: false });
// Close this window ..
window.close();
} catch (err) {
this.setState({ error: err, isLoading: false });
}
};

protected onEmailChange = (email: string) => {
Expand Down
5 changes: 1 addition & 4 deletions src/ui/greeting/GreetingContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,7 @@ export class GreetingContainer extends React.Component<
status: ICloudStatus,
) => {
// Focus the window when the status requires the users attention
if (
status.kind === 'error' ||
(status.kind === 'authenticating' && !status.waitingForUser)
) {
if (status.kind === 'authenticated') {
electron.remote.getCurrentWindow().focus();
}
// Update the cloud status
Expand Down

0 comments on commit a0bba9f

Please sign in to comment.