Files
ProxmoxAAS-Dashboard/app/routes/login.go
T
2026-10-26 23:15:38 +00:00

70 lines
1.4 KiB
Go

package routes
import (
"fmt"
"net/http"
"proxmoxaas-dashboard/app/common"
paas "proxmoxaas-dashboard/proxmoxaas-common-lib"
"github.com/go-viper/mapstructure/v2"
)
// stores each realm's data
type Realm struct {
Default int `json:"default"`
Realm string `json:"realm"`
Comment string `json:"comment"`
}
func GetLoginRealms() ([]Realm, error) {
realms := []Realm{}
body := []any{}
res, code, err := common.RequestGetAPI("/proxmox/access/domains", nil, &body)
if err != nil {
return realms, err
}
if code != 200 { // we expect /access/domains to always be avaliable
return realms, fmt.Errorf("request to /proxmox/access/domains resulted in %+v", res)
}
for _, v := range body {
v = v.(map[string]any)
realm := Realm{}
err := mapstructure.Decode(v, &realm)
if err != nil {
return realms, err
}
realms = append(realms, realm)
}
return realms, nil
}
func HandleGETLogin(w http.ResponseWriter, r *http.Request) {
realms, err := GetLoginRealms()
if err != nil {
common.HandleNonFatalError(w, err)
return
}
sel := common.Select{
ID: "realm",
Required: true,
}
for _, realm := range realms {
sel.Options = append(sel.Options, common.Option{
Selected: realm.Default != 0,
Value: realm.Realm,
Display: realm.Comment,
})
}
paas.HTTPExecuteHTMLTemplate(w, common.TMPL, "html/login.html", map[string]any{
"global": common.Global,
"page": "login",
"realms": sel,
})
}