From c4b2454b12a6744a26304a42bb81958e918a3177 Mon Sep 17 00:00:00 2001 From: Rita Zerrizuela Date: Fri, 26 Mar 2021 16:13:02 -0300 Subject: [PATCH] Special-case the Angular port on qs download --- internal/cli/logout.go | 1 + internal/cli/quickstarts.go | 44 +++++++++++++++++++++++++------------ 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/internal/cli/logout.go b/internal/cli/logout.go index 988b334a8..4e6883105 100644 --- a/internal/cli/logout.go +++ b/internal/cli/logout.go @@ -12,6 +12,7 @@ func logoutCmd(cli *cli) *cobra.Command { Use: "logout", Short: "Logout of a tenant's session", Long: `auth0 logout `, + Args: cobra.MaximumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { // NOTE(cyx): This was mostly copy/pasted from tenants // use command. Consider refactoring. diff --git a/internal/cli/quickstarts.go b/internal/cli/quickstarts.go index 6bc0f5a61..b177a9a5b 100644 --- a/internal/cli/quickstarts.go +++ b/internal/cli/quickstarts.go @@ -24,11 +24,12 @@ import ( // QuickStart app types and defaults const ( - qsNative = "native" - qsSpa = "spa" - qsWebApp = "webapp" - qsBackend = "backend" - _defaultURL = "http://localhost:3000" + qsNative = "native" + qsSpa = "spa" + qsWebApp = "webapp" + qsBackend = "backend" + qsDefaultURL = "http://localhost" + qspDefaultPort = 3000 ) var ( @@ -145,7 +146,7 @@ func downloadQuickstart(cli *cli) *cobra.Command { cli.renderer.Infof("Quickstart sample sucessfully downloaded at %s", target) qsType := quickstartsTypeFor(client.GetAppType()) - if err := promptDefaultURLs(cmd.Context(), cli, client, qsType); err != nil { + if err := promptDefaultURLs(cli, client, qsType, inputs.Stack); err != nil { return err } return nil @@ -323,11 +324,13 @@ func quickstartsTypeFor(v string) string { // whether the app has already added the default quickstart url to allowed url lists. // If not, it prompts the user to add the default url and updates the application // if they accept. -func promptDefaultURLs(ctx context.Context, cli *cli, client *management.Client, qsType string) error { +func promptDefaultURLs(cli *cli, client *management.Client, qsType string, qsStack string) error { + defaultURL := defaultURLFor(qsStack) + if !strings.EqualFold(qsType, qsSpa) && !strings.EqualFold(qsType, qsWebApp) { return nil } - if containsStr(client.Callbacks, _defaultURL) || containsStr(client.AllowedLogoutURLs, _defaultURL) { + if containsStr(client.Callbacks, defaultURL) || containsStr(client.AllowedLogoutURLs, defaultURL) { return nil } @@ -337,11 +340,11 @@ func promptDefaultURLs(ctx context.Context, cli *cli, client *management.Client, AllowedLogoutURLs: client.AllowedLogoutURLs, } - if confirmed := prompt.Confirm(formatURLPrompt(qsType)); confirmed { - a.Callbacks = append(a.Callbacks, _defaultURL) - a.AllowedLogoutURLs = append(a.AllowedLogoutURLs, _defaultURL) + if confirmed := prompt.Confirm(formatURLPrompt(qsType, defaultURL)); confirmed { + a.Callbacks = append(a.Callbacks, defaultURL) + a.AllowedLogoutURLs = append(a.AllowedLogoutURLs, defaultURL) if strings.EqualFold(qsType, qsSpa) { - a.WebOrigins = append(a.WebOrigins, _defaultURL) + a.WebOrigins = append(a.WebOrigins, defaultURL) } err := ansi.Spinner("Updating application", func() error { @@ -357,7 +360,7 @@ func promptDefaultURLs(ctx context.Context, cli *cli, client *management.Client, // formatURLPrompt creates the correct prompt based on app type for // asking the user if they would like to add default urls. -func formatURLPrompt(qsType string) string { +func formatURLPrompt(qsType string, url string) string { var p strings.Builder p.WriteString("\nQuickstarts use localhost, do you want to add %s to the list of allowed callback URLs") if strings.EqualFold(qsType, qsSpa) { @@ -365,5 +368,18 @@ func formatURLPrompt(qsType string) string { } else { p.WriteString(" and logout URLs?") } - return fmt.Sprintf(p.String(), _defaultURL) + return fmt.Sprintf(p.String(), url) +} + +func defaultURLFor(s string) string { + switch strings.ToLower(s) { + case "angular": // See https://github.com/auth0-samples/auth0-angular-samples/issues/225#issuecomment-806448893 + return defaultURL(qsDefaultURL, 4200) + default: + return defaultURL(qsDefaultURL, qspDefaultPort) + } } + +func defaultURL(url string, port int) string { + return fmt.Sprintf("%s:%d", url, port) +}