From 947d13236234b4acdb0b2ff587e1da563dc05e08 Mon Sep 17 00:00:00 2001 From: Yang Luo Date: Mon, 17 May 2021 23:25:28 +0800 Subject: [PATCH] Refactor out c.RequireSignedIn() --- controllers/account.go | 26 ++++++++++---------------- controllers/link.go | 11 ++++------- controllers/util.go | 13 ++++++++++++- controllers/verification.go | 12 ++++++------ 4 files changed, 32 insertions(+), 30 deletions(-) diff --git a/controllers/account.go b/controllers/account.go index e3618cbb..7a7cb3c0 100644 --- a/controllers/account.go +++ b/controllers/account.go @@ -141,17 +141,14 @@ func (c *ApiController) Logout() { // @Success 200 {object} controllers.Response The Response object // @router /get-account [get] func (c *ApiController) GetAccount() { - var resp Response - - if c.GetSessionUser() == "" { - resp = Response{Status: "error", Msg: "Please sign in first", Data: c.GetSessionUser()} - c.Data["json"] = resp - c.ServeJSON() + userId, ok := c.RequireSignedIn() + if !ok { return } - username := c.GetSessionUser() - user := object.GetUser(username) + var resp Response + + user := object.GetUser(userId) organization := object.GetOrganizationByUser(user) resp = Response{Status: "ok", Msg: "", Data: user, Data2: organization} @@ -166,17 +163,14 @@ func (c *ApiController) GetAccount() { // @Success 200 {object} controllers.Response The Response object // @router /upload-avatar [post] func (c *ApiController) UploadAvatar() { - var resp Response - - username := c.GetSessionUser() - if c.GetSessionUser() == "" { - resp = Response{Status: "error", Msg: "Please sign in first", Data: c.GetSessionUser()} - c.Data["json"] = resp - c.ServeJSON() + userId, ok := c.RequireSignedIn() + if !ok { return } - user := object.GetUser(username) + var resp Response + + user := object.GetUser(userId) avatarBase64 := c.Ctx.Request.Form.Get("avatarfile") index := strings.Index(avatarBase64, ",") diff --git a/controllers/link.go b/controllers/link.go index 2e335d21..814dd257 100644 --- a/controllers/link.go +++ b/controllers/link.go @@ -25,15 +25,13 @@ type LinkForm struct { } func (c *ApiController) Unlink() { - var resp Response - - if c.GetSessionUser() == "" { - resp = Response{Status: "error", Msg: "Please sign in first", Data: c.GetSessionUser()} - c.Data["json"] = resp - c.ServeJSON() + userId, ok := c.RequireSignedIn() + if !ok { return } + var resp Response + var form LinkForm err := json.Unmarshal(c.Ctx.Input.RequestBody, &form) if err != nil { @@ -41,7 +39,6 @@ func (c *ApiController) Unlink() { } providerType := form.ProviderType - userId := c.GetSessionUser() user := object.GetUser(userId) value := object.GetUserField(user, providerType) diff --git a/controllers/util.go b/controllers/util.go index 18b4a9c3..37f4936e 100644 --- a/controllers/util.go +++ b/controllers/util.go @@ -28,7 +28,7 @@ func InitHttpClient() { if err != nil { panic(err) } - if !useProxy{ + if !useProxy { httpClient = &http.Client{} return } @@ -57,3 +57,14 @@ func (c *ApiController) ResponseError(error string) { c.Data["json"] = Response{Status: "error", Msg: error} c.ServeJSON() } + +func (c *ApiController) RequireSignedIn() (string, bool) { + userId := c.GetSessionUser() + if userId == "" { + resp := Response{Status: "error", Msg: "Please sign in first"} + c.Data["json"] = resp + c.ServeJSON() + return "", false + } + return userId, true +} diff --git a/controllers/verification.go b/controllers/verification.go index 11b08d68..b0cb5dab 100644 --- a/controllers/verification.go +++ b/controllers/verification.go @@ -23,11 +23,11 @@ import ( ) func (c *ApiController) SendVerificationCode() { - userId := c.GetSessionUser() - if len(userId) == 0 { - c.ResponseError("Please sign in first") + userId, ok := c.RequireSignedIn() + if !ok { return } + user := object.GetUser(userId) if user == nil { c.ResponseError("No such user.") @@ -77,11 +77,11 @@ func (c *ApiController) SendVerificationCode() { } func (c *ApiController) ResetEmailOrPhone() { - userId := c.GetSessionUser() - if len(userId) == 0 { - c.ResponseError("Please sign in first") + userId, ok := c.RequireSignedIn() + if !ok { return } + user := object.GetUser(userId) if user == nil { c.ResponseError("No such user.")