From 9cdf03be6b141d10b2a53656912f4bdcea553365 Mon Sep 17 00:00:00 2001 From: jan Date: Sun, 20 Dec 2015 09:51:38 +0100 Subject: =?UTF-8?q?user.Logout=20hinzugef=C3=BCgt=20zum=20cookie=20clearen?= =?UTF-8?q?,=20macht=20das=20ganze=20etwas=20sch=C3=B6ner.=20vielleicht=20?= =?UTF-8?q?sollte=20der=20datenbankkram=20auch=20dort=20gemacht=20werden,?= =?UTF-8?q?=20aber=20dann=20br=C3=A4uchten=20wir=20irgendwoher=20die=20sql?= =?UTF-8?q?.Conn.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit diff --git a/charakterin.go b/charakterin.go index 127e362..abf9627 100644 --- a/charakterin.go +++ b/charakterin.go @@ -119,7 +119,7 @@ func (c *Charakterin) Login(w http.ResponseWriter, r *http.Request) { // Logout loggt einen Charakter aus. Wird direkt über den Request gehandlet. func (c *Charakterin) Logout(w http.ResponseWriter, r *http.Request) { - cookie, err := r.Cookie("session") + user, err := c.GetUserFromRequest(r) if err != nil { http.Redirect(w, r, c.FallbackRoute, 302) return @@ -127,30 +127,24 @@ func (c *Charakterin) Logout(w http.ResponseWriter, r *http.Request) { stmt, err := c.Database.Prepare("DELETE FROM login.sessions WHERE id=$1") if err != nil { - http.Redirect(w, r, c.FallbackRoute, 302) + http.Error(w, "500", http.StatusInternalServerError) return } - result, err := stmt.Exec(cookie.Value) + result, err := stmt.Exec(user.SessionID) if err != nil { log.Println(err) - http.Redirect(w, r, c.FallbackRoute, 302) + http.Error(w, "500", http.StatusInternalServerError) return } if val, err := result.RowsAffected(); err != nil || val == 0 { - log.Println("could not remove session",cookie.Value,err) + log.Println("could not remove session",user.SessionID,err) http.Redirect(w, r, c.FallbackRoute, 302) return } - http.SetCookie(w, &http.Cookie{ - Name: "session", - Value: "benis", - Expires: time.Now(), - MaxAge: 0, - }) - + user.Logout(w) http.Redirect(w, r, c.FallbackRoute, 302) } @@ -174,7 +168,7 @@ func (c *Charakterin) IsLoggedIn(r *http.Request) bool { func (c *Charakterin) GetUserFromRequest(r *http.Request) (*User, error) { cookie, err := r.Cookie("session") if err != nil { - return nil, errors.New("not logged in") + return nil, errors.New("no_session_cookie") } var email, name string @@ -199,6 +193,7 @@ func (c *Charakterin) GetUserFromRequest(r *http.Request) (*User, error) { password, dspName, lastActivity, + cookie.Value, } return user, nil diff --git a/user.go b/user.go index 4fd536c..1546c6b 100644 --- a/user.go +++ b/user.go @@ -2,6 +2,7 @@ package charakterin import ( "time" + "net/http" ) // Ein User ist ein ganz toller Benutzer. @@ -12,6 +13,7 @@ type User struct { Password []byte DisplayName string LastActivity time.Time + SessionID string } // GetName gibt den Anzeigenamen oder wenn dieser nicht gesetzt ist den Benutzernamen zurück. @@ -21,3 +23,13 @@ func (u *User) GetName() string { } return u.Name } + +func (u *User) Logout(w http.ResponseWriter) { + http.SetCookie(w, &http.Cookie{ + Name: "session", + Value: "benis", + Expires: time.Now(), + MaxAge: 0, + }) + return +} \ No newline at end of file -- cgit v0.10.1