aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Christophersen <jan@ruken.pw>2015-12-19 22:00:02 (UTC)
committerJan Christophersen <jan@ruken.pw>2015-12-19 22:00:02 (UTC)
commit52a7ca0fadf937d17d9d7dc44d8bc2ba7b8e174f (patch)
treec0942aec77d0cd9b6f3bd555b4a411c44d42533d
parent135133902d834e2165c9d7fe064a6f3b40182208 (diff)
implementierung von logout-funktionalität.
-rw-r--r--charakterin.go77
-rw-r--r--user.go23
2 files changed, 96 insertions, 4 deletions
diff --git a/charakterin.go b/charakterin.go
index 3573e8b..127e362 100644
--- a/charakterin.go
+++ b/charakterin.go
@@ -2,6 +2,7 @@ package charakterin
2 2
3import ( 3import (
4 "database/sql" 4 "database/sql"
5 "errors"
5 "io/ioutil" 6 "io/ioutil"
6 "log" 7 "log"
7 "net/http" 8 "net/http"
@@ -116,6 +117,43 @@ func (c *Charakterin) Login(w http.ResponseWriter, r *http.Request) {
116 http.Redirect(w, r, c.FallbackRoute, 302) 117 http.Redirect(w, r, c.FallbackRoute, 302)
117} 118}
118 119
120// Logout loggt einen Charakter aus. Wird direkt über den Request gehandlet.
121func (c *Charakterin) Logout(w http.ResponseWriter, r *http.Request) {
122 cookie, err := r.Cookie("session")
123 if err != nil {
124 http.Redirect(w, r, c.FallbackRoute, 302)
125 return
126 }
127
128 stmt, err := c.Database.Prepare("DELETE FROM login.sessions WHERE id=$1")
129 if err != nil {
130 http.Redirect(w, r, c.FallbackRoute, 302)
131 return
132 }
133
134 result, err := stmt.Exec(cookie.Value)
135 if err != nil {
136 log.Println(err)
137 http.Redirect(w, r, c.FallbackRoute, 302)
138 return
139 }
140
141 if val, err := result.RowsAffected(); err != nil || val == 0 {
142 log.Println("could not remove session",cookie.Value,err)
143 http.Redirect(w, r, c.FallbackRoute, 302)
144 return
145 }
146
147 http.SetCookie(w, &http.Cookie{
148 Name: "session",
149 Value: "benis",
150 Expires: time.Now(),
151 MaxAge: 0,
152 })
153
154 http.Redirect(w, r, c.FallbackRoute, 302)
155}
156
119// IsLoggedIn überprüft anhand eines Request, ob der User eingeloggt ist. 157// IsLoggedIn überprüft anhand eines Request, ob der User eingeloggt ist.
120func (c *Charakterin) IsLoggedIn(r *http.Request) bool { 158func (c *Charakterin) IsLoggedIn(r *http.Request) bool {
121 cookie, err := r.Cookie("session") 159 cookie, err := r.Cookie("session")
@@ -123,14 +161,45 @@ func (c *Charakterin) IsLoggedIn(r *http.Request) bool {
123 return false 161 return false
124 } 162 }
125 163
126 rows, err := c.Database.Query(`SELECT * FROM login.sessions WHERE id = $1`, cookie.Value) 164 var result string
165 err = c.Database.QueryRow(`SELECT login.get_user_by_session($1)`, cookie.Value).Scan(&result)
127 if err != nil { 166 if err != nil {
128 log.Println(err) 167 log.Println(err)
129 return false 168 return false
130 } 169 }
131 170
132 if rows.Next() { 171 return true
133 return true 172}
173
174func (c *Charakterin) GetUserFromRequest(r *http.Request) (*User, error) {
175 cookie, err := r.Cookie("session")
176 if err != nil {
177 return nil, errors.New("not logged in")
134 } 178 }
135 return false 179
180 var email, name string
181 var displayName *string
182 var password []byte
183 var id int
184 var lastActivity time.Time
185 err = c.Database.QueryRow(`SELECT * FROM login.get_user_by_session($1)`, cookie.Value).Scan(&id, &email, &name, &password, &displayName, &lastActivity)
186 if err != nil {
187 return nil, err
188 }
189
190 dspName := ""
191 if displayName != nil {
192 dspName = *displayName
193 }
194
195 user := &User{
196 id,
197 name,
198 email,
199 password,
200 dspName,
201 lastActivity,
202 }
203
204 return user, nil
136} 205}
diff --git a/user.go b/user.go
new file mode 100644
index 0000000..4fd536c
--- /dev/null
+++ b/user.go
@@ -0,0 +1,23 @@
1package charakterin
2
3import (
4 "time"
5)
6
7// Ein User ist ein ganz toller Benutzer.
8type User struct {
9 ID int
10 Name string
11 EMail string
12 Password []byte
13 DisplayName string
14 LastActivity time.Time
15}
16
17// GetName gibt den Anzeigenamen oder wenn dieser nicht gesetzt ist den Benutzernamen zurück.
18func (u *User) GetName() string {
19 if len(u.DisplayName) > 0 {
20 return u.DisplayName
21 }
22 return u.Name
23}