From 4db2cb89861d47c4cf8682a296bb2e295d14fef6 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Wed, 19 Aug 2020 13:31:07 -0400 Subject: [PATCH] Templatize OAuth buttons across signup and login pages This moves fields into the `OAuthButtons` struct and puts the buttons into templates/includes/oauth.tmpl. --- account.go | 36 +++++++++++------------------------- app.go | 19 ++++--------------- oauth.go | 24 ++++++++++++++++-------- pages/landing.tmpl | 18 ++++-------------- pages/login.tmpl | 27 +-------------------------- templates.go | 10 ++++++++-- templates/include/oauth.tmpl | 28 ++++++++++++++++++++++++++++ 7 files changed, 72 insertions(+), 90 deletions(-) create mode 100644 templates/include/oauth.tmpl diff --git a/account.go b/account.go index 92b1523..236dfb3 100644 --- a/account.go +++ b/account.go @@ -304,32 +304,18 @@ func viewLogin(app *App, w http.ResponseWriter, r *http.Request) error { p := &struct { page.StaticPage - To string - Message template.HTML - Flashes []template.HTML - LoginUsername string - OauthSlack bool - OauthWriteAs bool - OauthGitlab bool - GitlabDisplayName string - OauthGeneric bool - OauthGenericDisplayName string - OauthGitea bool - GiteaDisplayName string + *OAuthButtons + To string + Message template.HTML + Flashes []template.HTML + LoginUsername string }{ - StaticPage: pageForReq(app, r), - To: r.FormValue("to"), - Message: template.HTML(""), - Flashes: []template.HTML{}, - LoginUsername: getTempInfo(app, "login-user", r, w), - OauthSlack: app.Config().SlackOauth.ClientID != "", - OauthWriteAs: app.Config().WriteAsOauth.ClientID != "", - OauthGitlab: app.Config().GitlabOauth.ClientID != "", - GitlabDisplayName: config.OrDefaultString(app.Config().GitlabOauth.DisplayName, gitlabDisplayName), - OauthGeneric: app.Config().GenericOauth.ClientID != "", - OauthGenericDisplayName: config.OrDefaultString(app.Config().GenericOauth.DisplayName, genericOauthDisplayName), - OauthGitea: app.Config().GiteaOauth.ClientID != "", - GiteaDisplayName: config.OrDefaultString(app.Config().GiteaOauth.DisplayName, giteaDisplayName), + StaticPage: pageForReq(app, r), + OAuthButtons: NewOAuthButtons(app.Config()), + To: r.FormValue("to"), + Message: template.HTML(""), + Flashes: []template.HTML{}, + LoginUsername: getTempInfo(app, "login-user", r, w), } if earlyError != "" { diff --git a/app.go b/app.go index 06e677b..2aed437 100644 --- a/app.go +++ b/app.go @@ -238,27 +238,16 @@ func handleViewLanding(app *App, w http.ResponseWriter, r *http.Request) error { p := struct { page.StaticPage + *OAuthButtons Flashes []template.HTML Banner template.HTML Content template.HTML ForcedLanding bool - - OauthSlack bool - OauthWriteAs bool - OauthGitlab bool - OauthGeneric bool - OauthGenericDisplayName string - GitlabDisplayName string }{ - StaticPage: pageForReq(app, r), - ForcedLanding: forceLanding, - OauthSlack: app.Config().SlackOauth.ClientID != "", - OauthWriteAs: app.Config().WriteAsOauth.ClientID != "", - OauthGitlab: app.Config().GitlabOauth.ClientID != "", - OauthGeneric: app.Config().GenericOauth.ClientID != "", - OauthGenericDisplayName: config.OrDefaultString(app.Config().GenericOauth.DisplayName, genericOauthDisplayName), - GitlabDisplayName: config.OrDefaultString(app.Config().GitlabOauth.DisplayName, gitlabDisplayName), + StaticPage: pageForReq(app, r), + OAuthButtons: NewOAuthButtons(app.Config()), + ForcedLanding: forceLanding, } banner, err := getLandingBanner(app) diff --git a/oauth.go b/oauth.go index dbcf3bf..620ffaf 100644 --- a/oauth.go +++ b/oauth.go @@ -30,19 +30,27 @@ import ( // OAuthButtons holds display information for different OAuth providers we support. type OAuthButtons struct { - SlackEnabled bool - WriteAsEnabled bool - GitLabEnabled bool - GitLabDisplayName string + SlackEnabled bool + WriteAsEnabled bool + GitLabEnabled bool + GitLabDisplayName string + GiteaEnabled bool + GiteaDisplayName string + GenericEnabled bool + GenericDisplayName string } // NewOAuthButtons creates a new OAuthButtons struct based on our app configuration. func NewOAuthButtons(cfg *config.Config) *OAuthButtons { return &OAuthButtons{ - SlackEnabled: cfg.SlackOauth.ClientID != "", - WriteAsEnabled: cfg.WriteAsOauth.ClientID != "", - GitLabEnabled: cfg.GitlabOauth.ClientID != "", - GitLabDisplayName: config.OrDefaultString(cfg.GitlabOauth.DisplayName, gitlabDisplayName), + SlackEnabled: cfg.SlackOauth.ClientID != "", + WriteAsEnabled: cfg.WriteAsOauth.ClientID != "", + GitLabEnabled: cfg.GitlabOauth.ClientID != "", + GitLabDisplayName: config.OrDefaultString(cfg.GitlabOauth.DisplayName, gitlabDisplayName), + GiteaEnabled: cfg.GiteaOauth.ClientID != "", + GiteaDisplayName: config.OrDefaultString(cfg.GiteaOauth.DisplayName, giteaDisplayName), + GenericEnabled: cfg.GenericOauth.ClientID != "", + GenericDisplayName: config.OrDefaultString(cfg.GenericOauth.DisplayName, genericOauthDisplayName), } } diff --git a/pages/landing.tmpl b/pages/landing.tmpl index e661aa6..2131b40 100644 --- a/pages/landing.tmpl +++ b/pages/landing.tmpl @@ -60,6 +60,9 @@ form dd { margin-top: 0; max-width: 8em; } +.or { + margin-bottom: 2.5em !important; +} {{end}} {{define "content"}} @@ -73,20 +76,7 @@ form dd { {{ if .OpenRegistration }} - {{ if or .OauthSlack .OauthWriteAs .OauthGitlab .OauthGeneric }} - {{ if .OauthSlack }} -
Sign in with Slack
- {{ end }} - {{ if .OauthWriteAs }} - - {{ end }} - {{ if .OauthGitlab }} - - {{ end }} - {{ if .OauthGeneric }} - - {{ end }} - {{ end }} + {{template "oauth-buttons" .}} {{if not .DisablePasswordAuth}} {{if .Flashes}}{{end}} - {{ if or .OauthSlack .OauthWriteAs .OauthGitlab .OauthGeneric .OauthGitea }} -
- {{ if .OauthSlack }} - Sign in with Slack - {{ end }} - {{ if .OauthWriteAs }} - Sign in with Write.as - {{ end }} - {{ if .OauthGitlab }} - Sign in with {{.GitlabDisplayName}} - {{ end }} - {{ if .OauthGeneric }} - Sign in with {{ .OauthGenericDisplayName }} - {{ end }} - {{ if .OauthGitea }} - Sign in with {{.GiteaDisplayName}} - {{ end }} -
- - {{if not .DisablePasswordAuth}} -
-

or

-
-
- {{end}} - {{ end }} + {{template "oauth-buttons" .}} {{if not .DisablePasswordAuth}}
diff --git a/templates.go b/templates.go index 5ee4bcf..3cef57a 100644 --- a/templates.go +++ b/templates.go @@ -85,12 +85,18 @@ func initPage(parentDir, path, key string) { log.Info(" [%s] %s", key, path) } - pages[key] = template.Must(template.New("").Funcs(funcMap).ParseFiles( + files := []string{ path, filepath.Join(parentDir, templatesDir, "include", "footer.tmpl"), filepath.Join(parentDir, templatesDir, "base.tmpl"), filepath.Join(parentDir, templatesDir, "user", "include", "silenced.tmpl"), - )) + } + + if key == "login.tmpl" || key == "landing.tmpl" { + files = append(files, filepath.Join(parentDir, templatesDir, "include", "oauth.tmpl")) + } + + pages[key] = template.Must(template.New("").Funcs(funcMap).ParseFiles(files...)) } func initUserPage(parentDir, path, key string) { diff --git a/templates/include/oauth.tmpl b/templates/include/oauth.tmpl new file mode 100644 index 0000000..6617fd0 --- /dev/null +++ b/templates/include/oauth.tmpl @@ -0,0 +1,28 @@ +{{define "oauth-buttons"}} + {{ if or .SlackEnabled .WriteAsEnabled .GitLabEnabled .GiteaEnabled .GenericEnabled }} +
+ {{ if .SlackEnabled }} + Sign in with Slack + {{ end }} + {{ if .WriteAsEnabled }} + Sign in with Write.as + {{ end }} + {{ if .GitLabEnabled }} + Sign in with {{.GitLabDisplayName}} + {{ end }} + {{ if .GiteaEnabled }} + Sign in with {{.GiteaDisplayName}} + {{ end }} + {{ if .GenericEnabled }} + Sign in with {{.GenericDisplayName}} + {{ end }} +
+ + {{if not .DisablePasswordAuth}} +
+

or

+
+
+ {{end}} + {{ end }} +{{end}} \ No newline at end of file