Templatize OAuth buttons across signup and login pages

This moves fields into the `OAuthButtons` struct and puts the buttons into templates/includes/oauth.tmpl.
This commit is contained in:
Matt Baer 2020-08-19 13:31:07 -04:00
parent a773d94dc7
commit 4db2cb8986
7 changed files with 72 additions and 90 deletions

View File

@ -304,32 +304,18 @@ func viewLogin(app *App, w http.ResponseWriter, r *http.Request) error {
p := &struct { p := &struct {
page.StaticPage page.StaticPage
To string *OAuthButtons
Message template.HTML To string
Flashes []template.HTML Message template.HTML
LoginUsername string Flashes []template.HTML
OauthSlack bool LoginUsername string
OauthWriteAs bool
OauthGitlab bool
GitlabDisplayName string
OauthGeneric bool
OauthGenericDisplayName string
OauthGitea bool
GiteaDisplayName string
}{ }{
StaticPage: pageForReq(app, r), StaticPage: pageForReq(app, r),
To: r.FormValue("to"), OAuthButtons: NewOAuthButtons(app.Config()),
Message: template.HTML(""), To: r.FormValue("to"),
Flashes: []template.HTML{}, Message: template.HTML(""),
LoginUsername: getTempInfo(app, "login-user", r, w), Flashes: []template.HTML{},
OauthSlack: app.Config().SlackOauth.ClientID != "", LoginUsername: getTempInfo(app, "login-user", r, w),
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),
} }
if earlyError != "" { if earlyError != "" {

19
app.go
View File

@ -238,27 +238,16 @@ func handleViewLanding(app *App, w http.ResponseWriter, r *http.Request) error {
p := struct { p := struct {
page.StaticPage page.StaticPage
*OAuthButtons
Flashes []template.HTML Flashes []template.HTML
Banner template.HTML Banner template.HTML
Content template.HTML Content template.HTML
ForcedLanding bool ForcedLanding bool
OauthSlack bool
OauthWriteAs bool
OauthGitlab bool
OauthGeneric bool
OauthGenericDisplayName string
GitlabDisplayName string
}{ }{
StaticPage: pageForReq(app, r), StaticPage: pageForReq(app, r),
ForcedLanding: forceLanding, OAuthButtons: NewOAuthButtons(app.Config()),
OauthSlack: app.Config().SlackOauth.ClientID != "", ForcedLanding: forceLanding,
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),
} }
banner, err := getLandingBanner(app) banner, err := getLandingBanner(app)

View File

@ -30,19 +30,27 @@ import (
// OAuthButtons holds display information for different OAuth providers we support. // OAuthButtons holds display information for different OAuth providers we support.
type OAuthButtons struct { type OAuthButtons struct {
SlackEnabled bool SlackEnabled bool
WriteAsEnabled bool WriteAsEnabled bool
GitLabEnabled bool GitLabEnabled bool
GitLabDisplayName string GitLabDisplayName string
GiteaEnabled bool
GiteaDisplayName string
GenericEnabled bool
GenericDisplayName string
} }
// NewOAuthButtons creates a new OAuthButtons struct based on our app configuration. // NewOAuthButtons creates a new OAuthButtons struct based on our app configuration.
func NewOAuthButtons(cfg *config.Config) *OAuthButtons { func NewOAuthButtons(cfg *config.Config) *OAuthButtons {
return &OAuthButtons{ return &OAuthButtons{
SlackEnabled: cfg.SlackOauth.ClientID != "", SlackEnabled: cfg.SlackOauth.ClientID != "",
WriteAsEnabled: cfg.WriteAsOauth.ClientID != "", WriteAsEnabled: cfg.WriteAsOauth.ClientID != "",
GitLabEnabled: cfg.GitlabOauth.ClientID != "", GitLabEnabled: cfg.GitlabOauth.ClientID != "",
GitLabDisplayName: config.OrDefaultString(cfg.GitlabOauth.DisplayName, gitlabDisplayName), 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),
} }
} }

View File

@ -60,6 +60,9 @@ form dd {
margin-top: 0; margin-top: 0;
max-width: 8em; max-width: 8em;
} }
.or {
margin-bottom: 2.5em !important;
}
</style> </style>
{{end}} {{end}}
{{define "content"}} {{define "content"}}
@ -73,20 +76,7 @@ form dd {
<div{{if not .OpenRegistration}} style="padding: 2em 0;"{{end}}> <div{{if not .OpenRegistration}} style="padding: 2em 0;"{{end}}>
{{ if .OpenRegistration }} {{ if .OpenRegistration }}
{{ if or .OauthSlack .OauthWriteAs .OauthGitlab .OauthGeneric }} {{template "oauth-buttons" .}}
{{ if .OauthSlack }}
<div class="row content-container signinbtns signinoauthbtns"><a class="loginbtn" href="/oauth/slack"><img alt="Sign in with Slack" height="40" width="172" src="/img/sign_in_with_slack.png" srcset="/img/sign_in_with_slack.png 1x, /img/sign_in_with_slack@2x.png 2x" /></a></div>
{{ end }}
{{ if .OauthWriteAs }}
<div class="row content-container signinbtns signinoauthbtns"><a class="btn cta loginbtn" id="writeas-login" href="/oauth/write.as">Sign in with <strong>Write.as</strong></a></div>
{{ end }}
{{ if .OauthGitlab }}
<div class="row content-container signinbtns signinoauthbtns"><a class="btn cta loginbtn" id="gitlab-login" href="/oauth/gitlab">Sign in with <strong>{{.GitlabDisplayName}}</strong></a></div>
{{ end }}
{{ if .OauthGeneric }}
<div class="row content-container signinbtns signinoauthbtns"><a class="btn cta loginbtn" id="generic-oauth-login" href="/oauth/generic">Sign in with <strong>{{ .OauthGenericDisplayName }}</strong></a></div>
{{ end }}
{{ end }}
{{if not .DisablePasswordAuth}} {{if not .DisablePasswordAuth}}
{{if .Flashes}}<ul class="errors"> {{if .Flashes}}<ul class="errors">
{{range .Flashes}}<li class="urgent">{{.}}</li>{{end}} {{range .Flashes}}<li class="urgent">{{.}}</li>{{end}}

View File

@ -13,32 +13,7 @@ input{margin-bottom:0.5em;}
{{range .Flashes}}<li class="urgent">{{.}}</li>{{end}} {{range .Flashes}}<li class="urgent">{{.}}</li>{{end}}
</ul>{{end}} </ul>{{end}}
{{ if or .OauthSlack .OauthWriteAs .OauthGitlab .OauthGeneric .OauthGitea }} {{template "oauth-buttons" .}}
<div class="row content-container signinbtns">
{{ if .OauthSlack }}
<a class="loginbtn" href="/oauth/slack"><img alt="Sign in with Slack" height="40" width="172" src="/img/sign_in_with_slack.png" srcset="/img/sign_in_with_slack.png 1x, /img/sign_in_with_slack@2x.png 2x" /></a>
{{ end }}
{{ if .OauthWriteAs }}
<a class="btn cta loginbtn" id="writeas-login" href="/oauth/write.as">Sign in with <strong>Write.as</strong></a>
{{ end }}
{{ if .OauthGitlab }}
<a class="btn cta loginbtn" id="gitlab-login" href="/oauth/gitlab">Sign in with <strong>{{.GitlabDisplayName}}</strong></a>
{{ end }}
{{ if .OauthGeneric }}
<a class="btn cta loginbtn" id="generic-oauth-login" href="/oauth/generic">Sign in with <strong>{{ .OauthGenericDisplayName }}</strong></a>
{{ end }}
{{ if .OauthGitea }}
<a class="btn cta loginbtn" id="gitea-login" href="/oauth/gitea">Sign in with <strong>{{.GiteaDisplayName}}</strong></a>
{{ end }}
</div>
{{if not .DisablePasswordAuth}}
<div class="or">
<p>or</p>
<hr class="short" />
</div>
{{end}}
{{ end }}
{{if not .DisablePasswordAuth}} {{if not .DisablePasswordAuth}}
<form action="/auth/login" method="post" style="text-align: center;margin-top:1em;" onsubmit="disableSubmit()"> <form action="/auth/login" method="post" style="text-align: center;margin-top:1em;" onsubmit="disableSubmit()">

View File

@ -85,12 +85,18 @@ func initPage(parentDir, path, key string) {
log.Info(" [%s] %s", key, path) log.Info(" [%s] %s", key, path)
} }
pages[key] = template.Must(template.New("").Funcs(funcMap).ParseFiles( files := []string{
path, path,
filepath.Join(parentDir, templatesDir, "include", "footer.tmpl"), filepath.Join(parentDir, templatesDir, "include", "footer.tmpl"),
filepath.Join(parentDir, templatesDir, "base.tmpl"), filepath.Join(parentDir, templatesDir, "base.tmpl"),
filepath.Join(parentDir, templatesDir, "user", "include", "silenced.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) { func initUserPage(parentDir, path, key string) {

View File

@ -0,0 +1,28 @@
{{define "oauth-buttons"}}
{{ if or .SlackEnabled .WriteAsEnabled .GitLabEnabled .GiteaEnabled .GenericEnabled }}
<div class="row content-container signinbtns">
{{ if .SlackEnabled }}
<a class="loginbtn" href="/oauth/slack"><img alt="Sign in with Slack" height="40" width="172" src="/img/sign_in_with_slack.png" srcset="/img/sign_in_with_slack.png 1x, /img/sign_in_with_slack@2x.png 2x" /></a>
{{ end }}
{{ if .WriteAsEnabled }}
<a class="btn cta loginbtn" id="writeas-login" href="/oauth/write.as">Sign in with <strong>Write.as</strong></a>
{{ end }}
{{ if .GitLabEnabled }}
<a class="btn cta loginbtn" id="gitlab-login" href="/oauth/gitlab">Sign in with <strong>{{.GitLabDisplayName}}</strong></a>
{{ end }}
{{ if .GiteaEnabled }}
<a class="btn cta loginbtn" id="gitea-login" href="/oauth/gitea">Sign in with <strong>{{.GiteaDisplayName}}</strong></a>
{{ end }}
{{ if .GenericEnabled }}
<a class="btn cta loginbtn" id="generic-oauth-login" href="/oauth/generic">Sign in with <strong>{{.GenericDisplayName}}</strong></a>
{{ end }}
</div>
{{if not .DisablePasswordAuth}}
<div class="or">
<p>or</p>
<hr class="short" />
</div>
{{end}}
{{ end }}
{{end}}