Added user email verification support.

This commit is contained in:
mikestefanello 2022-01-08 15:32:18 -05:00
parent feb11bbe5b
commit ea46a38f68
31 changed files with 417 additions and 85 deletions

View file

@ -1,6 +1,8 @@
package routes
import (
"fmt"
"github.com/mikestefanello/pagoda/context"
"github.com/mikestefanello/pagoda/controller"
"github.com/mikestefanello/pagoda/ent"
@ -86,6 +88,31 @@ func (c *Register) Post(ctx echo.Context) error {
return c.Redirect(ctx, "login")
}
msg.Info(ctx, "Your account has been created. You are now logged in.")
msg.Success(ctx, "Your account has been created. You are now logged in.")
// Send the verification email
c.sendVerificationEmail(ctx, u)
return c.Redirect(ctx, "home")
}
func (c *Register) sendVerificationEmail(ctx echo.Context, usr *ent.User) {
// Generate a token
token, err := c.Container.Auth.GenerateEmailVerificationToken(usr.Email)
if err != nil {
ctx.Logger().Errorf("unable to generate email verification token: %v", err)
return
}
// Send the email
err = c.Container.Mail.Send(ctx, usr.Email, fmt.Sprintf(
"Confirm your email address: %s",
ctx.Echo().Reverse("verify_email", token),
))
if err != nil {
ctx.Logger().Errorf("unable to send email verification token: %v", err)
return
}
msg.Info(ctx, "An email was sent to you to verify your email address.")
}

View file

@ -83,6 +83,9 @@ func userRoutes(c *services.Container, g *echo.Group, ctr controller.Controller)
logout := Logout{Controller: ctr}
g.GET("/logout", logout.Get, middleware.RequireAuthentication()).Name = "logout"
verifyEmail := VerifyEmail{Controller: ctr}
g.GET("/email/verify/:token", verifyEmail.Get).Name = "verify_email"
noAuth := g.Group("/user", middleware.RequireNoAuthentication())
login := Login{Controller: ctr}
noAuth.GET("/login", login.Get).Name = "login"

70
routes/verify_email.go Normal file
View file

@ -0,0 +1,70 @@
package routes
import (
"github.com/labstack/echo/v4"
"github.com/mikestefanello/pagoda/context"
"github.com/mikestefanello/pagoda/controller"
"github.com/mikestefanello/pagoda/ent"
"github.com/mikestefanello/pagoda/ent/user"
"github.com/mikestefanello/pagoda/msg"
)
type VerifyEmail struct {
controller.Controller
}
func (c *VerifyEmail) Get(ctx echo.Context) error {
c.verifyToken(ctx)
return c.Redirect(ctx, "home")
}
func (c *VerifyEmail) verifyToken(ctx echo.Context) {
var usr *ent.User
// Validate the token
token := ctx.Param("token")
email, err := c.Container.Auth.ValidateEmailVerificationToken(token)
if err != nil {
msg.Warning(ctx, "The link is either invalid or has expired.")
return
}
// Check if it matches the authenticated user
if u := ctx.Get(context.AuthenticatedUserKey); u != nil {
authUser := u.(*ent.User)
if authUser.Email == email {
usr = authUser
}
}
// Query to find a matching user, if needed
if usr == nil {
usr, err = c.Container.ORM.User.
Query().
Where(user.Email(email)).
Only(ctx.Request().Context())
if err != nil {
ctx.Logger().Errorf("error querying user during email verification: %v", err)
msg.Danger(ctx, "An error occurred. Please try again.")
return
}
}
// Verify the user
err = c.Container.ORM.User.
Update().
SetVerified(true).
Where(user.ID(usr.ID)).
Exec(ctx.Request().Context())
if err != nil {
ctx.Logger().Errorf("error setting user as verified: %v", err)
msg.Danger(ctx, "An error occurred. Please try again.")
return
}
msg.Success(ctx, "You email has been successfully verified.")
}