respondWithSession makes a HTTP response with the session from the user with the given userID. It sets the HTTP-Only cookie for browser clients and also sends a JSON response for non-browser clients.
(w http.ResponseWriter, statusCode int, session *database.Session)
| 268 | // respondWithSession makes a HTTP response with the session from the user with the given userID. |
| 269 | // It sets the HTTP-Only cookie for browser clients and also sends a JSON response for non-browser clients. |
| 270 | func respondWithSession(w http.ResponseWriter, statusCode int, session *database.Session) { |
| 271 | setSessionCookie(w, session.Key, session.ExpiresAt) |
| 272 | |
| 273 | response := SessionResponse{ |
| 274 | Key: session.Key, |
| 275 | ExpiresAt: session.ExpiresAt.Unix(), |
| 276 | } |
| 277 | |
| 278 | w.Header().Set("Content-Type", "application/json") |
| 279 | |
| 280 | dat, err := json.Marshal(response) |
| 281 | if err != nil { |
| 282 | handleJSONError(w, err, "encoding response") |
| 283 | return |
| 284 | } |
| 285 | |
| 286 | w.WriteHeader(statusCode) |
| 287 | w.Write(dat) |
| 288 | } |
| 289 | |
| 290 | // respondJSON encodes the given payload into a JSON format and writes it to the given response writer |
| 291 | func respondJSON(w http.ResponseWriter, statusCode int, payload interface{}) { |
no test coverage detected