lishwist/auth/login_get.go

63 lines
1.4 KiB
Go

package auth
import (
sesh "lishwist/session"
"lishwist/templates"
"net/http"
)
type LoginProps struct {
GeneralError string
SuccessfulRegistration bool
Username templates.InputProps
Password templates.InputProps
}
func NewLoginProps() LoginProps {
return LoginProps{
Username: templates.InputProps{
Name: "username",
Required: true,
},
Password: templates.InputProps{
Name: "password",
Type: "password",
Required: true,
},
}
}
func (auth *AuthMiddleware) Login(w http.ResponseWriter, r *http.Request) {
session, _ := auth.Store.Get(r, "lishwist_user")
props := NewLoginProps()
flash, err := sesh.GetFirstFlash(w, r, session, "login_props")
if err != nil {
http.Error(w, "Something went wrong. Error code: Zuko", http.StatusInternalServerError)
return
}
flashProps, ok := flash.(*LoginProps)
if ok {
props.Username.Value = flashProps.Username.Value
props.GeneralError = flashProps.GeneralError
props.Username.Error = flashProps.Username.Error
props.Password.Error = flashProps.Password.Error
}
flash, err = sesh.GetFirstFlash(w, r, session, "successful_registration")
if err != nil {
http.Error(w, "Something went wrong. Error code: Zuko", http.StatusInternalServerError)
return
}
successfulReg, _ := flash.(bool)
if successfulReg {
props.SuccessfulRegistration = true
}
templates.Execute(w, "login.gotmpl", props)
}