Initial commit of password reset workflow.
This commit is contained in:
parent
b4de8e58f9
commit
e6a5fa58c7
6 changed files with 184 additions and 16 deletions
|
|
@ -6,6 +6,7 @@ import (
|
|||
"goweb/auth"
|
||||
"goweb/context"
|
||||
"goweb/ent"
|
||||
"goweb/msg"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
|
@ -13,16 +14,16 @@ import (
|
|||
func LoadAuthenticatedUser(authClient *auth.Client) echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
if user, err := authClient.GetAuthenticatedUser(c); err == nil {
|
||||
switch err.(type) {
|
||||
case *ent.NotFoundError:
|
||||
c.Logger().Debug("auth user not found")
|
||||
case nil:
|
||||
c.Set(context.AuthenticatedUserKey, user)
|
||||
c.Logger().Info("auth user loaded in to context: %d", user.ID)
|
||||
default:
|
||||
c.Logger().Errorf("error querying for authenticated user: %v", err)
|
||||
}
|
||||
u, err := authClient.GetAuthenticatedUser(c)
|
||||
switch err.(type) {
|
||||
case *ent.NotFoundError:
|
||||
c.Logger().Debug("auth user not found")
|
||||
case auth.NotAuthenticatedError:
|
||||
case nil:
|
||||
c.Set(context.AuthenticatedUserKey, u)
|
||||
c.Logger().Info("auth user loaded in to context: %d", u.ID)
|
||||
default:
|
||||
c.Logger().Errorf("error querying for authenticated user: %v", err)
|
||||
}
|
||||
|
||||
return next(c)
|
||||
|
|
@ -30,6 +31,28 @@ func LoadAuthenticatedUser(authClient *auth.Client) echo.MiddlewareFunc {
|
|||
}
|
||||
}
|
||||
|
||||
func LoadValidPasswordToken(authClient *auth.Client) echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
tokenParam := c.Param("password_token")
|
||||
if tokenParam == "" {
|
||||
c.Logger().Warn("missing password token path parameter")
|
||||
return echo.NewHTTPError(http.StatusNotFound, "Not found")
|
||||
}
|
||||
|
||||
token, err := authClient.GetValidPasswordToken(c, tokenParam)
|
||||
if err != nil {
|
||||
msg.Warning(c, "The link is either invalid or has expired. Please request a new one.")
|
||||
return c.Redirect(http.StatusFound, c.Echo().Reverse("forgot_password"))
|
||||
}
|
||||
|
||||
c.Set(context.PasswordTokenKey, token)
|
||||
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func RequireAuthentication() echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue