package api import ( "log" lishwist "lishwist/core" "lishwist/http/templates" ) type LoginProps struct { GeneralError string `json:",omitempty"` SuccessfulRegistration bool `json:",omitempty"` Username templates.InputProps Password templates.InputProps } func NewLoginProps(username, password string) *LoginProps { return &LoginProps{ Username: templates.InputProps{ Name: "username", Required: true, Value: username, }, Password: templates.InputProps{ Name: "password", Type: "password", Required: true, Value: password, }, } } func (p *LoginProps) Validate() (valid bool) { valid = true if !p.Username.Validate() { valid = false } if !p.Password.Validate() { valid = false } return } func Login(username, password string) *LoginProps { props := NewLoginProps(username, password) valid := props.Validate() props.Password.Value = "" if !valid { log.Printf("Invalid props: %#v\n", props) return props } _, err := lishwist.Login(props.Username.Value, props.Password.Value) if err == nil { return nil } switch err.(type) { case lishwist.ErrorInvalidCredentials: log.Printf("Invalid credentials: %w\n", err) props.GeneralError = "Username or password invalid" return props default: log.Printf("Login error: %w\n", err) props.GeneralError = "Something went wrong." return props } }