-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
64 lines (53 loc) · 1.44 KB
/
auth.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
package handlers
import (
"net/http"
"github.com/porium/porium/models"
log "github.com/sirupsen/logrus"
"github.com/labstack/echo"
)
func (h *Handler) SetNewAuth(c echo.Context) error {
auth := new(models.Auth)
if err := c.Bind(auth); err != nil {
log.WithError(err).Error("failed to bind request body to new auth request")
return &echo.HTTPError{
Code: http.StatusBadRequest,
Message: "failed to process auth request",
}
}
if err := h.authRepo.Create(auth); err != nil {
log.WithError(err).Error("signing up new user failed")
return &echo.HTTPError{
Code: http.StatusInternalServerError,
Message: "signing up new user failed",
}
}
return c.JSON(http.StatusOK, nil)
}
func (h *Handler) GetAuth(c echo.Context) error {
type getAuthReq struct {
LookupKey string
}
req := new(getAuthReq)
if err := c.Bind(req); err != nil {
log.WithError(err).Error("failed to bind request body")
return &echo.HTTPError{
Code: http.StatusBadRequest,
Message: "Missing field: lookupKey",
}
}
auth, err := h.authRepo.GetByLookupKey(req.LookupKey)
if err != nil {
log.WithError(err).Errorln("auth lookup failed with key: ", req.LookupKey)
return &echo.HTTPError{
Code: http.StatusInternalServerError,
Message: "Failed to lookup auth",
}
}
if auth == nil {
return &echo.HTTPError{
Code: http.StatusUnauthorized,
Message: "Invalid username or password",
}
}
return c.JSON(http.StatusOK, auth)
}