Skip to content

Commit

Permalink
Mention env var when using API Token and whoami/log(out|in) comma…
Browse files Browse the repository at this point in the history
…nds.
  • Loading branch information
dom96 committed Feb 5, 2024
1 parent e61dba5 commit 66285aa
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .changeset/perfect-berries-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"wrangler": minor
---

feature: whoami, logout and login commands mention the CLOUDFLARE_API_TOKEN env var now

It is easy to get confused when trying to logout while the CLOUDFLARE_API_TOKEN env var is set.
The logout command normally prints out a message which states that the user is not logged in. This
change rectifes this to explicitly call out that the CLOUDFLARE_API_TOKEN is set and requests that
the user unsets it to logout.
20 changes: 20 additions & 0 deletions packages/wrangler/src/user/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,16 @@ export async function loginOrRefreshIfRequired(
export async function login(
props: LoginProps = { browser: true }
): Promise<boolean> {
const authFromEnv = getAuthFromEnv();
if (authFromEnv) {
// Auth from env overrides any login details, so no point in allowing the user to login.
logger.error(
"You are logged in with an API Token. Unset the CLOUDFLARE_API_TOKEN in the " +
"environment to log in via OAuth."
);
return false;
}

logger.log("Attempting to login via OAuth...");
const urlToOpen = await getAuthURL(props?.scopes);
let server: http.Server;
Expand Down Expand Up @@ -1058,6 +1068,16 @@ async function refreshToken(): Promise<boolean> {
}

export async function logout(): Promise<void> {
const authFromEnv = getAuthFromEnv();
if (authFromEnv) {
// Auth from env overrides any login details, so we cannot log out.
logger.log(
"You are logged in with an API Token. Unset the CLOUDFLARE_API_TOKEN in the " +
"environment to log out."
);
return;
}

if (!LocalState.accessToken) {
if (!LocalState.refreshToken) {
logger.log("Not logged in, exiting...");
Expand Down
5 changes: 5 additions & 0 deletions packages/wrangler/src/whoami.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export async function whoami() {
`👋 You are logged in with an ${user.authType}. Unable to retrieve email for this user. Are you missing the \`User->User Details->Read\` permission?`
);
}

if (user.authType === "API Token") {
logger.log("ℹ️ The API Token is read from the CLOUDFLARE_API_TOKEN in your environment.");
}

logger.table(
user.accounts.map((account) => ({
"Account Name": account.name,
Expand Down

0 comments on commit 66285aa

Please sign in to comment.