feat: only show auth providers if they're enabled

This commit is contained in:
Sam 2023-04-18 23:31:57 +02:00
parent 17f2552c6a
commit f5d7bc4095
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
4 changed files with 98 additions and 83 deletions

View file

@ -144,9 +144,9 @@ type oauthURLsRequest struct {
}
type oauthURLsResponse struct {
Discord string `json:"discord"`
Tumblr string `json:"tumblr"`
Google string `json:"google"`
Discord string `json:"discord,omitempty"`
Tumblr string `json:"tumblr,omitempty"`
Google string `json:"google,omitempty"`
}
func (s *Server) oauthURLs(w http.ResponseWriter, r *http.Request) error {
@ -162,22 +162,25 @@ func (s *Server) oauthURLs(w http.ResponseWriter, r *http.Request) error {
if err != nil {
return errors.Wrap(err, "setting CSRF state")
}
var resp oauthURLsResponse
// copy Discord config and set redirect url
discordCfg := discordOAuthConfig
discordCfg.RedirectURL = req.CallbackDomain + "/auth/login/discord"
// copy tumblr config
tumblrCfg := tumblrOAuthConfig
tumblrCfg.RedirectURL = req.CallbackDomain + "/auth/login/tumblr"
// copy google config
googleCfg := googleOAuthConfig
googleCfg.RedirectURL = req.CallbackDomain + "/auth/login/google"
if discordOAuthConfig.ClientID != "" {
discordCfg := discordOAuthConfig
discordCfg.RedirectURL = req.CallbackDomain + "/auth/login/discord"
resp.Discord = discordCfg.AuthCodeURL(state) + "&prompt=none"
}
if tumblrOAuthConfig.ClientID != "" {
tumblrCfg := tumblrOAuthConfig
tumblrCfg.RedirectURL = req.CallbackDomain + "/auth/login/tumblr"
resp.Tumblr = tumblrCfg.AuthCodeURL(state)
}
if googleOAuthConfig.ClientID != "" {
googleCfg := googleOAuthConfig
googleCfg.RedirectURL = req.CallbackDomain + "/auth/login/google"
resp.Google = googleCfg.AuthCodeURL(state)
}
render.JSON(w, r, oauthURLsResponse{
Discord: discordCfg.AuthCodeURL(state) + "&prompt=none",
Tumblr: tumblrCfg.AuthCodeURL(state),
Google: googleCfg.AuthCodeURL(state),
})
render.JSON(w, r, resp)
return nil
}