miniauth/pkg/web/web.go
2025-03-16 01:16:06 +01:00

58 lines
1.3 KiB
Go

package web
import (
"net/http"
"git.keks.cloud/kekskurse/miniauth/pkg/miniauth"
"github.com/gin-gonic/gin"
)
type WebConfig struct {
PublicRegistration bool `env:"PUBLIC_REGISTRATION, default=0"`
}
type Web struct {
config WebConfig
ma miniauth.Miniauth
}
func NewWeb(config WebConfig, ma miniauth.Miniauth) Web {
w := Web{}
w.config = config
w.ma = ma
return w
}
func (w Web) RegisterRoutes(routing *gin.RouterGroup) error {
routing.GET("/register", w.GetRegisterPage)
routing.POST("/register", w.PostRegisterPage)
return nil
}
func (w Web) GetRegisterPage(c *gin.Context) {
if !w.config.PublicRegistration {
c.HTML(403, "msg.html", gin.H{"msg": "Public registration disabled"})
return
}
c.HTML(http.StatusOK, "register.html", nil)
}
func (w Web) PostRegisterPage(c *gin.Context) {
if !w.config.PublicRegistration {
c.HTML(403, "msg.html", gin.H{"msg": "Public registration disabled"})
return
}
if c.PostForm("password") != c.PostForm("confirm_password") {
c.HTML(http.StatusOK, "register.html", gin.H{"msg": "Passworts dont match"})
return
}
err := w.ma.RegisterUser(c.PostForm("username"), c.PostForm("email"), c.PostForm("password"))
if err != nil {
c.HTML(http.StatusOK, "register.html", gin.H{"msg": err.Error()})
return
}
c.HTML(403, "msg.html", gin.H{"msg": "Your account was created, you can login now"})
}