-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from hhertout/dev
feat: adding auth pre formatted requests
- Loading branch information
Showing
4 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |