Skip to content

Commit

Permalink
[wip]メール送信部分実装
Browse files Browse the repository at this point in the history
  • Loading branch information
KazumaSun committed Feb 21, 2024
1 parent 469c4b4 commit 7234c51
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 0 deletions.
20 changes: 20 additions & 0 deletions api/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1991,6 +1991,26 @@ const docTemplate = `{
},
},
},
"/mail_auth/reset_password":{
"post": {
tags: ["email"],
"description": "パスワードリセットのメール送信",
responses: {
"200": {
"description": "パスワードリセットのメールが送信される",
}
},
"parameters": [
{
"name": "email",
"in": "query",
"description": "email",
"required": true,
"type": "string"
}
],
},
},
},
"definitions":{
"activity":{
Expand Down
3 changes: 3 additions & 0 deletions api/env/dev.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ NUTMEG_DB_PASSWORD="password"
NUTMEG_DB_HOST="nutfes-finansu-db"
NUTMEG_DB_PORT="3306"
NUTMEG_DB_NAME="finansu_db"

NUTMEG_MAIL_SENDER="[email protected]"
NUTMEG_MAIL_PASSWORD="vephvnmuxszssmoa"
11 changes: 11 additions & 0 deletions api/externals/controller/mail_auth_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type MailAuthController interface {
SignIn(echo.Context) error
SignOut(echo.Context) error
IsSignIn(echo.Context) error
ResetPassword(echo.Context) error
}

func NewMailAuthController(u usecase.MailAuthUseCase) MailAuthController {
Expand Down Expand Up @@ -69,3 +70,13 @@ func (auth *mailAuthController) IsSignIn(c echo.Context) error {
c.JSON(http.StatusOK, isSignIn)
return nil
}

// reset password
func (auth *mailAuthController) ResetPassword(c echo.Context) error {
email := c.QueryParam("email")
err := auth.u.ResetPassword(c.Request().Context(), email)
if err != nil {
return err
}
return nil
}
31 changes: 31 additions & 0 deletions api/externals/repository/mail_auth_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package repository

import (
"context"
"net/smtp"
"database/sql"
"github.com/NUTFes/FinanSu/api/drivers/db"
"github.com/joho/godotenv"
"fmt"
"os"
)

type mailAuthRepository struct {
Expand All @@ -15,6 +18,7 @@ type MailAuthRepository interface {
CreateMailAuth(context.Context, string, string, string) (int64, error)
FindMailAuthByEmail(context.Context, string) *sql.Row
FindMailAuthByID(context.Context, string) *sql.Row
ResetPassword(context.Context, []string) error
}

func NewMailAuthRepository(client db.Client) MailAuthRepository {
Expand Down Expand Up @@ -43,3 +47,30 @@ func (r *mailAuthRepository) FindMailAuthByID(c context.Context, id string) *sql
fmt.Printf("\x1b[36m%s\n", query)
return row
}

// reset password
func (r *mailAuthRepository) ResetPassword(c context.Context, email []string) error {
err := godotenv.Load("env/dev.env")
if err != nil {
fmt.Println(err)
}

mailSender := os.Getenv("NUTMEG_MAIL_SENDER")
mailPassword := os.Getenv("NUTMEG_MAIL_PASSWORD")
message := []byte("test")

smtpHost := "smtp.gmail.com"
smtpPort := "587"

// Authenyication
auth := smtp.PlainAuth("", mailSender, mailPassword, smtpHost)

// send email
err = smtp.SendMail(smtpHost+":"+smtpPort, auth, mailSender, email, message)
if err != nil {
fmt.Println(err)
return err
}
fmt.Println("Email Sent Successfully!")
return nil
}
12 changes: 12 additions & 0 deletions api/internals/usecase/mail_auth_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type MailAuthUseCase interface {
SignIn(context.Context, string, string) (domain.Token, error)
SignOut(context.Context, string) error
IsSignIn(context.Context, string) (domain.IsSignIn, error)
ResetPassword(context.Context, string) error
}

func NewMailAuthUseCase(mailAuthRep rep.MailAuthRepository, sessionRep rep.SessionRepository) MailAuthUseCase {
Expand Down Expand Up @@ -123,3 +124,14 @@ func (u *mailAuthUseCase) IsSignIn(c context.Context, accessToken string) (domai
}
return isSignIn, nil
}

// reset password
func (u *mailAuthUseCase) ResetPassword(c context.Context, email string) error {
receiverEmail := []string{email}
err := u.mailAuthRep.ResetPassword(c, receiverEmail)
if err != nil {
return err
}

return err
}
1 change: 1 addition & 0 deletions api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ func (r router) ProvideRouter(e *echo.Echo) {
e.POST("/mail_auth/signin", r.mailAuthController.SignIn)
e.DELETE("/mail_auth/signout", r.mailAuthController.SignOut)
e.GET("/mail_auth/is_signin", r.mailAuthController.IsSignIn)
e.POST("/mail_auth/reset_password", r.mailAuthController.ResetPassword)

// purchaseitemsのRoute
e.GET("/purchaseitems", r.purchaseItemController.IndexPurchaseItem)
Expand Down

0 comments on commit 7234c51

Please sign in to comment.