-
Notifications
You must be signed in to change notification settings - Fork 0
/
userHandler.go
74 lines (66 loc) · 2.12 KB
/
userHandler.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
73
74
package handlers
import (
"bytes"
"encoding/json"
"github.com/GerardSoleCa/PubKeyManager/domain"
"github.com/GerardSoleCa/PubKeyManager/server/utils"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"net/http"
)
// UserInteractor interface
type UserInteractor interface {
AddUser(user *domain.User) error
AuthenticateUser(username, password string) (domain.User, error)
}
// UserServiceHandler struct
type UserServiceHandler struct {
utils.HttpUtils
UserInteractor UserInteractor
Session *sessions.CookieStore
}
// ConfigureAuthHandler configures router routes for authentication
func ConfigureAuthHandler(router *mux.Router, interactor UserInteractor, session *sessions.CookieStore) {
handler := UserServiceHandler{}
handler.UserInteractor = interactor
handler.Session = session
router.Path("/auth/login").HandlerFunc(handler.login).Methods("POST")
router.Path("/auth/register").HandlerFunc(handler.register).Methods("POST")
}
func (handler UserServiceHandler) login(rw http.ResponseWriter, q *http.Request) {
glog.Debugf("Request > UserHandler > Login")
user := domain.User{}
if handler.ParseBody(q.Body, &user) != nil {
handler.BadRequest(rw)
return
}
user, err := handler.UserInteractor.AuthenticateUser(user.Username, user.Password)
if err != nil {
glog.Debugf("Error > UserHandler > Login :: %s", err.Error())
handler.Unauthorized(rw)
return
}
user.Password = ""
session, _ := handler.Session.Get(q, "authenticated")
session.Values["id"] = user.Id
session.Values["name"] = user.Username
session.Save(q, rw)
var response bytes.Buffer
encoder := json.NewEncoder(&response)
encoder.Encode(user)
handler.Ok(rw, response.String())
glog.Debugf("End Request > UserHandler > Login :: %s", response.String())
}
func (handler UserServiceHandler) register(rw http.ResponseWriter, q *http.Request) {
user := &domain.User{}
if handler.ParseBody(q.Body, user) != nil {
handler.BadRequest(rw)
return
}
if err := handler.UserInteractor.AddUser(user); err != nil {
handler.ErrorResponse(rw, &utils.ApiError{Code: 500, Err: err.Error()})
return
}
user.Password = ""
handler.CreatedWithBody(rw, user)
}