Skip to content

Commit

Permalink
feat(middleware): add AccountVerifiedMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
pcfreak30 committed Sep 18, 2024
1 parent c07e18a commit ab98860
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions middleware/account_verified.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package middleware

import (
"go.lumeweb.com/portal/core"
"net/http"
)

func AccountVerifiedMiddleware(ctx core.Context) func(http.Handler) http.Handler {
userService := ctx.Service(core.USER_SERVICE).(core.UserService)

return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

user, err := GetUserFromContext(r.Context())
if err != nil {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}

verified, err := userService.IsAccountVerified(user)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

if !verified {
acctErr := core.NewAccountError(core.ErrKeyAccountNotVerified, nil)
http.Error(w, acctErr.Error(), acctErr.HttpStatus())
return
}

next.ServeHTTP(w, r)
})
}
}

0 comments on commit ab98860

Please sign in to comment.