refac: runtime settings are read once at startup and passed in through the composition root; no handler, router or repository reads environment variables
This commit is contained in:
1 parent
e474f03d91
commit
0b4e614ec9
15 files changed
+722
-66
No files matched your search
@@ -6,7 +6,6 @@ import (
|
||||
"RenewCMS/internal/infrastructure/useCases"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/jwtauth/v5"
|
||||
@@ -18,6 +17,7 @@ type Handler struct {
|
||||
ListUseCase *useCases.ListUsersUseCase
|
||||
CreateUseCase *useCases.CreateUserUseCase
|
||||
AuthenticateUseCase *useCases.AuthenticateUserUseCase
|
||||
IsProduction bool
|
||||
}
|
||||
|
||||
type RegisterCredentials struct {
|
||||
@@ -31,29 +31,26 @@ type LoginCredentials struct {
|
||||
Password string `json:"password" validate:"required,min=8"`
|
||||
}
|
||||
|
||||
func shouldCookieBeSecure() bool {
|
||||
return os.Getenv("ENVIRONMENT") == "production"
|
||||
}
|
||||
|
||||
func removeJwtCookie(w http.ResponseWriter) {
|
||||
func (h *Handler) removeJwtCookie(w http.ResponseWriter) {
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: "jwt",
|
||||
Value: "",
|
||||
Expires: time.Now(),
|
||||
MaxAge: -1,
|
||||
Secure: shouldCookieBeSecure(),
|
||||
Secure: h.IsProduction,
|
||||
HttpOnly: true,
|
||||
Path: "/",
|
||||
})
|
||||
}
|
||||
|
||||
func NewHandler(token *jwtauth.JWTAuth, get *useCases.GetUserUseCase, list *useCases.ListUsersUseCase, create *useCases.CreateUserUseCase, authenticate *useCases.AuthenticateUserUseCase) *Handler {
|
||||
func NewHandler(token *jwtauth.JWTAuth, get *useCases.GetUserUseCase, list *useCases.ListUsersUseCase, create *useCases.CreateUserUseCase, authenticate *useCases.AuthenticateUserUseCase, isProduction bool) *Handler {
|
||||
return &Handler{
|
||||
Token: token,
|
||||
GetUseCase: get,
|
||||
ListUseCase: list,
|
||||
CreateUseCase: create,
|
||||
AuthenticateUseCase: authenticate,
|
||||
IsProduction: isProduction,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,7 +63,7 @@ func (h *Handler) SetJwtCookie(w *http.ResponseWriter, userId uint32) error {
|
||||
Name: "jwt",
|
||||
Value: tokenString,
|
||||
Expires: time.Now().Add(24 * time.Hour),
|
||||
Secure: shouldCookieBeSecure(),
|
||||
Secure: h.IsProduction,
|
||||
HttpOnly: true,
|
||||
Path: "/",
|
||||
})
|
||||
@@ -176,7 +173,7 @@ func (h *Handler) Register(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (h *Handler) Logout(w http.ResponseWriter, _ *http.Request) {
|
||||
removeJwtCookie(w)
|
||||
h.removeJwtCookie(w)
|
||||
message, _ := json.Marshal(map[string]any{"message": "User logged out! HTTPonly jwt cookie deleted"})
|
||||
_, _ = w.Write(message)
|
||||
}
|
||||
Reference in new issue
Block a user