#!/usr/bin/env bun
import { ConfigManager } from './config/environment.js';
import { TokenManager } from './auth/token-manager.js';
import { OAuthServer } from './auth/oauth-server.js';
async function authenticate() {
console.log('šŖ Kite API Authentication');
console.log('==========================');
try {
// Initialize dependencies
const config = new ConfigManager();
const tokenManager = new TokenManager();
const oauthServer = new OAuthServer(config, tokenManager);
// Check if already authenticated
if (tokenManager.isTokenValid()) {
const tokenData = tokenManager.getValidTokenData();
console.log('ā
Already authenticated!');
console.log(`š¤ User: ${tokenData?.user_name || tokenData?.user_id}`);
console.log(`š
Token generated: ${tokenData?.generated_at}`);
console.log('');
console.log('If you want to re-authenticate, delete the access_token.json file first.');
return;
}
// Start OAuth flow
console.log(`š Starting OAuth server on port ${config.getOAuthPort()}`);
console.log('');
const loginUrl = oauthServer.getLoginUrl();
console.log('š Authentication Steps:');
console.log('1. Open the following URL in your browser:');
console.log('');
console.log(` ${loginUrl}`);
console.log('');
console.log('2. Complete authentication with your Zerodha credentials');
console.log('3. You will be redirected back and the token will be saved automatically');
console.log('');
console.log('ā³ Waiting for authentication...');
console.log(' (Press Ctrl+C to cancel)');
// Wait for authentication
const tokenData = await oauthServer.startAuthFlow();
console.log('');
console.log('ā
Authentication successful!');
console.log(`š¤ Welcome, ${tokenData.user_name || tokenData.user_id}!`);
console.log(`š¾ Token saved to: ${tokenManager.getTokenFilePath()}`);
console.log('');
console.log('š You can now use the Kite MCP server!');
console.log(' Start with: bun start');
} catch (error) {
console.error('');
console.error('ā Authentication failed:');
console.error(' ', error instanceof Error ? error.message : String(error));
console.error('');
console.error('š” Troubleshooting:');
console.error(' - Check your API_KEY and API_SECRET in .env file');
console.error(' - Make sure your redirect URL is correctly configured in Kite Connect app');
console.error(' - Ensure you complete the authentication within 5 minutes');
process.exit(1);
}
}
// Handle graceful shutdown
process.on('SIGINT', () => {
console.log('\nš Authentication cancelled by user');
process.exit(0);
});
process.on('SIGTERM', () => {
console.log('\nš Authentication terminated');
process.exit(0);
});
// Run authentication
authenticate().catch((error) => {
console.error('Unexpected error during authentication:', error);
process.exit(1);
});