From 5745639bb2bf0bd7f7f4a8d574f2d0a71e29f53e Mon Sep 17 00:00:00 2001 From: Szymon Dziedzic Date: Thu, 18 Jul 2024 11:51:55 +0200 Subject: [PATCH] [ENG-12793][eas-cli] make `eas login` and `eas whoami` behave more clearly when one is authenticated using `EXPO_TOKEN` (#2461) * eas login and eas whoami improvements * apply suggested change * Update packages/eas-cli/src/commands/account/view.ts Co-authored-by: Will Schurman * add changelog * fix changelog --------- Co-authored-by: Will Schurman --- CHANGELOG.md | 4 +++ .../eas-cli/src/commands/account/login.ts | 29 +++++++++++++++++-- packages/eas-cli/src/commands/account/view.ts | 7 ++++- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b579e0e7f6..21f30a6ddc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ This is the log of notable changes to EAS CLI and related packages. ### ๐Ÿงน Chores +- Indicate if a user is logged in using `EXPO_TOKEN` when running `eas whoami` command. ([#2461](https://github.com/expo/eas-cli/pull/2461) by [@szdziedzic](https://github.com/szdziedzic)) +- Throw error when `eas login` command is run with `EXPO_TOKEN` environment variable set. ([#2461](https://github.com/expo/eas-cli/pull/2461) by [@szdziedzic](https://github.com/szdziedzic)) +- Check if user is already logged in when running `eas login` command. ([#2461](https://github.com/expo/eas-cli/pull/2461) by [@szdziedzic](https://github.com/szdziedzic)) + ## [10.2.0](https://github.com/expo/eas-cli/releases/tag/v10.2.0) - 2024-07-15 ### ๐ŸŽ‰ New features diff --git a/packages/eas-cli/src/commands/account/login.ts b/packages/eas-cli/src/commands/account/login.ts index 01525e6d0a..d3c946c1f6 100644 --- a/packages/eas-cli/src/commands/account/login.ts +++ b/packages/eas-cli/src/commands/account/login.ts @@ -1,7 +1,10 @@ -import { Flags } from '@oclif/core'; +import { Errors, Flags } from '@oclif/core'; +import chalk from 'chalk'; import EasCommand from '../../commandUtils/EasCommand'; import Log from '../../log'; +import { confirmAsync } from '../../prompts'; +import { getActorDisplayName } from '../../user/User'; export default class AccountLogin extends EasCommand { static override description = 'log in with your Expo account'; @@ -17,6 +20,7 @@ export default class AccountLogin extends EasCommand { }; static override contextDefinition = { + ...this.ContextOptions.MaybeLoggedIn, ...this.ContextOptions.SessionManagment, }; @@ -25,7 +29,28 @@ export default class AccountLogin extends EasCommand { flags: { sso }, } = await this.parse(AccountLogin); - const { sessionManager } = await this.getContextAsync(AccountLogin, { nonInteractive: false }); + const { + sessionManager, + maybeLoggedIn: { actor }, + } = await this.getContextAsync(AccountLogin, { nonInteractive: false }); + + if (sessionManager.getAccessToken()) { + throw new Error( + 'EXPO_TOKEN is set in your environment, and is being used for all EAS authentication. To use username/password authentication, unset EXPO_TOKEN in your environment and re-run the command.' + ); + } + + if (actor) { + Log.warn(`You are already logged in as ${chalk.bold(getActorDisplayName(actor))}.`); + + const shouldContinue = await confirmAsync({ + message: 'Do you want to continue?', + }); + if (!shouldContinue) { + Errors.error('Aborted', { exit: 1 }); + } + } + await sessionManager.showLoginPromptAsync({ sso }); Log.log('Logged in'); } diff --git a/packages/eas-cli/src/commands/account/view.ts b/packages/eas-cli/src/commands/account/view.ts index 1d9a97d983..0b8dc45027 100644 --- a/packages/eas-cli/src/commands/account/view.ts +++ b/packages/eas-cli/src/commands/account/view.ts @@ -12,14 +12,19 @@ export default class AccountView extends EasCommand { static override contextDefinition = { ...this.ContextOptions.MaybeLoggedIn, + ...this.ContextOptions.SessionManagment, }; async runAsync(): Promise { const { maybeLoggedIn: { actor }, + sessionManager, } = await this.getContextAsync(AccountView, { nonInteractive: true }); if (actor) { - Log.log(chalk.green(getActorDisplayName(actor))); + const loggedInAs = sessionManager.getAccessToken() + ? `${getActorDisplayName(actor)} (authenticated using EXPO_TOKEN)` + : getActorDisplayName(actor); + Log.log(chalk.green(loggedInAs)); // personal account is included, only show if more accounts that personal account // but do show personal account in list if there are more