@@ -39,23 +39,23 @@ export const getApp = async () => {
3939 app . post ( '/token' , async ( req , res ) => {
4040 try {
4141 Debug ( req ) ;
42- if ( isLoading ) {
42+ if ( isLoading ) {
4343 return res . status ( 429 ) . json ( { message : 'Another request is in progress. Please try again later.' } ) ;
4444 }
45- isLoading = true ;
46-
47- if ( Auth . wantToChangeOrganization ) {
45+ isLoading = true ;
46+
47+ if ( Auth . wantToChangeOrganization ) {
4848 ConfigStore . delete ( CONFIG_KEYS . AUTH_TOKEN ) ;
4949 clearExtensionContext ( ) ;
5050 }
5151 const expiryTimestamp =
5252 Math . floor ( Date . now ( ) / 1000 ) + req . body . auth_token . expires_in ;
5353 req . body . auth_token . expiry_time = expiryTimestamp ;
54- if ( Auth . newDomainToUpdate ) {
55- if ( Auth . newDomainToUpdate === 'api.fynd.com' ) {
54+ if ( Auth . newDomainToUpdate ) {
55+ if ( Auth . newDomainToUpdate === 'api.fynd.com' ) {
5656 Env . setEnv ( Auth . newDomainToUpdate ) ;
5757 }
58- else {
58+ else {
5959 await Env . setNewEnvs ( Auth . newDomainToUpdate ) ;
6060 }
6161 }
@@ -77,15 +77,15 @@ export const getApp = async () => {
7777 Auth . stopSever ( ) ;
7878 res . status ( 500 ) . json ( { message : 'failed' } ) ;
7979 }
80- finally {
81- isLoading = false ;
80+ finally {
81+ isLoading = false ;
8282 }
8383 } ) ;
8484
8585 return { app } ;
8686} ;
8787
88- function startTimer ( ) {
88+ function startTimer ( ) {
8989 Debug ( "Server timer starts" )
9090 Auth . timer_id = setTimeout ( ( ) => {
9191 Auth . stopSever ( ( ) => {
@@ -94,20 +94,20 @@ function startTimer(){
9494 } , SERVER_TIMER )
9595}
9696
97- function resetTimer ( ) {
97+ function resetTimer ( ) {
9898 if ( Auth . timer_id ) {
9999 Debug ( "Server timer stoped" )
100100 clearTimeout ( Auth . timer_id )
101101 Auth . timer_id = null ;
102102 }
103103}
104104
105- function clearExtensionContext ( ) {
105+ function clearExtensionContext ( ) {
106106 const extensionContext = new ExtensionContext ( ) ;
107107 extensionContext . deleteAll ( ) ;
108108}
109109
110- export const startServer = async ( port :number ) => {
110+ export const startServer = async ( port : number ) => {
111111 if ( Auth . server ) return Auth . server ;
112112
113113 const { app } = await getApp ( ) ;
@@ -144,21 +144,21 @@ export default class Auth {
144144 static timer_id ;
145145 static wantToChangeOrganization = false ;
146146 static newDomainToUpdate = null ;
147- constructor ( ) { }
147+ constructor ( ) { }
148148 public static async login ( options ) {
149149
150150 let env : string ;
151- const port = await getRandomFreePort ( [ ] ) ;
152- if ( options . host ) {
151+ const port = await getRandomFreePort ( [ ] ) ;
152+ if ( options . host ) {
153153 env = await Env . verifyAndSanitizeEnvValue ( options . host ) ;
154154 }
155- else {
155+ else {
156156 env = 'api.fynd.com' ;
157157 }
158158
159159 let current_env = Env . getEnvValue ( ) ;
160160
161- if ( current_env !== env ) {
161+ if ( current_env !== env ) {
162162 // update new domain after login
163163 Auth . newDomainToUpdate = env ;
164164
@@ -189,30 +189,27 @@ export default class Auth {
189189 await startServer ( port ) ;
190190 }
191191 } ) ;
192- } else
192+ } else
193193 await startServer ( port ) ;
194194 try {
195195 let domain = null ;
196196 let partnerDomain = env . replace ( 'api' , 'partners' ) ;
197197 domain = `https://${ partnerDomain } ` ;
198+ const region = options . region ?. trim ( ) ;
199+ const callbackUrl = `${ getLocalBaseUrl ( ) } :${ port } ` ;
200+ const queryParams = new URLSearchParams ( { 'fdk-cli' : 'true' , callback : callbackUrl } ) ;
201+ if ( region ) queryParams . set ( 'region' , region ) ;
202+ const authUrl = `${ domain } /organizations/?${ queryParams . toString ( ) } ` ;
198203 try {
199204 if ( Auth . wantToChangeOrganization || ! isLoggedIn ) {
200- await open (
201- `${ domain } /organizations/?fdk-cli=true&callback=${ encodeURIComponent (
202- `${ getLocalBaseUrl ( ) } :${ port } ` ,
203- ) } `,
204- ) ;
205+ await open ( authUrl ) ;
205206 console . log (
206- `Open link on browser: ${ OutputFormatter . link ( `${ domain } /organizations/?fdk-cli=true&callback=${ encodeURIComponent (
207- `${ getLocalBaseUrl ( ) } :${ port } ` ,
208- ) } `) } `,
207+ `Open link on browser: ${ OutputFormatter . link ( authUrl ) } ` ,
209208 ) ;
210209 }
211210 } catch ( err ) {
212211 console . log (
213- `Open link on browser: ${ OutputFormatter . link ( `${ domain } /organizations/?fdk-cli=true&callback=${ encodeURIComponent (
214- `${ getLocalBaseUrl ( ) } :${ port } ` ,
215- ) } `) } `,
212+ `Open link on browser: ${ OutputFormatter . link ( authUrl ) } ` ,
216213 ) ;
217214 }
218215 } catch ( error ) {
@@ -240,7 +237,7 @@ export default class Auth {
240237 }
241238 }
242239
243- private static updateConfigStoreForLogout ( ) {
240+ private static updateConfigStoreForLogout ( ) {
244241 const currentEnv = ConfigStore . get (
245242 CONFIG_KEYS . CURRENT_ENV_VALUE ,
246243 ) ;
@@ -258,9 +255,8 @@ export default class Auth {
258255 const activeEmail =
259256 user . emails . find ( ( e ) => e . active && e . primary ) ?. email ||
260257 'Primary email missing' ;
261- const text = `Name: ${ user . first_name } ${
262- user . last_name
263- } \nEmail: ${ activeEmail } \nOrganization: ${ getOrganizationDisplayName ( ) } `;
258+ const text = `Name: ${ user . first_name } ${ user . last_name
259+ } \nEmail: ${ activeEmail } \nOrganization: ${ getOrganizationDisplayName ( ) } `;
264260 Logger . info ( successBox ( { text } ) ) ;
265261 } catch ( error ) {
266262 throw new CommandError ( error . message , error . code ) ;
0 commit comments