Skip to content

Commit a1eb17f

Browse files
author
Manus AI
committed
feat: Add generate-token command for creating/regenerating registry tokens
1 parent c1facb8 commit a1eb17f

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

index.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,87 @@ async function verifyCommand(options) {
937937
}
938938
}
939939

940+
// Command to generate or regenerate registry token
941+
async function generateTokenCommand(options) {
942+
console.log('\n🔑 Generate Registry Token\n');
943+
944+
try {
945+
let email = options.email;
946+
let password = options.password;
947+
const host = options.host || 'https://api.fleetbase.io';
948+
949+
// Prompt for credentials if not provided
950+
if (!email || !password) {
951+
const answers = await prompt([
952+
{
953+
type: 'input',
954+
name: 'email',
955+
message: 'Email address:',
956+
initial: email,
957+
skip: () => !!email,
958+
validate: (value) => value ? true : 'Email is required'
959+
},
960+
{
961+
type: 'password',
962+
name: 'password',
963+
message: 'Password:',
964+
skip: () => !!password,
965+
validate: (value) => value ? true : 'Password is required'
966+
}
967+
]);
968+
email = email || answers.email;
969+
password = password || answers.password;
970+
}
971+
972+
// Ensure host has protocol
973+
const apiHost = host.startsWith('http://') || host.startsWith('https://')
974+
? host
975+
: `https://${host}`;
976+
const generateTokenApi = `${apiHost}/~registry/v1/developer-account/generate-token`;
977+
978+
console.log('\nGenerating token...');
979+
980+
// Make API call to generate token
981+
const response = await axios.post(generateTokenApi, {
982+
email: email,
983+
password: password
984+
});
985+
986+
if (response.data.status === 'success') {
987+
console.log('\n✓ ' + response.data.message);
988+
console.log('\n🔑 Your Registry Token:');
989+
console.log(` ${response.data.token}`);
990+
console.log('\n💡 Save this token securely! You\'ll need it to authenticate with the registry.');
991+
console.log(' Use: flb set-auth ' + response.data.token + (host !== 'https://api.fleetbase.io' ? ` --host ${host}` : ''));
992+
console.log('\n⚠️ Note: This replaces any previously generated token.');
993+
} else {
994+
console.error('\nToken generation failed:', response.data.message || 'Unknown error');
995+
process.exit(1);
996+
}
997+
} catch (error) {
998+
if (error.response) {
999+
const errorData = error.response.data;
1000+
1001+
// Handle different error response formats
1002+
let errorMessage = 'Unknown error';
1003+
if (errorData.message) {
1004+
errorMessage = errorData.message;
1005+
} else if (errorData.error) {
1006+
errorMessage = errorData.error;
1007+
} else if (errorData.errors && Array.isArray(errorData.errors)) {
1008+
errorMessage = errorData.errors.join(', ');
1009+
}
1010+
1011+
console.error('\nToken generation failed:', errorMessage);
1012+
} else if (error.request) {
1013+
console.error('\nToken generation failed: No response from server');
1014+
} else {
1015+
console.error('\nToken generation failed:', error.message);
1016+
}
1017+
process.exit(1);
1018+
}
1019+
}
1020+
9401021
// Command to resend verification code
9411022
async function resendVerificationCommand(options) {
9421023
console.log('\n📧 Resend Verification Code\n');
@@ -1386,6 +1467,14 @@ program
13861467
.option('-h, --host <host>', 'API host with protocol (default: https://api.fleetbase.io)')
13871468
.action(resendVerificationCommand);
13881469

1470+
program
1471+
.command('generate-token')
1472+
.description('Generate or regenerate your registry authentication token')
1473+
.option('-e, --email <email>', 'Email address')
1474+
.option('-p, --password <password>', 'Password')
1475+
.option('-h, --host <host>', 'API host with protocol (default: https://api.fleetbase.io)')
1476+
.action(generateTokenCommand);
1477+
13891478
program
13901479
.command('install-fleetbase')
13911480
.description('Install Fleetbase using Docker')

0 commit comments

Comments
 (0)