diff options
author | jan <jan@ruken.pw> | 2015-12-20 08:51:38 (UTC) |
---|---|---|
committer | jan <jan@ruken.pw> | 2015-12-20 08:51:38 (UTC) |
commit | 9cdf03be6b141d10b2a53656912f4bdcea553365 (patch) | |
tree | 7c079196b2df2823960044ff69002db6889dbfc7 /charakterin.go | |
parent | 52a7ca0fadf937d17d9d7dc44d8bc2ba7b8e174f (diff) |
user.Logout hinzugefügt zum cookie clearen, macht das ganze etwas schöner. vielleicht sollte der datenbankkram auch dort gemacht werden, aber dann bräuchten wir irgendwoher die sql.Conn.
Diffstat (limited to 'charakterin.go')
-rw-r--r-- | charakterin.go | 21 |
1 files changed, 8 insertions, 13 deletions
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) { | |||
119 | 119 | ||
120 | // Logout loggt einen Charakter aus. Wird direkt über den Request gehandlet. | 120 | // Logout loggt einen Charakter aus. Wird direkt über den Request gehandlet. |
121 | func (c *Charakterin) Logout(w http.ResponseWriter, r *http.Request) { | 121 | func (c *Charakterin) Logout(w http.ResponseWriter, r *http.Request) { |
122 | cookie, err := r.Cookie("session") | 122 | user, err := c.GetUserFromRequest(r) |
123 | if err != nil { | 123 | if err != nil { |
124 | http.Redirect(w, r, c.FallbackRoute, 302) | 124 | http.Redirect(w, r, c.FallbackRoute, 302) |
125 | return | 125 | return |
@@ -127,30 +127,24 @@ func (c *Charakterin) Logout(w http.ResponseWriter, r *http.Request) { | |||
127 | 127 | ||
128 | stmt, err := c.Database.Prepare("DELETE FROM login.sessions WHERE id=$1") | 128 | stmt, err := c.Database.Prepare("DELETE FROM login.sessions WHERE id=$1") |
129 | if err != nil { | 129 | if err != nil { |
130 | http.Redirect(w, r, c.FallbackRoute, 302) | 130 | http.Error(w, "500", http.StatusInternalServerError) |
131 | return | 131 | return |
132 | } | 132 | } |
133 | 133 | ||
134 | result, err := stmt.Exec(cookie.Value) | 134 | result, err := stmt.Exec(user.SessionID) |
135 | if err != nil { | 135 | if err != nil { |
136 | log.Println(err) | 136 | log.Println(err) |
137 | http.Redirect(w, r, c.FallbackRoute, 302) | 137 | http.Error(w, "500", http.StatusInternalServerError) |
138 | return | 138 | return |
139 | } | 139 | } |
140 | 140 | ||
141 | if val, err := result.RowsAffected(); err != nil || val == 0 { | 141 | if val, err := result.RowsAffected(); err != nil || val == 0 { |
142 | log.Println("could not remove session",cookie.Value,err) | 142 | log.Println("could not remove session",user.SessionID,err) |
143 | http.Redirect(w, r, c.FallbackRoute, 302) | 143 | http.Redirect(w, r, c.FallbackRoute, 302) |
144 | return | 144 | return |
145 | } | 145 | } |
146 | 146 | ||
147 | http.SetCookie(w, &http.Cookie{ | 147 | user.Logout(w) |
148 | Name: "session", | ||
149 | Value: "benis", | ||
150 | Expires: time.Now(), | ||
151 | MaxAge: 0, | ||
152 | }) | ||
153 | |||
154 | http.Redirect(w, r, c.FallbackRoute, 302) | 148 | http.Redirect(w, r, c.FallbackRoute, 302) |
155 | } | 149 | } |
156 | 150 | ||
@@ -174,7 +168,7 @@ func (c *Charakterin) IsLoggedIn(r *http.Request) bool { | |||
174 | func (c *Charakterin) GetUserFromRequest(r *http.Request) (*User, error) { | 168 | func (c *Charakterin) GetUserFromRequest(r *http.Request) (*User, error) { |
175 | cookie, err := r.Cookie("session") | 169 | cookie, err := r.Cookie("session") |
176 | if err != nil { | 170 | if err != nil { |
177 | return nil, errors.New("not logged in") | 171 | return nil, errors.New("no_session_cookie") |
178 | } | 172 | } |
179 | 173 | ||
180 | var email, name string | 174 | var email, name string |
@@ -199,6 +193,7 @@ func (c *Charakterin) GetUserFromRequest(r *http.Request) (*User, error) { | |||
199 | password, | 193 | password, |
200 | dspName, | 194 | dspName, |
201 | lastActivity, | 195 | lastActivity, |
196 | cookie.Value, | ||
202 | } | 197 | } |
203 | 198 | ||
204 | return user, nil | 199 | return user, nil |