Files
2025-04-10 09:16:37 +02:00

33 lines
843 B
Go

package helper
import (
"context"
"net/http"
"os"
"github.com/alexedwards/scs/v2"
)
// setting cors, important for later frontend use
//
// in DEBUG == "true" everything is set to "*" so that no cors errors will be happen
func SetCors(w http.ResponseWriter) {
if os.Getenv("NO_CORS") == "true" {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
// log.Println("Setting cors to *")
}
}
func RequiresLogin(session *scs.SessionManager, w http.ResponseWriter, r *http.Request) {
r = r.WithContext(context.WithValue(r.Context(), "session", session))
if GetEnv("GO_ENV", "production") == "debug" {
return
}
if session.Exists(r.Context(), "user") {
return
}
http.Redirect(w, r, "/user/login", http.StatusSeeOther)
}