diff --git a/docs/docs.go b/docs/docs.go index 54654de2d2148676582e965b09635b00bd1d1d88..370840d6e684f8ef343ad0986bffd965e7669f94 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -359,9 +359,9 @@ const docTemplate = `{ } } }, - "/auth/send-verify": { + "/auth/verify": { "post": { - "description": "Send Email Verification to user", + "description": "Do Email Verification to user", "consumes": [ "application/json" ], @@ -371,7 +371,7 @@ const docTemplate = `{ "tags": [ "auth" ], - "summary": "Send Email Verification", + "summary": "Do Email Verification", "parameters": [ { "description": "Register Payload", @@ -379,7 +379,7 @@ const docTemplate = `{ "in": "body", "required": true, "schema": { - "$ref": "#/definitions/verification.VerificationSendRequestPayload" + "$ref": "#/definitions/verification.VerificationRequestPayload" } } ], @@ -405,9 +405,9 @@ const docTemplate = `{ } } }, - "/auth/verify": { + "/auth/verify/resend": { "post": { - "description": "Do Email Verification to user", + "description": "Send Email Verification to user", "consumes": [ "application/json" ], @@ -417,7 +417,7 @@ const docTemplate = `{ "tags": [ "auth" ], - "summary": "Do Email Verification", + "summary": "Send Email Verification", "parameters": [ { "description": "Register Payload", @@ -715,6 +715,18 @@ const docTemplate = `{ } } }, + "verification.VerificationRequestPayload": { + "type": "object", + "required": [ + "id" + ], + "properties": { + "id": { + "type": "string", + "example": "6ba7b812-9dad-11d1-80b4-00c04fd430c8" + } + } + }, "verification.VerificationSendRequestPayload": { "description": "Information that should be passed when request verify", "type": "object", diff --git a/docs/swagger.json b/docs/swagger.json index 261ea429ddbc17681e498576b9f8336f36db9192..acbb09b9750eddb9621a4c8509271d994b3c6227 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -351,9 +351,9 @@ } } }, - "/auth/send-verify": { + "/auth/verify": { "post": { - "description": "Send Email Verification to user", + "description": "Do Email Verification to user", "consumes": [ "application/json" ], @@ -363,7 +363,7 @@ "tags": [ "auth" ], - "summary": "Send Email Verification", + "summary": "Do Email Verification", "parameters": [ { "description": "Register Payload", @@ -371,7 +371,7 @@ "in": "body", "required": true, "schema": { - "$ref": "#/definitions/verification.VerificationSendRequestPayload" + "$ref": "#/definitions/verification.VerificationRequestPayload" } } ], @@ -397,9 +397,9 @@ } } }, - "/auth/verify": { + "/auth/verify/resend": { "post": { - "description": "Do Email Verification to user", + "description": "Send Email Verification to user", "consumes": [ "application/json" ], @@ -409,7 +409,7 @@ "tags": [ "auth" ], - "summary": "Do Email Verification", + "summary": "Send Email Verification", "parameters": [ { "description": "Register Payload", @@ -707,6 +707,18 @@ } } }, + "verification.VerificationRequestPayload": { + "type": "object", + "required": [ + "id" + ], + "properties": { + "id": { + "type": "string", + "example": "6ba7b812-9dad-11d1-80b4-00c04fd430c8" + } + } + }, "verification.VerificationSendRequestPayload": { "description": "Information that should be passed when request verify", "type": "object", diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 621aafeb5b409b9bce2389736ecb41ad361a070c..f65ec1a9bd886e3c6fd7460ea165043dab217241 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -123,6 +123,14 @@ definitions: required: - email type: object + verification.VerificationRequestPayload: + properties: + id: + example: 6ba7b812-9dad-11d1-80b4-00c04fd430c8 + type: string + required: + - id + type: object verification.VerificationSendRequestPayload: description: Information that should be passed when request verify properties: @@ -367,18 +375,18 @@ paths: summary: Register New Account tags: - auth - /auth/send-verify: + /auth/verify: post: consumes: - application/json - description: Send Email Verification to user + description: Do Email Verification to user parameters: - description: Register Payload in: body name: data required: true schema: - $ref: '#/definitions/verification.VerificationSendRequestPayload' + $ref: '#/definitions/verification.VerificationRequestPayload' produces: - application/json responses: @@ -394,14 +402,14 @@ paths: description: Internal Server Error schema: $ref: '#/definitions/web.BaseResponse' - summary: Send Email Verification + summary: Do Email Verification tags: - auth - /auth/verify: + /auth/verify/resend: post: consumes: - application/json - description: Do Email Verification to user + description: Send Email Verification to user parameters: - description: Register Payload in: body @@ -424,7 +432,7 @@ paths: description: Internal Server Error schema: $ref: '#/definitions/web.BaseResponse' - summary: Do Email Verification + summary: Send Email Verification tags: - auth /reset/confirm: diff --git a/handler/auth/send_verify.go b/handler/auth/send_verify.go index c0505b7f85460b162da5349f9d3de291e9b844c0..b2b4a2e70eaca89ac5de5f26a76fb4210d894416 100644 --- a/handler/auth/send_verify.go +++ b/handler/auth/send_verify.go @@ -20,7 +20,7 @@ import ( // @Success 200 {object} web.BaseResponse // @Failure 400 {object} web.BaseResponse // @Failure 500 {object} web.BaseResponse -// @Router /auth/send-verify [post] +// @Router /auth/verify/resend [post] func (a AuthHandlerImpl) SendEmailVerify(w http.ResponseWriter, r *http.Request) { payload := verification.VerificationSendRequestPayload{} validate := validator.New() diff --git a/handler/auth/verify.go b/handler/auth/verify.go index aba414b5568d4161de9f74147ef5bd55cac88ee6..a118518f67d6d8e4dff329541d93d406f545cbca 100644 --- a/handler/auth/verify.go +++ b/handler/auth/verify.go @@ -16,7 +16,7 @@ import ( // @Description Do Email Verification to user // @Produce json // @Accept json -// @Param data body verification.VerificationSendRequestPayload true "Register Payload" +// @Param data body verification.VerificationRequestPayload true "Register Payload" // @Success 200 {object} web.BaseResponse // @Failure 400 {object} web.BaseResponse // @Failure 500 {object} web.BaseResponse diff --git a/routes/auth/route.go b/routes/auth/route.go index f9d8b6b9d24b5fa6ff2ba7b475e5ecdb18ee0fe7..e81bb261069b4abc287c84ec793901ca42a9529e 100644 --- a/routes/auth/route.go +++ b/routes/auth/route.go @@ -14,7 +14,7 @@ func (ar AuthRoutes) Register(r chi.Router) { r.Post("/login", ar.AuthHandler.Login) r.Post("/refresh", ar.AuthHandler.Refresh) r.Post("/register", ar.AuthHandler.Register) - r.Post("/send-verify", ar.AuthHandler.SendEmailVerify) - r.Get("/verify", ar.AuthHandler.EmailVerify) + r.Post("/verify/resend", ar.AuthHandler.SendEmailVerify) + r.Post("/verify", ar.AuthHandler.EmailVerify) }) } diff --git a/service/verification/send.go b/service/verification/send.go index 75ab1ed4035bea50cefe705db0d566de84e49ff6..cfedd802ffa95cfb9aece68168ed30ae4c3ca580 100644 --- a/service/verification/send.go +++ b/service/verification/send.go @@ -1,24 +1,43 @@ package verification import ( + "errors" "time" "github.com/google/uuid" "gitlab.informatika.org/ocw/ocw-backend/model/domain/cache" + "gitlab.informatika.org/ocw/ocw-backend/model/web" "gitlab.informatika.org/ocw/ocw-backend/provider/mail" + "gorm.io/gorm" ) func (v VerificationServiceImpl) SendVerifyMail(email string) error { + _, err := v.UserRepository.Get(email) + + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return web.NewResponseError("user not found", web.EmailNotExist) + } + + return err + } + res, err := v.CacheRepository.GetInteger(cache.Key{ Id: v.RedisPrefixKey + "verify:cnt:" + email, }) - if err != nil || res > v.Environment.EmailVerificationMaxRetry { - return err + if err != nil { + if err.Error() != "redigo: nil returned" { + return err + } + } + + if res > v.Environment.EmailVerificationMaxRetry { + return nil } id := uuid.New().String() - v.CacheRepository.Incr(v.RedisPrefixKey+"verify:cnt"+email, v.EmailVerificationRetryInterval*int64(time.Minute)) + v.CacheRepository.Incr(v.RedisPrefixKey+"verify:cnt:"+email, v.EmailVerificationRetryInterval*int64(time.Minute)) v.CacheRepository.Set(cache.String{ Key: cache.Key{ Id: v.RedisPrefixKey + "verify:id:" + id, diff --git a/service/verification/verify.go b/service/verification/verify.go index 7ac77d294003b5e538cc6490f2dd5662f3726a0b..4bfd2c115bb5f07811699599415be37bee5151da 100644 --- a/service/verification/verify.go +++ b/service/verification/verify.go @@ -2,7 +2,6 @@ package verification import ( "gitlab.informatika.org/ocw/ocw-backend/model/domain/cache" - "gitlab.informatika.org/ocw/ocw-backend/model/domain/user" "gitlab.informatika.org/ocw/ocw-backend/model/web" ) @@ -20,10 +19,15 @@ func (v VerificationServiceImpl) DoVerification(id string) error { return web.NewResponseErrorf("VERIFY", "id '%s' is not valid", id) } - v.UserRepository.Update(user.User{ - Email: email, - IsActivated: true, - }) + data, err := v.UserRepository.Get(email) + + if err != nil { + return web.NewResponseErrorf("VERIFY", "username '%s' is not found", email) + } + + data.IsActivated = true + + v.UserRepository.Update(*data) return nil }