@@ -84,26 +84,30 @@ export default class LoginIndex extends Command {
84
84
// Extract the JWT and email from query parameters
85
85
const jwt = url . searchParams . get ( 'jwt' )
86
86
const email = url . searchParams . get ( 'email' )
87
-
88
- if ( jwt && email ) {
87
+ try {
88
+ if ( jwt && email ) {
89
89
// Send response back to browser indicating success
90
- res . writeHead ( 200 , { 'Content-Type' : 'text/html' } )
91
- res . end ( loginHTML )
92
-
93
- // Close the server once JWT and eamail are captured
94
- server . close ( )
90
+ res . writeHead ( 200 , { 'Content-Type' : 'text/html' } )
91
+ res . end ( loginHTML )
95
92
96
- const orgs = await sendGraphQLRequest ( jwt )
97
- const selectedOrg = await promptOrgSelection ( orgs )
98
- // Store JWT and email securely
99
- this . writeToEnvFile ( jwt , email , selectedOrg . id )
93
+ const orgs = await sendGraphQLRequest ( jwt )
94
+ const selectedOrg = await promptOrgSelection ( orgs )
95
+ // Store JWT and email securely
96
+ await this . writeToEnvFile ( jwt , email , selectedOrg . id )
100
97
101
- // Confirm successful login in the CLI
102
- this . log ( 'Successfully logged in as ' + chalk . dim ( email ) + '! 🎉' )
103
- } else {
98
+ // Confirm successful login in the CLI
99
+ this . log ( 'Successfully logged in as ' + chalk . dim ( email ) + '! 🎉' )
100
+ } else {
104
101
// Respond with an error if JWT or email is missing
105
- res . writeHead ( 400 , { 'Content-Type' : 'text/plain' } )
106
- res . end ( 'JWT or email not found in the request.' )
102
+ res . writeHead ( 400 , { 'Content-Type' : 'text/plain' } )
103
+ res . end ( 'JWT or email not found in the request.' )
104
+ }
105
+ } catch {
106
+ // Respond with an error if an error occurs
107
+ res . writeHead ( 500 , { 'Content-Type' : 'text/plain' } )
108
+ res . end ( 'An error occurred during authentication.' )
109
+ } finally {
110
+ server . close ( )
107
111
}
108
112
} )
109
113
0 commit comments