diff options
-rw-r--r-- | charakterin.go | 21 | ||||
-rw-r--r-- | user.go | 12 |
2 files changed, 20 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 |
@@ -2,6 +2,7 @@ package charakterin | |||
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "time" | 4 | "time" |
5 | "net/http" | ||
5 | ) | 6 | ) |
6 | 7 | ||
7 | // Ein User ist ein ganz toller Benutzer. | 8 | // Ein User ist ein ganz toller Benutzer. |
@@ -12,6 +13,7 @@ type User struct { | |||
12 | Password []byte | 13 | Password []byte |
13 | DisplayName string | 14 | DisplayName string |
14 | LastActivity time.Time | 15 | LastActivity time.Time |
16 | SessionID string | ||
15 | } | 17 | } |
16 | 18 | ||
17 | // GetName gibt den Anzeigenamen oder wenn dieser nicht gesetzt ist den Benutzernamen zurück. | 19 | // GetName gibt den Anzeigenamen oder wenn dieser nicht gesetzt ist den Benutzernamen zurück. |
@@ -21,3 +23,13 @@ func (u *User) GetName() string { | |||
21 | } | 23 | } |
22 | return u.Name | 24 | return u.Name |
23 | } | 25 | } |
26 | |||
27 | func (u *User) Logout(w http.ResponseWriter) { | ||
28 | http.SetCookie(w, &http.Cookie{ | ||
29 | Name: "session", | ||
30 | Value: "benis", | ||
31 | Expires: time.Now(), | ||
32 | MaxAge: 0, | ||
33 | }) | ||
34 | return | ||
35 | } \ No newline at end of file | ||