-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuser_handlers.go
64 lines (58 loc) · 2.07 KB
/
user_handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (c) 2019 Faye Amacker. All rights reserved.
// Use of this source code is governed by Apache License 2.0 found in the LICENSE file.
package main
import (
"encoding/base64"
"encoding/json"
"net/http"
"github.com/gorilla/sessions"
)
func (s *server) handleLogout(w http.ResponseWriter, r *http.Request) {
session, ok := r.Context().Value(contextKeyLoginSession).(*sessions.Session)
if !ok {
panic("Failed to get session data from context")
}
delete(session.Values, sessionMapKeyUserSession)
writeOKServerResponse(w)
}
func (s *server) handleUser() http.HandlerFunc {
type response struct {
Status string `json:"status"`
Name string `json:"name"`
DisplayName string `json:"displayName"`
CredentialID string `json:"credentialID"`
RegisteredAt string `json:"registeredAt"`
LoggedInAt string `json:"loggedInAt"`
}
return func(w http.ResponseWriter, r *http.Request) {
session, ok := r.Context().Value(contextKeyLoginSession).(*sessions.Session)
if !ok {
panic("Failed to get session data from context")
}
uSession, ok := session.Values[sessionMapKeyUserSession].(*userSession)
if !ok {
writeFailedServerResponse(w, http.StatusUnauthorized, "Session doesn't have user credential")
return
}
registeredAt, loggedInAt, err := s.dataStore.getCredentialTimestamp(r.Context(), uSession.User.UserID, uSession.LoggedInCredentialID)
if err != nil {
writeFailedServerResponse(w, http.StatusInternalServerError, "Failed to find credential: "+err.Error())
return
}
resp := response{
Status: statusOK,
Name: uSession.User.UserName,
DisplayName: uSession.User.DisplayName,
CredentialID: base64.RawURLEncoding.EncodeToString(uSession.LoggedInCredentialID),
RegisteredAt: registeredAt.Format("02 Jan 06 15:04 MST"),
LoggedInAt: loggedInAt.Format("02 Jan 06 15:04 MST"),
}
b, err := json.Marshal(resp)
if err != nil {
writeFailedServerResponse(w, http.StatusInternalServerError, "failed to json marshal response body")
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(b)
}
}