-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathauth.js
More file actions
119 lines (107 loc) · 2.91 KB
/
auth.js
File metadata and controls
119 lines (107 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import process from 'node:process';
import {execa} from 'execa';
import {
setAuthMode,
setToken,
clearAll,
getAllConfig,
getConfigPath,
} from '../utils/config-manager.js';
import {
showSuccess,
showError,
showWarning,
showAuthStatus,
} from '../utils/ui.js';
/**
* Authentication commands
* Handles GitHub Copilot and OpenAI authentication
*/
export async function authenticateWithCopilot() {
try {
console.log('🔐 Setting up GitHub Copilot authentication...\n');
// Check if gh CLI is installed
try {
await execa('gh', ['--version']);
} catch {
showError('GitHub CLI (gh) is not installed.');
console.log('');
console.log('Install it from: https://cli.github.com/');
console.log('Then run: gh auth login');
process.exit(1);
}
// Check if gh is authenticated
try {
await execa('gh', ['auth', 'status']);
} catch {
showError('GitHub CLI is not authenticated.');
console.log('');
console.log('Please authenticate first:');
console.log(' gh auth login');
console.log('');
console.log('Make sure you have GitHub Copilot enabled on your account.');
process.exit(1);
}
// Store auth mode
setAuthMode('copilot');
showSuccess('GitHub Copilot ready!');
console.log('');
console.log('✅ GitHub CLI authenticated');
console.log('✅ Copilot SDK will use your CLI session');
console.log('');
console.log('You can now use: magicc commit');
} catch (error) {
showError(`Setup failed: ${error.message}`);
process.exit(1);
}
}
export async function authenticateWithOpenAI(apiKey) {
try {
if (!apiKey) {
showError('OpenAI API key is required.');
console.log('');
console.log('Usage: magicc auth openai <your-api-key>');
console.log('');
console.log('To get an API key:');
console.log(' Visit: https://platform.openai.com/account/api-keys');
process.exit(1);
}
// Validate the API key format
if (!apiKey.startsWith('sk-')) {
showWarning('API key should start with "sk-". Please verify your key.');
}
// Store the key
setToken('openai', apiKey);
setAuthMode('openai');
showSuccess('OpenAI authentication successful!');
console.log('');
console.log(`📁 Config stored at: ${getConfigPath()}`);
console.log('');
console.log(
'💡 Consider switching to GitHub Copilot for better integration:',
);
console.log(' magicc auth copilot');
} catch (error) {
showError(`Authentication failed: ${error.message}`);
process.exit(1);
}
}
export function showAuthStatusCommand() {
const config = getAllConfig();
showAuthStatus(config);
console.log(`📁 Config: ${getConfigPath()}`);
console.log();
}
export function logout() {
try {
clearAll();
showSuccess('All credentials cleared.');
console.log('');
console.log('To authenticate again, use:');
console.log(' magicc auth copilot');
console.log(' magicc auth openai <key>');
} catch (error) {
showError(`Logout failed: ${error.message}`);
process.exit(1);
}
}