Changes made while flying Tokyo -> Auckland #11

Merged
Teajey merged 5 commits from tokyo-wellington-changes into main 2024-12-27 22:40:30 +13:00
4 changed files with 26 additions and 10 deletions
Showing only changes of commit c8d179e297 - Show all commits

View File

@ -11,7 +11,7 @@ func Login(h http.Header, r *rsvp.Request) rsvp.Response {
props := api.NewLoginProps("", "") props := api.NewLoginProps("", "")
flash := session.FlashGet("login_props") flash := session.FlashGet()
flashProps, ok := flash.(*api.LoginProps) flashProps, ok := flash.(*api.LoginProps)
if ok { if ok {
props.Username.Value = flashProps.Username.Value props.Username.Value = flashProps.Username.Value
@ -21,7 +21,7 @@ func Login(h http.Header, r *rsvp.Request) rsvp.Response {
props.Password.Error = flashProps.Password.Error props.Password.Error = flashProps.Password.Error
} }
flash = session.FlashGet("successful_registration") flash = session.FlashGet()
successfulReg, _ := flash.(bool) successfulReg, _ := flash.(bool)
if successfulReg { if successfulReg {
props.SuccessfulRegistration = true props.SuccessfulRegistration = true
@ -39,7 +39,7 @@ func LoginPost(h http.Header, r *rsvp.Request) rsvp.Response {
props := api.Login(username, password) props := api.Login(username, password)
if props != nil { if props != nil {
session.FlashSet(&props, "login_props") session.FlashSet(&props)
return rsvp.SeeOther("/").SaveSession(session) return rsvp.SeeOther("/").SaveSession(session)
} }

View File

@ -10,7 +10,7 @@ func Register(h http.Header, r *rsvp.Request) rsvp.Response {
props := api.NewRegisterProps("", "", "") props := api.NewRegisterProps("", "", "")
session := r.GetSession() session := r.GetSession()
flash := session.FlashGet("register_props") flash := session.FlashGet()
flashProps, _ := flash.(*api.RegisterProps) flashProps, _ := flash.(*api.RegisterProps)
if flashProps != nil { if flashProps != nil {
@ -36,10 +36,10 @@ func RegisterPost(h http.Header, r *rsvp.Request) rsvp.Response {
s := r.GetSession() s := r.GetSession()
if props != nil { if props != nil {
s.FlashSet(&props, "register_props") s.FlashSet(&props)
return rsvp.SeeOther("/register").SaveSession(s) return rsvp.SeeOther("/register").SaveSession(s)
} }
s.FlashSet(true, "successful_registration") s.FlashSet(true)
return rsvp.SeeOther("/").SaveSession(s) return rsvp.SeeOther("/").SaveSession(s)
} }

View File

@ -29,6 +29,13 @@ func (res *Response) Write(w http.ResponseWriter, r *http.Request) error {
if res.SeeOther != "" { if res.SeeOther != "" {
http.Redirect(w, r, res.SeeOther, http.StatusSeeOther) http.Redirect(w, r, res.SeeOther, http.StatusSeeOther)
flash := res.Session.FlashPeek()
if flash != nil {
err := json.NewEncoder(w).Encode(flash)
if err != nil {
return err
}
}
return nil return nil
} }

View File

@ -8,8 +8,8 @@ type Session struct {
inner *sessions.Session inner *sessions.Session
} }
func (s *Session) FlashGet(key ...string) any { func (s *Session) FlashGet() any {
list := s.inner.Flashes(key...) list := s.inner.Flashes()
if len(list) < 1 { if len(list) < 1 {
return nil return nil
} else { } else {
@ -17,8 +17,17 @@ func (s *Session) FlashGet(key ...string) any {
} }
} }
func (s *Session) FlashSet(value any, key ...string) { func (s *Session) FlashPeek() any {
s.inner.AddFlash(value, key...) list, _ := s.inner.Values["_flash"].([]any)
if len(list) < 1 {
return nil
} else {
return list[0]
}
}
func (s *Session) FlashSet(value any) {
s.inner.AddFlash(value)
} }
func (s *Session) SetID(value string) { func (s *Session) SetID(value string) {