Skip to content

Commit

Permalink
Cleaning up messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
willvedd authored and sergiught committed Dec 9, 2022
1 parent e08f92d commit 0001874
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
15 changes: 10 additions & 5 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,18 +207,23 @@ func (c *cli) prepareTenant(ctx context.Context) (Tenant, error) {
return Tenant{}, err
}

if t.AccessToken == "" || (scopesChanged(t) && t.authenticatedWithDeviceCodeFlow()) {
return RunLoginAsUser(ctx, c, true)
if scopesChanged(t) && t.authenticatedWithDeviceCodeFlow() {
c.renderer.Warnf("Required scopes have changed. Please sign in to re-authorize the CLI.")
return RunLoginAsUser(ctx, c)
}

if !t.hasExpiredToken() {
if t.AccessToken != "" && !t.hasExpiredToken() {
return t, nil
}

if err := t.regenerateAccessToken(ctx, c); err != nil {
// Ask and guide the user through the login process.
c.renderer.Errorf("failed to renew access token, %s", err)
return RunLoginAsUser(ctx, c, true)
if t.authenticatedWithDeviceCodeFlow() {
c.renderer.Warnf("Failed to renew access token. Please sign in to re-authenticate the CLI.")
return RunLoginAsUser(ctx, c)
}

return t, fmt.Errorf("Failed to renew access token. This may occur if the designated application has been deleted or client secret has been rotated. Please re-authenticate by running `auth0 login --as-machine`")
}

if err := c.addTenant(t); err != nil {
Expand Down
26 changes: 8 additions & 18 deletions internal/cli/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ auth0 login --domain <tenant-domain> --client-id <client-id> --client-secret <cl
return err
}
} else {
if _, err := RunLoginAsUser(ctx, cli, false); err != nil {
cli.renderer.Output(fmt.Sprintf(
"%s\n\n%s\n\n",
"✪ Welcome to the Auth0 CLI 🎊",
"If you don't have an account, please create one here: https://auth0.com/signup.",
))
if _, err := RunLoginAsUser(ctx, cli); err != nil {
return err
}
}
Expand Down Expand Up @@ -95,28 +100,13 @@ auth0 login --domain <tenant-domain> --client-id <client-id> --client-secret <cl

// RunLoginAsUser runs the login flow guiding the user through the process
// by showing the login instructions, opening the browser.
// Use `expired` to run the login from other commands setup:
// this will only affect the messages.
func RunLoginAsUser(ctx context.Context, cli *cli, expired bool) (Tenant, error) {
message := fmt.Sprintf(
"%s\n\n%s\n\n",
"✪ Welcome to the Auth0 CLI 🎊",
"If you don't have an account, please create one here: https://auth0.com/signup.",
)

if expired {
message = "Please sign in to re-authorize the CLI."
cli.renderer.Warnf(message)
} else {
cli.renderer.Output(message)
}

func RunLoginAsUser(ctx context.Context, cli *cli) (Tenant, error) {
state, err := cli.authenticator.Start(ctx)
if err != nil {
return Tenant{}, fmt.Errorf("Failed to start the authentication process: %w.", err)
}

message = fmt.Sprintf("Your device confirmation code is: %s\n\n", ansi.Bold(state.UserCode))
message := fmt.Sprintf("Your device confirmation code is: %s\n\n", ansi.Bold(state.UserCode))
cli.renderer.Output(message)

if cli.noInput {
Expand Down

0 comments on commit 0001874

Please sign in to comment.