@@ -57,17 +57,11 @@ func apiKeyLogin(cfg *config.AuthLogin, out io.Writer) error {
5757 defer spin .Stop ()
5858
5959 // resolve the org from the api key
60- res , err := cfg . Client . Orgs . GetOrgName ( & orgs. GetOrgNameParams {}, nil )
60+ org , err := resolveOrg ( cfg )
6161 if err != nil {
6262 spin .StopFail ()
6363 return err
6464 }
65- orgInfo := res .Payload
66- if len (orgInfo .Orgs ) == 0 {
67- spin .StopFail ()
68- return errors .New ("could not resolve org from API key" )
69- }
70- org := orgInfo .Orgs [0 ]
7165
7266 // store the auth info
7367 err = auth .StoreAuthInKeyring (& auth.Auth {
@@ -99,11 +93,22 @@ func bearerTokenLogin(cfg *config.AuthLogin, out io.Writer) error {
9993 return err
10094 }
10195
96+ // init the API client with the provided bearer token
97+ if err := cfg .InitAPIConfigWithBearerToken (token .AccessToken ); err != nil {
98+ return err
99+ }
100+
101+ // resolve the org from the bearer token
102+ org , err := resolveOrg (cfg )
103+ if err != nil {
104+ return err
105+ }
106+
102107 // store the auth info
103108 expiresAt := time .Now ().Add (time .Duration (token .ExpiresIn ) * time .Second )
104109 err = auth .StoreAuthInKeyring (& auth.Auth {
105110 BearerToken : token .AccessToken ,
106- OrgName : token . OrgName ,
111+ OrgName : org . Name ,
107112 ExpiresAt : & expiresAt ,
108113 })
109114 if err != nil {
@@ -153,3 +158,15 @@ func waitForUserAuth(cfg *config.AuthLogin, out io.Writer,
153158 }
154159 }
155160}
161+
162+ func resolveOrg (cfg * config.AuthLogin ) (* models.OrgsOrg , error ) {
163+ res , err := cfg .Client .Orgs .GetOrgName (& orgs.GetOrgNameParams {}, nil )
164+ if err != nil {
165+ return nil , err
166+ }
167+ orgInfo := res .Payload
168+ if len (orgInfo .Orgs ) == 0 {
169+ return nil , errors .New ("could not resolve orgs" )
170+ }
171+ return orgInfo .Orgs [0 ], nil
172+ }
0 commit comments