Skip to content

Commit

Permalink
🏉
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryo Arima committed Aug 18, 2024
1 parent d3578b9 commit 2ea2d8b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 17 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ services:
container_name: redis
ports:
- "6379:6379"
command: ["redis-server", "--port", "6379", "--requirepass", "mysecretpassword", "--user", "default on >mysecretpassword"]
command: ["redis-server", "--port", "6379", "--requirepass", "mysecretpassword", "--user", "default", "on", ">mysecretpassword", "+@all", "~*"]
15 changes: 10 additions & 5 deletions pkg/config/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ type YamlConfig struct {
}

type Server struct {
Port string `yaml:"port"`
Admin Admin `yaml:"admin"`
Jwt Jwt `yaml:"jwt"`
Mail Mail `yaml:"mail"`
TmpLength int `yaml:"tmplength"`
Port string `yaml:"port"`
Admin Admin `yaml:"admin"`
Jwt Jwt `yaml:"jwt"`
Mail Mail `yaml:"mail"`
Tmp Tmp `yaml:"tmp"`
}

type Tmp struct {
Length int `yaml:"length"`
Letters string `yaml:"letters"`
}

type Mail struct {
Expand Down
26 changes: 23 additions & 3 deletions pkg/server/controller/common_public.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,29 @@ func (commonControllerForPublic commonControllerForPublic) CreateEmail(c *gin.Co
}

func (commonControllerForPublic commonControllerForPublic) VerifyEmail(c *gin.Context) {
var userRequest request.UserRequest
fmt.Println(userRequest)
c.Query("code")
code := c.Query("code")
uuid := c.Query("uuid")
if code == "" {
c.JSON(http.StatusBadRequest, &response.UserResponse{Code: "SERVER_CONTROLLER_VERIFY__FOR__001", Message: "code is required", Users: []response.User{}})
return
} else {
user := commonControllerForPublic.UserRepository.FindUserByUUID(uuid)
tempCode := commonControllerForPublic.CommonRepository.GetTempCode(model.Email{
To: user.Email,
})
fmt.Println("tempCode: ", tempCode)
if code != tempCode {
c.JSON(http.StatusBadRequest, &response.UserResponse{Code: "SERVER_CONTROLLER_VERIFY__FOR__002", Message: "code is invalid", Users: []response.User{}})
return
} else {
commonControllerForPublic.UserRepository.UpdateUser(model.Users{
UUID: uuid,
Status: "EmailVerified",
})
c.JSON(http.StatusOK, &response.UserResponse{Code: "SERVER_CONTROLLER_VERIFY__FOR__003", Message: "ok", Users: []response.User{}})
return
}
}
}

func NewCommonControllerForPublic(userRepository repository.UserRepository, commonRepository repository.CommonRepository) CommonControllerForPublic {
Expand Down
4 changes: 1 addition & 3 deletions pkg/server/middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ func CheckHash(password string, expectedHash string) (bool, error) {
return true, nil
}

func GenTempCode(n int) string {
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

func GenTempCode(n int, letters string) string {
b := make([]byte, n)
if _, err := rand.Read(b); err != nil {
return ""
Expand Down
19 changes: 14 additions & 5 deletions pkg/server/repository/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ func (commonRepository commonRepository) CreateEmail(email model.Email, tempCode
fmt.Println("Error parsing template file:", err)
return
}
var userModel model.Users
commonRepository.BaseConfig.DBConnection.Where("email = ?", email.To).First(&userModel)
email.From = commonRepository.BaseConfig.YamlConfig.Application.Server.Mail.User
email.VeryfyEmailURL = commonRepository.BaseConfig.YamlConfig.Application.Client.ServerEndpoint + "/api/public/email?code=" + tempCode
email.VeryfyEmailURL = commonRepository.BaseConfig.YamlConfig.Application.Client.ServerEndpoint + "/api/public/email?code=" + tempCode + "&uuid=" + userModel.UUID

var emailContent bytes.Buffer
err = tmpl.Execute(&emailContent, email)
Expand All @@ -44,7 +46,6 @@ func (commonRepository commonRepository) CreateEmail(email model.Email, tempCode
}

toMailAddress := []string{email.To}
fmt.Println(emailContent.String())
reader := strings.NewReader(emailContent.String())
transformer := japanese.ISO2022JP.NewEncoder()
msgISO2022JP, err := io.ReadAll(transform.NewReader(reader, transformer))
Expand All @@ -63,10 +64,16 @@ func (commonRepository commonRepository) CreateEmail(email model.Email, tempCode
}

func (commonRepository commonRepository) SetTempCode(email model.Email) string {
length := commonRepository.BaseConfig.YamlConfig.Application.Server.TmpLength
tempCode := middleware.GenTempCode(length)
length := commonRepository.BaseConfig.YamlConfig.Application.Server.Tmp.Length
letters := commonRepository.BaseConfig.YamlConfig.Application.Server.Tmp.Letters
tempCode := middleware.GenTempCode(length, letters)
context := commonRepository.BaseConfig.RedisConf.Ctx
commonRepository.BaseConfig.RedisConf.RedisConnection.Set(context, email.To, tempCode, time.Hour)
err := commonRepository.BaseConfig.RedisConf.RedisConnection.Set(context, email.To, tempCode, time.Hour).Err()
if err != nil {
fmt.Println("Error setting key:", err)
}
fmt.Println("+++++++++++++++++")
fmt.Println(tempCode)
return tempCode
}

Expand All @@ -76,6 +83,8 @@ func (commonRepository commonRepository) GetTempCode(email model.Email) string {
if err != nil {
fmt.Println(err)
}
fmt.Println("+++++++++++++++++")
fmt.Println(tempCode)
return tempCode
}

Expand Down

0 comments on commit 2ea2d8b

Please sign in to comment.