Skip to content
This repository was archived by the owner on Jan 19, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions src/commands/login/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ describe('loginCommand', () => {
const mockUser = { email: 'user@example.com' };
vi.mocked(loginWithToken).mockResolvedValue({ user: mockUser });

await loginCommand.parseAsync(['node', 'test']);
await loginCommand.parseAsync(['node', 'test', '--region', 'eu']);

expect(password).toHaveBeenCalledWith(expect.objectContaining({
message: 'Please enter your token:',
Expand All @@ -183,11 +183,11 @@ describe('loginCommand', () => {
const mockUser = { email: 'test@example.com', friendly_name: 'Test User' };
vi.mocked(loginWithToken).mockResolvedValue({ user: mockUser });

await loginCommand.parseAsync(['node', 'test', '--token', mockToken]);
await loginCommand.parseAsync(['node', 'test', '--token', mockToken, '--region', 'eu']);

expect(loginWithToken).toHaveBeenCalledWith(mockToken, 'eu');

expect(konsola.ok).toHaveBeenCalledWith('Successfully logged in. Welcome Test User.', true);
expect(konsola.ok).toHaveBeenCalledWith('Successfully logged in to region Europe (eu). Welcome Test User.', true);
});

it('should login with a valid token in another region --region', async () => {
Expand All @@ -199,7 +199,7 @@ describe('loginCommand', () => {

expect(loginWithToken).toHaveBeenCalledWith(mockToken, 'us');

expect(konsola.ok).toHaveBeenCalledWith('Successfully logged in. Welcome Test User.', true);
expect(konsola.ok).toHaveBeenCalledWith('Successfully logged in to region United States (us). Welcome Test User.', true);
});

it('should throw an error for an invalid token', async () => {
Expand Down
57 changes: 37 additions & 20 deletions src/commands/login/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export const loginCommand = program
.option(
'-r, --region <region>',
`The region you would like to work in. Please keep in mind that the region must match the region of your space. This region flag will be used for the other cli's commands. You can use the values: ${allRegionsText}.`,
regions.EU,
)
.action(async (options: {
token: string;
Expand All @@ -46,10 +45,6 @@ export const loginCommand = program
// Command options
const { token, region } = options;

if (!isRegion(region)) {
handleError(new CommandError(`The provided region: ${region} is not valid. Please use one of the following values: ${Object.values(regions).join(' | ')}`));
}

const { state, updateSession, persistCredentials, initializeSession } = session();

await initializeSession();
Expand All @@ -59,17 +54,34 @@ export const loginCommand = program
return;
}

if (region && !isRegion(region)) {
handleError(new CommandError(`The provided region: ${region} is not valid. Please use one of the following values: ${Object.values(regions).join(' | ')}`));
return;
}

if (token) {
const spinner = new Spinner({
verbose: !isVitest,
}).start(`Logging in with token`);
});
try {
const { user } = await loginWithToken(token, region);
updateSession(user.email, token, region);
await persistCredentials(region);
let userRegion = region;
if (!userRegion) {
userRegion = await select({
message: 'Please select the region you would like to work in:',
choices: Object.values(regions).map((region: RegionCode) => ({
name: regionNames[region],
value: region,
})),
default: regions.EU,
});
}
spinner.start(`Logging in with token`);
const { user } = await loginWithToken(token, userRegion);
updateSession(user.email, token, userRegion);
await persistCredentials(userRegion);
spinner.succeed();

konsola.ok(`Successfully logged in. Welcome ${chalk.hex(colorPalette.PRIMARY)(user.friendly_name)}.`, true);
konsola.ok(`Successfully logged in to region ${chalk.hex(colorPalette.PRIMARY)(`${regionNames[userRegion]} (${userRegion})`)}. Welcome ${chalk.hex(colorPalette.PRIMARY)(user.friendly_name)}.`, true);
}
catch (error) {
spinner.failed();
Expand All @@ -96,7 +108,7 @@ export const loginCommand = program
updateSession(user.email, userToken, region);
await persistCredentials(region);

konsola.ok(`Successfully logged in. Welcome ${chalk.hex(colorPalette.PRIMARY)(user.friendly_name)}.`, true);
konsola.ok(`Successfully logged in to region ${chalk.hex(colorPalette.PRIMARY)(`${regionNames[region]} (${region})`)}. Welcome ${chalk.hex(colorPalette.PRIMARY)(user.friendly_name)}.`, true);
}

else {
Expand All @@ -111,14 +123,19 @@ export const loginCommand = program
const userPassword = await password({
message: 'Please enter your password:',
});
const userRegion = await select({
message: 'Please select the region you would like to work in:',
choices: Object.values(regions).map((region: RegionCode) => ({
name: regionNames[region],
value: region,
})),
default: regions.EU,
});

let userRegion = region;
if (!userRegion) {
userRegion = await select({
message: 'Please select the region you would like to work in:',
choices: Object.values(regions).map((region: RegionCode) => ({
name: regionNames[region],
value: region,
})),
default: regions.EU,
});
}

spinner.start(`Logging in with email`);
spinner.succeed();
const response = await loginWithEmailAndPassword(userEmail, userPassword, userRegion);
Expand All @@ -139,7 +156,7 @@ export const loginCommand = program
}
await persistCredentials(region);

konsola.ok(`Successfully logged in. Welcome ${chalk.hex(colorPalette.PRIMARY)(userEmail)}.`, true);
konsola.ok(`Successfully logged in to region ${chalk.hex(colorPalette.PRIMARY)(`${regionNames[userRegion]} (${userRegion})`)}. Welcome ${chalk.hex(colorPalette.PRIMARY)(userEmail)}.`, true);
}
}
catch (error) {
Expand Down
Loading