Skip to content

Commit

Permalink
Merge pull request #8 from hhertout/dev
Browse files Browse the repository at this point in the history
feat: adding auth pre formatted requests
  • Loading branch information
hhertout authored Dec 31, 2023
2 parents bf05cdc + 7580efe commit c02dbb5
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/controllers/authentication.controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package controllers

import (
"github.com/gin-gonic/gin"
"mailer_ms/src/mailer"
"net/http"
)

func (a ApiController) UpdatePasswordConfirmation(c *gin.Context) {
type data struct {
Email string `json:"email"`
}
var body MailRequest[data]

if err := c.BindJSON(&body); err != nil {
_ = a.repository.SaveWithError(body.To, body.Subject, err)
c.JSON(http.StatusBadRequest, gin.H{
"message": "bad request",
})
return
}

r := mailer.NewRequest(body.Subject, body.To)
_, err := r.ParseHTMLTemplate("passwordUpdated.html", body.Body)
if err != nil {
_ = a.repository.SaveWithError(body.To, body.Subject, err)
c.JSON(http.StatusBadRequest, gin.H{
"message": "Error: Failed to parse html template with given variables",
})
return
}

if err = a.mailer.SendEmail(r); err != nil {
_ = a.repository.SaveWithError(body.To, body.Subject, err)
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Internal server Error",
})
}

_ = a.repository.SaveWithoutError(body.To, body.Subject)
c.JSON(http.StatusOK, gin.H{
"message": "Email successfully sent",
})
}

func (a ApiController) PasswordReinitialisation(c *gin.Context) {
type data struct {
Password string `json:"password"`
}
var body MailRequest[data]

if err := c.BindJSON(&body); err != nil {
_ = a.repository.SaveWithError(body.To, body.Subject, err)
c.JSON(http.StatusBadRequest, gin.H{
"message": "bad request",
})
return
}

r := mailer.NewRequest(body.Subject, body.To)
_, err := r.ParseHTMLTemplate("passwordReinitialisation.html", body.Body)
if err != nil {
_ = a.repository.SaveWithError(body.To, body.Subject, err)
c.JSON(http.StatusBadRequest, gin.H{
"message": "Error: Failed to parse html template with given variables",
})
return
}

if err = a.mailer.SendEmail(r); err != nil {
_ = a.repository.SaveWithError(body.To, body.Subject, err)
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Internal server Error",
})
}

_ = a.repository.SaveWithoutError(body.To, body.Subject)
c.JSON(http.StatusOK, gin.H{
"message": "Email successfully sent",
})
}
3 changes: 3 additions & 0 deletions src/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@ func Serve() *gin.Engine {
r.GET("/api/mailer/list", c.GetMails)
r.POST("/api/mailer/helloworld", c.HelloWorldWithHtml)

r.POST("/api/mailer/password-updated", c.UpdatePasswordConfirmation)
r.POST("/api/mailer/password-reset", c.PasswordReinitialisation)

return r
}
10 changes: 10 additions & 0 deletions templates/passwordReinitialisation.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Password reinitialisation</title>
</head>
<body>
<table>Your new password : {{.Password}}</table>
</body>
</html>
10 changes: 10 additions & 0 deletions templates/passwordUpdated.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Password updated</title>
</head>
<body>
<table>Password updated for your account</table>
</body>
</html>

0 comments on commit c02dbb5

Please sign in to comment.