Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Special-case the Angular port on qs download #194

Merged
merged 3 commits into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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 {
Expand All @@ -357,13 +360,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)
}