-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
72 lines (59 loc) · 1.68 KB
/
user.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
65
66
67
68
69
70
71
72
// Copyright 2013 Joshua Marsh. All rights reserved. Use of this
// source code is governed by a BSD-style license that can be found in
// the LICENSE file.
// Package user provides functionality for getting information about
// the currently logged in user.
package user
import (
"appengine"
"github.com/gorilla/mux"
"github.com/icub3d/gorca"
"net/http"
)
// MakeMuxer creates a http.Handler to manage all user operations. If
// a prefix is given, that prefix will be the first part of the
// url. This muxer will provide the following handlers and return a
// RESTful 404 on all others.
//
// GET prefix + / Get the currently logged in user.
func MakeMuxer(prefix string) http.Handler {
var m *mux.Router
// Pass through the prefix if we got one.
if prefix == "" {
m = mux.NewRouter()
} else {
m = mux.NewRouter().PathPrefix(prefix).Subrouter()
}
// Add the handler for GET /.
m.HandleFunc("/", GetUser).Methods("GET")
// Everything else should 404.
m.HandleFunc("/{path:.*}", gorca.NotFoundFunc)
return m
}
// UserInfo is the information about the current user.
type UserInfo struct {
// The users e-mail address.
Email string
// The url to use to log the user out.
LogoutURL string
}
// GetUser sends a JSON response with the current user information.
func GetUser(w http.ResponseWriter, r *http.Request) {
// Get the context.
c := appengine.NewContext(r)
// Get the current user.
u, ok := gorca.GetUserOrUnexpected(c, w, r)
if !ok {
return
}
// Get their logout URL.
logout, ok := gorca.GetUserLogoutURL(c, w, r, "/")
if !ok {
return
}
// Write the user information out.
gorca.WriteJSON(c, w, r, UserInfo{
Email: u.Email,
LogoutURL: logout,
})
}