Skip to content

Commit

Permalink
Special-case the Angular port on qs download (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
Widcket authored Mar 26, 2021
1 parent 5cbf783 commit 770f1aa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
1 change: 1 addition & 0 deletions internal/cli/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func logoutCmd(cli *cli) *cobra.Command {
Use: "logout",
Short: "Logout of a tenant's session",
Long: `auth0 logout <tenant>`,
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.
Expand Down
44 changes: 30 additions & 14 deletions internal/cli/quickstarts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -148,7 +149,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
Expand Down Expand Up @@ -325,11 +326,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
}

Expand All @@ -339,11 +342,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 {
Expand All @@ -359,13 +362,26 @@ 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) {
p.WriteString(", logout URLs, and web origins?")
} 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)
}

0 comments on commit 770f1aa

Please sign in to comment.