Added auth to the container.

This commit is contained in:
mikestefanello 2021-12-15 09:29:43 -05:00
parent c9d50cb3d4
commit a33a76f8bc
9 changed files with 81 additions and 32 deletions

View file

@ -6,25 +6,20 @@ import (
"goweb/auth"
"goweb/context"
"goweb/ent"
"goweb/ent/user"
"github.com/labstack/echo/v4"
)
func LoadAuthenticatedUser(orm *ent.Client) echo.MiddlewareFunc {
func LoadAuthenticatedUser(authClient *auth.Client) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if userID, err := auth.GetUserID(c); err == nil {
u, err := orm.User.Query().
Where(user.ID(userID)).
First(c.Request().Context())
if user, err := authClient.GetAuthenticatedUser(c); err == nil {
switch err.(type) {
case *ent.NotFoundError:
c.Logger().Debug("auth user not found: %d", userID)
c.Logger().Debug("auth user not found")
case nil:
c.Set(context.AuthenticatedUserKey, u)
c.Logger().Info("auth user loaded in to context: %d", userID)
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)
}