Skip to content

Commit

Permalink
Improve login redirect (#959)
Browse files Browse the repository at this point in the history
@mieciu please test it before merging. I could not do that locally.

It fixes two issues:
- right redirect if you host it on different domain than localhost
- do also redirect if HTMX hit it in some fragment (auto-refreshed
panel)

---------

Co-authored-by: przemyslaw <[email protected]>
  • Loading branch information
jakozaur and mieciu authored Nov 7, 2024
1 parent ee91bfa commit 67bbaed
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 5 deletions.
3 changes: 2 additions & 1 deletion quesma/quesma/ui/console_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
uiTcpPort = "9999"
managementInternalPath = "/_quesma"
healthPath = managementInternalPath + "/health"
loginWithElasticSearch = "/login-with-elasticsearch"
)

//go:embed asset/*
Expand Down Expand Up @@ -70,7 +71,7 @@ func (qmc *QuesmaManagementConsole) createRouting() *mux.Router {
router.HandleFunc("/auth/{provider}/callback", authCallbackHandler)

// our logic for login
router.HandleFunc("/login-with-elasticsearch", qmc.HandleElasticsearchLogin)
router.HandleFunc(loginWithElasticSearch, qmc.HandleElasticsearchLogin)

authenticatedRoutes := router.PathPrefix("/").Subrouter()
if qmc.cfg.Elasticsearch.User == "" && qmc.cfg.Elasticsearch.Password == "" {
Expand Down
2 changes: 1 addition & 1 deletion quesma/quesma/ui/es_auth_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewElasticsearchAuthProvider() *ElasticsearchAuthProvider {
type ElasticsearchSession struct{}

func (e ElasticsearchSession) GetAuthURL() (string, error) {
return "http://localhost:9999/login-with-elasticsearch", nil
return loginWithElasticSearch, nil
}

func (e ElasticsearchSession) Marshal() string {
Expand Down
2 changes: 0 additions & 2 deletions quesma/quesma/ui/html_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ func (qmc *QuesmaManagementConsole) generateTopNavigation(target string) []byte
buffer.Html(abTestingPath)
buffer.Html(`">A/B</a></li>`)

buffer.Html(`<li><a href="/logout">Logout</a></li>`)

if qmc.isAuthEnabled {
buffer.Html(`<li><a href="/logout">Logout</a></li>`)
}
Expand Down
3 changes: 2 additions & 1 deletion quesma/quesma/ui/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (qmc *QuesmaManagementConsole) generateLoginForm() []byte {
buffer.Html(`<div class="login-form">`)
buffer.Html(`<h2>Login</h2>`)
buffer.Html(`<p style="color: #ccc;">Log in to Quesma admin console using your Elasticsearch credentials</p>`)
buffer.Html(`<form action="/login-with-elasticsearch" method="post">`)
buffer.Html(`<form action="`).Text(loginWithElasticSearch).Html(`" method="post">`)
buffer.Html(`<label for="username">Username:</label>`)
buffer.Html(`<input type="text" id="username" name="username" placeholder="Enter your Elasticsearch username" autofocus>`)
buffer.Html(`<label for="password">Password:</label>`)
Expand All @@ -41,6 +41,7 @@ func (qmc *QuesmaManagementConsole) HandleElasticsearchLogin(writer http.Respons
return
}
writer.Header().Set("Content-Type", "text/html")
writer.Header().Set("HX-Redirect", loginWithElasticSearch)
writer.Write(qmc.generateLoginForm())
} else if req.Method == http.MethodPost {
username := req.FormValue("username")
Expand Down

0 comments on commit 67bbaed

Please sign in to comment.