|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { Args, Command, Flags } from '@oclif/core'; |
| 4 | +import { printHeader, printSuccess, printError, printKV } from '../../utils/format.js'; |
| 5 | +import { writeAuthConfig } from '../../utils/auth-config.js'; |
| 6 | +import { ObjectStackClient } from '@objectstack/client'; |
| 7 | +import * as readline from 'node:readline/promises'; |
| 8 | +import { stdin as input, stdout as output } from 'node:process'; |
| 9 | + |
| 10 | +export default class AuthLogin extends Command { |
| 11 | + static override description = 'Authenticate and store session credentials'; |
| 12 | + |
| 13 | + static override examples = [ |
| 14 | + '$ os auth login', |
| 15 | + '$ os auth login --url https://api.example.com', |
| 16 | + '$ os auth login --email user@example.com --password mypassword', |
| 17 | + ]; |
| 18 | + |
| 19 | + static override flags = { |
| 20 | + url: Flags.string({ |
| 21 | + char: 'u', |
| 22 | + description: 'Server URL', |
| 23 | + default: 'http://localhost:3000', |
| 24 | + env: 'OBJECTSTACK_URL', |
| 25 | + }), |
| 26 | + email: Flags.string({ |
| 27 | + char: 'e', |
| 28 | + description: 'Email address', |
| 29 | + }), |
| 30 | + password: Flags.string({ |
| 31 | + char: 'p', |
| 32 | + description: 'Password', |
| 33 | + }), |
| 34 | + json: Flags.boolean({ |
| 35 | + description: 'Output as JSON', |
| 36 | + }), |
| 37 | + }; |
| 38 | + |
| 39 | + async run(): Promise<void> { |
| 40 | + const { flags } = await this.parse(AuthLogin); |
| 41 | + |
| 42 | + try { |
| 43 | + if (!flags.json) { |
| 44 | + printHeader('ObjectStack Login'); |
| 45 | + printKV('Server', flags.url); |
| 46 | + console.log(''); |
| 47 | + } |
| 48 | + |
| 49 | + // Prompt for credentials if not provided |
| 50 | + let email = flags.email; |
| 51 | + let password = flags.password; |
| 52 | + |
| 53 | + if (!email || !password) { |
| 54 | + const rl = readline.createInterface({ input, output }); |
| 55 | + |
| 56 | + if (!email) { |
| 57 | + email = await rl.question('Email: '); |
| 58 | + } |
| 59 | + |
| 60 | + if (!password) { |
| 61 | + // Note: This doesn't hide the password input in the terminal |
| 62 | + // For production use, consider using a library like 'inquirer' or 'prompts' |
| 63 | + password = await rl.question('Password: '); |
| 64 | + } |
| 65 | + |
| 66 | + rl.close(); |
| 67 | + } |
| 68 | + |
| 69 | + if (!email || !password) { |
| 70 | + throw new Error('Email and password are required'); |
| 71 | + } |
| 72 | + |
| 73 | + // Create client and authenticate |
| 74 | + const client = new ObjectStackClient({ |
| 75 | + baseUrl: flags.url, |
| 76 | + }); |
| 77 | + |
| 78 | + const response = await client.auth.login({ |
| 79 | + email, |
| 80 | + password, |
| 81 | + }); |
| 82 | + |
| 83 | + // Check if login was successful |
| 84 | + if (!response.data?.token && !response.data?.user) { |
| 85 | + throw new Error('Login failed: Invalid response from server'); |
| 86 | + } |
| 87 | + |
| 88 | + // Extract token - it might be in different locations depending on the auth system |
| 89 | + const token = response.data?.token || (response as any).token; |
| 90 | + const user = response.data?.user; |
| 91 | + |
| 92 | + if (!token) { |
| 93 | + throw new Error('Login failed: No token received from server'); |
| 94 | + } |
| 95 | + |
| 96 | + // Store credentials |
| 97 | + await writeAuthConfig({ |
| 98 | + url: flags.url, |
| 99 | + token, |
| 100 | + email: user?.email || email, |
| 101 | + userId: user?.id, |
| 102 | + createdAt: new Date().toISOString(), |
| 103 | + }); |
| 104 | + |
| 105 | + if (flags.json) { |
| 106 | + console.log(JSON.stringify({ |
| 107 | + success: true, |
| 108 | + email: user?.email || email, |
| 109 | + userId: user?.id, |
| 110 | + }, null, 2)); |
| 111 | + } else { |
| 112 | + printSuccess('Authentication successful'); |
| 113 | + printKV('Email', user?.email || email); |
| 114 | + if (user?.id) { |
| 115 | + printKV('User ID', user.id); |
| 116 | + } |
| 117 | + console.log(''); |
| 118 | + console.log(' Credentials stored in ~/.objectstack/credentials.json'); |
| 119 | + console.log(''); |
| 120 | + } |
| 121 | + } catch (error: any) { |
| 122 | + if (flags.json) { |
| 123 | + console.log(JSON.stringify({ |
| 124 | + success: false, |
| 125 | + error: error.message, |
| 126 | + }, null, 2)); |
| 127 | + this.exit(1); |
| 128 | + } |
| 129 | + printError(error.message || String(error)); |
| 130 | + this.exit(1); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments