@@ -7,9 +7,82 @@ export class AuthManager {
77 constructor ( private context : vscode . ExtensionContext ) { }
88
99 async login ( ) : Promise < void > {
10+ // Show login options
11+ const loginMethod = await vscode . window . showQuickPick ( [
12+ {
13+ label : '🌐 Login with Browser' ,
14+ description : 'Open codegen.com in your browser to authenticate' ,
15+ detail : 'Recommended - secure OAuth flow'
16+ } ,
17+ {
18+ label : '🔑 Enter API Token' ,
19+ description : 'Manually enter your API token' ,
20+ detail : 'For users who already have a token'
21+ }
22+ ] , {
23+ placeHolder : 'Choose how you want to authenticate with Codegen' ,
24+ ignoreFocusOut : true
25+ } ) ;
26+
27+ if ( ! loginMethod ) {
28+ return ;
29+ }
30+
31+ if ( loginMethod . label . includes ( 'Browser' ) ) {
32+ await this . loginWithBrowser ( ) ;
33+ } else {
34+ await this . loginWithToken ( ) ;
35+ }
36+ }
37+
38+ private async loginWithBrowser ( ) : Promise < void > {
39+ try {
40+ // Generate a unique state parameter for security
41+ const state = Math . random ( ) . toString ( 36 ) . substring ( 2 , 15 ) ;
42+
43+ // Construct the OAuth URL
44+ const config = vscode . workspace . getConfiguration ( 'codegen' ) ;
45+ const baseUrl = config . get ( 'webUrl' , 'https://codegen.com' ) ;
46+ const authUrl = `${ baseUrl } /auth/vscode?state=${ state } ` ;
47+
48+ // Open the browser
49+ vscode . env . openExternal ( vscode . Uri . parse ( authUrl ) ) ;
50+
51+ // Show a message to the user
52+ const result = await vscode . window . showInformationMessage (
53+ 'Opening codegen.com in your browser for authentication...' ,
54+ { modal : false } ,
55+ 'I\'ve completed authentication' ,
56+ 'Cancel'
57+ ) ;
58+
59+ if ( result === 'Cancel' ) {
60+ return ;
61+ }
62+
63+ if ( result === 'I\'ve completed authentication' ) {
64+ // Prompt for the token that should now be available
65+ const token = await vscode . window . showInputBox ( {
66+ prompt : 'Paste the API token from your browser' ,
67+ placeHolder : 'The token should be displayed after authentication' ,
68+ password : true ,
69+ ignoreFocusOut : true
70+ } ) ;
71+
72+ if ( token ) {
73+ await this . storeTokenAndValidate ( token ) ;
74+ }
75+ }
76+ } catch ( error ) {
77+ vscode . window . showErrorMessage ( `Browser login failed: ${ error } ` ) ;
78+ throw error ;
79+ }
80+ }
81+
82+ private async loginWithToken ( ) : Promise < void > {
1083 const token = await vscode . window . showInputBox ( {
1184 prompt : 'Enter your Codegen API token' ,
12- placeHolder : 'Your API token from codegen.com' ,
85+ placeHolder : 'Your API token from codegen.com/settings ' ,
1386 password : true ,
1487 ignoreFocusOut : true
1588 } ) ;
@@ -18,6 +91,10 @@ export class AuthManager {
1891 return ;
1992 }
2093
94+ await this . storeTokenAndValidate ( token ) ;
95+ }
96+
97+ private async storeTokenAndValidate ( token : string ) : Promise < void > {
2198 // Store the token securely
2299 await this . context . secrets . store ( AuthManager . TOKEN_KEY , token ) ;
23100
@@ -28,7 +105,7 @@ export class AuthManager {
28105 await this . context . globalState . update ( AuthManager . ORG_ID_KEY , orgId ) ;
29106 }
30107
31- vscode . window . showInformationMessage ( 'Successfully logged in to Codegen!' ) ;
108+ vscode . window . showInformationMessage ( 'Successfully logged in to Codegen! 🎉 ' ) ;
32109 } catch ( error ) {
33110 vscode . window . showErrorMessage ( `Login failed: ${ error } ` ) ;
34111 // Clear the token if login failed
0 commit comments