diff options
Diffstat (limited to 'charakterin.go')
-rw-r--r-- | charakterin.go | 77 |
1 files changed, 73 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 | ||
3 | import ( | 3 | import ( |
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. | ||
121 | func (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. |
120 | func (c *Charakterin) IsLoggedIn(r *http.Request) bool { | 158 | func (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 | |||
174 | func (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 | } |