Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve login flow #67

Merged
merged 1 commit into from
Dec 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 47 additions & 10 deletions src/commands/login.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { env, Uri } from 'vscode';
import { env, Uri, window } from 'vscode';
import { canRun, disabledForLocal } from '../utils/commands';
import { raiseError, raiseInfo } from '../utils/errors';
import { COMMAND_NAMES } from '../constants';
Expand All @@ -7,8 +7,12 @@ import logger from '../utils/logger';
import globals from '../utils/globals';
import type { RuntimeContext } from '../utils/runtime-context';

let pendingLoginRequest: Awaited<ReturnType<RuntimeContext['authenticator']['login']>> | undefined = undefined;

export function getLoginCommand(context: RuntimeContext) {
return async () => {
abortPendingLogin();

if (!canRun() || disabledForLocal(context, COMMAND_NAMES.LOGIN)) {
return;
}
Expand All @@ -34,31 +38,45 @@ export function getLoginCommand(context: RuntimeContext) {
const method = 'device code';

try {
const loginRequest = await authenticator.login(method);
const loginRequest = pendingLoginRequest = await authenticator.login(method);

if (!loginRequest) {
abortPendingLogin();

trackEvent('command/login', {
status: 'cancelled',
method,
});

return;
}

const handle = loginRequest.handle;

raiseInfo(
const loginPromptResult = await window.showInformationMessage(
`Please open ${handle.verification_uri_complete} and enter the code ${handle.user_code} to login.`,
[{
title: 'Open login page',
callback: () => {
env.openExternal(Uri.parse(handle.verification_uri_complete));
}
}],
{
modal: true,
},
{title:'Open login page'}
);

if (!loginRequest) {
if (!loginPromptResult) {
abortPendingLogin();

trackEvent('command/login', {
status: 'cancelled',
method,
error: 'User cancelled login prompt.'
});

return;
}

// We can't really rely on the return value of 'env.openExternal' since it returns false in any case but 'Open' button
// (e.g. for copy button). So this is not a good indication of whether the user wants to continue or not.
await env.openExternal(Uri.parse(handle.verification_uri_complete));

const user = await loginRequest.onDone;

raiseInfo(`You are now logged in as ${user.email}.`);
Expand All @@ -68,14 +86,33 @@ export function getLoginCommand(context: RuntimeContext) {
method,
});
} catch (err) {
if (err.message === 'polling aborted') {
// This is expected upon cancellation of pending login request.
logger.log('Polling aborted', err);
} else {
logger.error(err);
raiseError(`Failed to login to Monokle Cloud. Please try again. Error: ${err.message}`);

abortPendingLogin();

trackEvent('command/login', {
status: 'failure',
method,
error: err.message,
});
}
}
};
}

function abortPendingLogin() {
try {
if (pendingLoginRequest) {
const handle = pendingLoginRequest.handle;
pendingLoginRequest = undefined;
handle?.abort();
}
} catch (err) {
logger.error(err);
}
}