CLI Login Loop Issue: railway login ↔ railway login --browserless [FIXED]
kryptobaseddev
HOBBYOP

15 days ago

Summary of Issue

I was unable to authenticate with the Railway CLI. The login flow kept looping between:

  • railway login (browser-based auth)

  • railway login --browserless (pairing flow)

No matter which method I used, I would end up with:

Unauthorized. Please login with `railway login`

This resulted in a continuous recursive loop between both login methods.

Environment

  • OS: Linux

  • CLI version before update: older version (prompted to upgrade)

  • CLI version after update: 4.30.3

  • Installed via: npm i -g @railway/cli

Exact Reproduction Steps

Attempt 1: Browser Login

railway login

CLI output:

Open the browser? Yes
⠴ Waiting for login...

Browser opened, but it instructed me to use:

railway login --browserless

Attempt 2: Browserless Login

railway login --browserless

Got pairing code and login URL.

After confirming in browser:

Unauthorized. Please login with `railway login`

This created a back-and-forth loop between both login commands.

Troubleshooting Performed

1. Updated CLI

The CLI indicated:

New version available: v4.30.3

So I upgraded:

npm i -g @railway/cli

Confirmed version:

railway --version
# 4.30.3

However, the Unauthorized loop continued.

2. Checked for Environment Variables

I ran:

env | grep RAILWAY

Result:

RAILWAY_TOKEN=....

This indicated a token was already set.

3. Checked Common Shell Config Locations

I verified there was noRAILWAY_TOKEN export in:

  • ~/.bashrc

  • ~/.bash_profile

  • ~/.profile

  • ~/.zshrc

  • /etc/environment

  • ~/.config/environment.d

  • ~/.railway

None of these contained the token.

4. Checked systemd User Environment

Then I checked:

systemctl --user show-environment | grep RAILWAY

Output showed:

RAILWAY_TOKEN=....

This confirmed the token was being injected via the user systemd session environment.

Root Cause

A stale RAILWAY_TOKEN was set in the user systemd session environment, which overrode the CLI login flow.

Because the token existed, the CLI considered the session authenticated but invalid, causing the Unauthorized loop.

Fix Steps (What Actually Solved It)

Step 1: Remove token from systemd user environment

systemctl --user unset-environment RAILWAY_TOKEN

Step 2: Remove token from current shell session

unset RAILWAY_TOKEN

Step 3: Verify token is completely removed

printenv | grep '^RAILWAY' || true
systemctl --user show-environment | grep '^RAILWAY' || true

Both commands returned nothing.

Step 4: Re-authenticate

railway login --browserless

Then:

  1. Opened the browser login URL

  2. Entered pairing code

  3. Confirmed login

  4. CLI showed: Logged in

Step 5: Confirm Authentication

railway whoami

Successfully returned my user info.

Final Working Flow

unset RAILWAY_TOKEN
systemctl --user unset-environment RAILWAY_TOKEN
railway login --browserless
railway whoami

Notes for Others

If the Unauthorized loop happens even after updating the CLI:

  • Check for RAILWAY_TOKEN in your environment

  • Especially check systemctl --user show-environment

  • If present, remove it with systemctl --user unset-environment RAILWAY_TOKEN

If the variable keeps reappearing, there may be a session startup hook running:

systemctl --user set-environment RAILWAY_TOKEN=...

Solved

2 Replies

kirensrinivasan
PRO

15 days ago

pray emojipray emojipray emojipray emojipray emojipray emoji


15 days ago

In the more recent versions of the CLI, we now tell you to check the token when authentication fails if authentication was attempted with a token, whereas in older versions, as you demonstrated, we just tell you to log in again.


Status changed to Solved brody 15 days ago


Loading...