Skip to content

Commit

Permalink
feat: /verify/email check only mode, and deprecate /verify/apikey
Browse files Browse the repository at this point in the history
  • Loading branch information
JingYiJun committed Jul 30, 2023
1 parent edb499e commit e877cb2
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 7 deletions.
27 changes: 22 additions & 5 deletions apis/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ func ChangePassword(c *fiber.Ctx) error {
func VerifyWithEmailOld(c *fiber.Ctx) error {
email := c.Params("email")
scope := c.Query("scope")
return verifyWithEmail(c, email, scope)
return verifyWithEmail(c, email, scope, false)
}

// VerifyWithEmail godoc
Expand All @@ -445,17 +445,19 @@ func VerifyWithEmailOld(c *fiber.Ctx) error {
// @Router /verify/email [get]
// @Param email query string true "email"
// @Param scope query string false "scope"
// @Param check query bool false "check"
// @Success 200 {object} EmailVerifyResponse
// @Failure 400 {object} common.MessageResponse
// @Failure 403 {object} common.MessageResponse “email不在白名单中”
// @Failure 500 {object} common.MessageResponse
func VerifyWithEmail(c *fiber.Ctx) error {
email := c.Query("email")
scope := c.Query("scope")
return verifyWithEmail(c, email, scope)
check := c.QueryBool("check")
return verifyWithEmail(c, email, scope, check)
}

func verifyWithEmail(c *fiber.Ctx, email, givenScope string) error {
func verifyWithEmail(c *fiber.Ctx, email, givenScope string, check bool) error {
if !utils.ValidateEmail(email) {
return common.BadRequest("email invalid")
}
Expand All @@ -481,6 +483,19 @@ func verifyWithEmail(c *fiber.Ctx, email, givenScope string) error {
} else {
scope = "reset"
}

if check {
message := "该邮箱已注册"
if !registered {
message = "该邮箱未注册"
}
return c.JSON(EmailVerifyResponse{
Message: message,
Registered: registered,
Scope: scope,
})
}

if givenScope == "register" && scope == "reset" {
return common.BadRequest("该用户已注册,请使用重置密码功能")
} else if givenScope == "reset" && scope == "register" {
Expand Down Expand Up @@ -513,8 +528,9 @@ func verifyWithEmail(c *fiber.Ctx, email, givenScope string) error {
}

return c.JSON(EmailVerifyResponse{
Message: "验证邮件已发送,请查收\n如未收到,请检查邮件地址是否正确,检查垃圾箱,或重试",
Scope: scope,
Message: "验证邮件已发送,请查收\n如未收到,请检查邮件地址是否正确,检查垃圾箱,或重试",
Registered: registered,
Scope: scope,
})
}

Expand All @@ -523,6 +539,7 @@ func verifyWithEmail(c *fiber.Ctx, email, givenScope string) error {
// @Summary verify with email in query and apikey
// @Description verify with email in query, return verification code
// @Tags account
// @Deprecated
// @Produce json
// @Router /verify/apikey [get]
// @Param email query ApikeyRequest true "apikey, email"
Expand Down
5 changes: 3 additions & 2 deletions apis/schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ func (v *VerificationType) UnmarshalText(data []byte) error {
}

type EmailVerifyResponse struct {
Message string `json:"message"`
Scope string `json:"scope" enums:"register,reset"`
Message string `json:"message"`
Registered bool `json:"registered"`
Scope string `json:"scope" enums:"register,reset"`
}

type ApikeyRequest struct {
Expand Down
13 changes: 13 additions & 0 deletions docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,7 @@
"account"
],
"summary": "verify with email in query and apikey",
"deprecated": true,
"parameters": [
{
"type": "string",
Expand Down Expand Up @@ -1042,6 +1043,12 @@
"description": "scope",
"name": "scope",
"in": "query"
},
{
"type": "boolean",
"description": "check",
"name": "check",
"in": "query"
}
],
"responses": {
Expand Down Expand Up @@ -1131,6 +1138,9 @@
"message": {
"type": "string"
},
"registered": {
"type": "boolean"
},
"scope": {
"type": "string",
"enum": [
Expand All @@ -1146,6 +1156,9 @@
"message": {
"type": "string"
},
"registered": {
"type": "boolean"
},
"scope": {
"type": "string",
"enum": [
Expand Down
9 changes: 9 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ definitions:
type: string
message:
type: string
registered:
type: boolean
scope:
enum:
- register
Expand All @@ -16,6 +18,8 @@ definitions:
properties:
message:
type: string
registered:
type: boolean
scope:
enum:
- register
Expand Down Expand Up @@ -961,6 +965,7 @@ paths:
- user
/verify/apikey:
get:
deprecated: true
description: verify with email in query, return verification code
parameters:
- in: query
Expand Down Expand Up @@ -1010,6 +1015,10 @@ paths:
in: query
name: scope
type: string
- description: check
in: query
name: check
type: boolean
produces:
- application/json
responses:
Expand Down

0 comments on commit e877cb2

Please sign in to comment.