/
api.go
80 lines (73 loc) · 1.72 KB
/
api.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
75
76
77
78
79
80
package auth
import (
"context"
"net/http"
mAuth "github.com/ddosakura/starmap/gate/middleware/auth"
"github.com/ddosakura/starmap/gate/middleware/client"
"github.com/ddosakura/starmap/gate/model"
auth "github.com/ddosakura/starmap/srv/auth/proto"
"github.com/labstack/echo"
)
func login(c echo.Context) error {
ut, e := client.AuthUser(c).
Login(context.Background(),
c.Get(kuap).(*auth.UserAuth),
)
if e != nil {
return model.NewResponse(c, http.StatusBadRequest).
MicroError(e).
Build(nil)
}
return model.NewResponse(c, http.StatusOK).
FreshJWT(ut.Token).
Build(ut.User)
}
func register(c echo.Context) error {
ut, e := client.AuthUser(c).
Register(context.Background(),
c.Get(kuap).(*auth.UserAuth),
)
if e != nil {
return model.NewResponse(c, http.StatusBadRequest).
MicroError(e).
Build(nil)
}
return model.NewResponse(c, http.StatusOK).
FreshJWT(ut.Token).
Build(ut.User)
}
func info(c echo.Context) error {
token := mAuth.GetJWT(c)
ut, e := client.AuthUser(c).
Info(context.Background(), &auth.UserToken{
Token: token,
})
if e != nil {
return model.NewResponse(c, http.StatusBadRequest).
MicroError(e).
Build(nil)
}
return model.NewResponse(c, http.StatusOK).
FreshJWT(ut.Token).
Build(ut.User)
}
func update(c echo.Context) error {
jwt := mAuth.JWT(c)
u := c.Get(kuap).(*auth.UserToken)
if u.Auth.Password == "" {
u.User.UUID = jwt.User.UUID
u.Auth = nil
} else {
u.Auth.ID = jwt.User.UUID
u.User = nil
}
ut, e := client.AuthUser(c).Change(context.Background(), u)
if e != nil {
return model.NewResponse(c, http.StatusBadRequest).
MicroError(e).
Build(nil)
}
return model.NewResponse(c, http.StatusOK).
FreshJWT(ut.Token).
Build(ut.User)
}