-
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.
- Loading branch information
1 parent
2bad342
commit 451b4de
Showing
7 changed files
with
235 additions
and
29 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,79 @@ | ||
package auth | ||
|
||
import ( | ||
"fmt" | ||
"gotaskapp/app/config" | ||
fail "gotaskapp/app/failures" | ||
"gotaskapp/app/helpers" | ||
"gotaskapp/app/repositories/auth" | ||
"net/http" | ||
"strings" | ||
|
||
sentrygin "github.com/getsentry/sentry-go/gin" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
var emailRequestPasswordResetbody = ` | ||
<html> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | ||
<title>Password reset</title> | ||
</head> | ||
<body> | ||
<p>Password reset link <b>Go TaskApp</b></p> | ||
<p>Click this link <a href="{{LINK}}">here</a> to reset your password.</p> | ||
<p>If you have not requested this link, ignore it.</p> | ||
</body> | ||
` | ||
|
||
type sendEmailPasswordReset struct { | ||
Email string `form:"email" binding:"required,email"` | ||
} | ||
|
||
// Request password reset | ||
func SendEmailPasswordReset(c *gin.Context) { | ||
|
||
var form sendEmailPasswordReset | ||
|
||
if err := c.ShouldBind(&form); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
credential, err := auth.SendEmailPasswordReset(form.Email) | ||
|
||
if err != nil { | ||
switch err.(type) { | ||
case *fail.DatabaseConnectFailure, | ||
*fail.SqlSelectFailure, | ||
*fail.GenerateJwtTokenFailure: | ||
if hub := sentrygin.GetHubFromContext(c); hub != nil { | ||
hub.CaptureException(err) | ||
} | ||
helpers.ApiResponse(c, false, http.StatusInternalServerError, "Server internal error", nil) | ||
return | ||
case *fail.SqlSelectNotFoundFailure: | ||
helpers.ApiResponse(c, false, http.StatusNotFound, "email not linked a account", nil) | ||
return | ||
default: | ||
helpers.ApiResponse(c, false, http.StatusInternalServerError, "an unexpected error occurred", nil) | ||
return | ||
} | ||
} | ||
|
||
link := fmt.Sprintf("http://%s%s%s", config.APP_HOST_FULL, "/auth/password/reset/", credential.Token) | ||
|
||
emailRequestPasswordResetbody = strings.ReplaceAll(emailRequestPasswordResetbody, "{{LINK}}", link) | ||
|
||
err = helpers.SendEmail([]string{credential.User.Email}, []string{}, "Password Reset", emailRequestPasswordResetbody) | ||
|
||
if err != nil { | ||
if hub := sentrygin.GetHubFromContext(c); hub != nil { | ||
hub.CaptureException(err) | ||
} | ||
helpers.ApiResponse(c, false, http.StatusInternalServerError, "Server internal error to send email verification", nil) | ||
return | ||
} | ||
|
||
helpers.ApiResponse(c, true, http.StatusOK, "A password reset link has been sent to your email, if it's not in your inbox check your span.", nil) | ||
} |
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,80 @@ | ||
package auth | ||
|
||
import ( | ||
"fmt" | ||
"gotaskapp/app/config" | ||
fail "gotaskapp/app/failures" | ||
"gotaskapp/app/helpers" | ||
"gotaskapp/app/repositories/auth" | ||
"net/http" | ||
"strings" | ||
|
||
sentrygin "github.com/getsentry/sentry-go/gin" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
var emailSignUpbody = ` | ||
<html> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | ||
<title>Link de confirmação da conta</title> | ||
</head> | ||
<body> | ||
<p>Clique neste link <a href="{{LINK}}">aqui</a> para confirmar seu email.</p> | ||
<p>Caso não tenha criado uma conta ignore este email.</p> | ||
</body> | ||
` | ||
|
||
type sendEmailVerification struct { | ||
Email string `form:"email" binding:"required,email"` | ||
} | ||
|
||
func SendEmailVerification(c *gin.Context) { | ||
|
||
var form sendEmailVerification | ||
|
||
if err := c.ShouldBind(&form); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
credential, err := auth.SendEmailVerification(form.Email) | ||
|
||
if err != nil { | ||
switch err.(type) { | ||
case *fail.DatabaseConnectFailure, | ||
*fail.SqlSelectFailure, | ||
*fail.GenerateJwtTokenFailure: | ||
if hub := sentrygin.GetHubFromContext(c); hub != nil { | ||
hub.CaptureException(err) | ||
} | ||
helpers.ApiResponse(c, false, http.StatusInternalServerError, "Server internal error", nil) | ||
return | ||
case *fail.SendEmailVerificationFailure: | ||
helpers.ApiResponse(c, false, http.StatusBadRequest, err.Error(), nil) | ||
return | ||
case *fail.SqlSelectNotFoundFailure: | ||
helpers.ApiResponse(c, false, http.StatusNotFound, "email not linked a account", nil) | ||
return | ||
default: | ||
helpers.ApiResponse(c, false, http.StatusInternalServerError, "an unexpected error occurred", nil) | ||
return | ||
} | ||
} | ||
|
||
go func() { | ||
link := fmt.Sprintf("%s%s%s", config.APP_URL, "/auth/email/verify/", credential.Token) | ||
|
||
emailSignUpbody = strings.ReplaceAll(emailSignUpbody, "{{LINK}}", link) | ||
|
||
err = helpers.SendEmail([]string{credential.User.Email}, []string{}, "Confirmação de email", emailSignUpbody) | ||
|
||
if err != nil { | ||
if hub := sentrygin.GetHubFromContext(c); hub != nil { | ||
hub.CaptureException(err) | ||
} | ||
} | ||
}() | ||
|
||
helpers.ApiResponse(c, true, http.StatusOK, "A link has been sent to your email, if it's not in your inbox check your box span.", nil) | ||
} |
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
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,39 @@ | ||
package auth | ||
|
||
import ( | ||
"gotaskapp/app/database" | ||
"gotaskapp/app/entities" | ||
fail "gotaskapp/app/failures" | ||
"gotaskapp/app/security" | ||
"time" | ||
) | ||
|
||
func SendEmailVerification(email string) (entities.Credential, error) { | ||
|
||
datasources, err := database.Datasources() | ||
|
||
if err != nil { | ||
return entities.Credential{}, err | ||
} | ||
|
||
user, err := datasources.User.ByEmail(email) | ||
|
||
if err != nil { | ||
return entities.Credential{}, err | ||
} | ||
|
||
if user.IsEmailVerified() { | ||
return entities.Credential{}, &fail.SendEmailVerificationFailure{M: "The email " + user.Email + " has already been verified.", E: err} | ||
} | ||
|
||
token, err := security.GenerateJwtToken(user.ID, time.Hour*6) | ||
|
||
if err != nil { | ||
return entities.Credential{}, &fail.GenerateJwtTokenFailure{M: "error generate jwt token", E: err} | ||
} | ||
|
||
return entities.Credential{ | ||
User: user, | ||
Token: token, | ||
}, nil | ||
} |
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