Skip to content

Commit 7182591

Browse files
authored
Merge pull request #218 from sixwaaaay/account
feat: update account
2 parents 0410ee7 + f115d2f commit 7182591

File tree

10 files changed

+151
-117
lines changed

10 files changed

+151
-117
lines changed

.github/workflows/account-ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
- name: Set up Go
2121
uses: actions/setup-go@v4
2222
with:
23-
go-version: 1.19
23+
go-version: "1.21"
2424

2525
- name: Build account Binary Executable
2626
run: |

cmd/account/accounthandler.go

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,18 @@ package main
1515

1616
import (
1717
"regexp"
18-
"time"
1918

19+
pb "codeberg.org/sixwaaaay/sharing-pb"
2020
"github.com/labstack/echo/v4"
21-
"github.com/sixwaaaay/sharing/pkg/encoder"
22-
"github.com/sixwaaaay/sharing/pkg/pb"
23-
"github.com/sixwaaaay/sharing/pkg/sign"
2421
)
2522

2623
type AccountHandler struct {
2724
client pb.UserServiceClient
28-
jwt sign.JWT
25+
signer signer
2926
}
3027

31-
func NewAccountHandler(client pb.UserServiceClient, jwt sign.JWT) *AccountHandler {
32-
return &AccountHandler{
33-
client: client,
34-
jwt: jwt,
35-
}
28+
func NewAccountHandler(client pb.UserServiceClient, signer signer) *AccountHandler {
29+
return &AccountHandler{client: client, signer: signer}
3630
}
3731

3832
func (h *AccountHandler) Update(e *echo.Echo) {
@@ -85,22 +79,16 @@ func (h *AccountHandler) Login(ctx echo.Context) error {
8579
return ctx.JSON(400, err)
8680
}
8781
u := reply.GetUser()
88-
option := sign.SignOption{
89-
Username: u.Name,
90-
UserID: u.Id,
91-
Duration: time.Duration(h.jwt.TTL) * time.Second,
92-
Secret: []byte(h.jwt.Secret),
93-
}
94-
token, err := sign.GenSignedToken(option)
82+
token, err := h.signer(u.Id, u.Name)
9583
if err != nil {
9684
return ctx.JSON(400, err)
9785
}
9886

99-
resp := &pb.JSONLoginReply{
100-
Account: u,
87+
resp := &LoginReply{
88+
Account: covert(u),
10189
Token: token,
10290
}
103-
return encoder.Marshal(ctx.Response(), resp)
91+
return ctx.JSON(200, resp)
10492
}
10593

10694
type RegisterRequest struct {
@@ -140,20 +128,50 @@ func (h *AccountHandler) Register(ctx echo.Context) error {
140128
return ctx.JSON(400, err)
141129
}
142130
u := reply.GetUser()
143-
option := sign.SignOption{
144-
Username: u.Name,
145-
UserID: u.Id,
146-
Duration: time.Duration(h.jwt.TTL) * time.Second,
147-
Secret: []byte(h.jwt.Secret),
148-
}
149-
token, err := sign.GenSignedToken(option)
131+
132+
token, err := h.signer(u.Id, u.Name)
150133
if err != nil {
151134
return ctx.JSON(400, err)
152135
}
153136

154-
resp := &pb.JSONLoginReply{
155-
Account: u,
137+
resp := &LoginReply{
138+
Account: covert(u),
156139
Token: token,
157140
}
158-
return encoder.Marshal(ctx.Response(), resp)
141+
return ctx.JSON(200, resp)
142+
}
143+
144+
type Account struct {
145+
Id int64 `json:"id"`
146+
Name string `json:"name"`
147+
IsFollow bool `json:"is_follow"`
148+
AvatarUrl string `json:"avatar_url"`
149+
BgUrl string `json:"bg_url"`
150+
Bio string `json:"bio"`
151+
LikesGiven int32 `json:"likes_given"`
152+
LikesReceived int32 `json:"likes_received"`
153+
VideosPosted int32 `json:"videos_posted"`
154+
Following int32 `json:"following"`
155+
Followers int32 `json:"followers"`
156+
}
157+
158+
type LoginReply struct {
159+
Account *Account `json:"account"`
160+
Token string `json:"token"`
161+
}
162+
163+
func covert(u *pb.User) *Account {
164+
return &Account{
165+
Id: u.Id,
166+
Name: u.Name,
167+
IsFollow: u.IsFollow,
168+
AvatarUrl: u.AvatarUrl,
169+
BgUrl: u.BgUrl,
170+
Bio: u.Bio,
171+
LikesGiven: u.LikesGiven,
172+
LikesReceived: u.LikesReceived,
173+
VideosPosted: u.VideosPosted,
174+
Following: u.Following,
175+
Followers: u.Followers,
176+
}
159177
}

cmd/account/configs/config.yaml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
ListenOn: :8080
2-
UserService:
3-
Address: shauser:8080
4-
JWT:
5-
SECRET: $JWT_SECRET
6-
TTL: 1296000 # 15day 15 * 24 * 60 * 60
2+
UserServiceAddress: shauser:8080
3+
SECRET: $JWT_SECRET
4+
TTL: 360h # 15day 15 * 24 * 60 * 60
75
Oauth:
86
clientid: $OAUTH_CLIENT_ID
97
clientsecret: $OAUTH_CLIENT_SECRET

cmd/account/go.mod

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
module github.com/sixwaaaay/sharing/cmd/account
22

3-
go 1.19
3+
go 1.21
44

55
require (
6+
codeberg.org/sixwaaaay/sharing-pb v0.1.2
7+
github.com/golang-jwt/jwt/v5 v5.2.0
68
github.com/labstack/echo/v4 v4.11.1
79
github.com/labstack/gommon v0.4.0
810
github.com/sixwaaaay/must v0.1.0
9-
github.com/sixwaaaay/sharing v0.9.1
11+
github.com/sixwaaaay/token v0.1.1
12+
github.com/spf13/viper v1.15.0
1013
go.uber.org/automaxprocs v1.5.3
1114
golang.org/x/oauth2 v0.15.0
1215
)
1316

1417
require (
1518
github.com/fsnotify/fsnotify v1.6.0 // indirect
16-
github.com/gogo/protobuf v1.3.2 // indirect
1719
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
18-
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
1920
github.com/golang/protobuf v1.5.3 // indirect
2021
github.com/hashicorp/hcl v1.0.0 // indirect
2122
github.com/magiconair/properties v1.8.7 // indirect
@@ -27,7 +28,6 @@ require (
2728
github.com/spf13/cast v1.5.0 // indirect
2829
github.com/spf13/jwalterweatherman v1.1.0 // indirect
2930
github.com/spf13/pflag v1.0.5 // indirect
30-
github.com/spf13/viper v1.15.0 // indirect
3131
github.com/subosito/gotenv v1.4.2 // indirect
3232
github.com/valyala/bytebufferpool v1.0.0 // indirect
3333
github.com/valyala/fasttemplate v1.2.2 // indirect
@@ -36,9 +36,9 @@ require (
3636
golang.org/x/sys v0.15.0 // indirect
3737
golang.org/x/text v0.14.0 // indirect
3838
golang.org/x/time v0.3.0 // indirect
39-
google.golang.org/appengine v1.6.7 // indirect
40-
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
41-
google.golang.org/grpc v1.54.0 // indirect
39+
google.golang.org/appengine v1.6.8 // indirect
40+
google.golang.org/genproto/googleapis/rpc v0.0.0-20231002182017-d307bd883b97 // indirect
41+
google.golang.org/grpc v1.60.0 // indirect
4242
google.golang.org/protobuf v1.31.0 // indirect
4343
gopkg.in/ini.v1 v1.67.0 // indirect
4444
gopkg.in/yaml.v3 v3.0.1 // indirect

0 commit comments

Comments
 (0)