added fallback middleware
All checks were successful
Release / publish (push) Successful in 4m7s

This commit is contained in:
u80864958
2025-05-12 09:30:38 +02:00
parent ca99d8fb87
commit 73ff28347a
6 changed files with 161 additions and 8 deletions

View File

@ -50,6 +50,45 @@ func (s *Service) Signup(w http.ResponseWriter, r *http.Request) {
}
}
// Signup handles user signup by decoding request body, hashing the password, and saving user data to the database.
func (s *Service) ChangePassword(w http.ResponseWriter, r *http.Request) {
var err error
var login Login
var password []byte
claims, ok := ExtractClaims(r.Context())
if !ok {
log.Println("Error while extracting claims")
w.WriteHeader(http.StatusInternalServerError)
return
}
if err = json.NewDecoder(r.Body).Decode(&login); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
if len([]byte(login.Password)) > 72 {
fmt.Fprint(w, "Password to long, max 72 bytes")
w.WriteHeader(http.StatusBadRequest)
return
}
if password, err = bcrypt.GenerateFromPassword([]byte(login.Password), 6); err != nil {
log.Println("Error: ", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
err = s.db.Model(&model.User{}).Where("id = ?", claims.UserID).Update("password", password).Error
if err != nil {
log.Printf("Error: %v", err)
w.WriteHeader(http.StatusInternalServerError)
}
w.WriteHeader(http.StatusOK)
}
// Login handles user login by decoding request body, verifying credentials, and returning a JWT token.
func (s *Service) Login(w http.ResponseWriter, r *http.Request) {
var login Login

View File

@ -19,9 +19,7 @@ func CreateMux(cfg *config.Config) (r *mux.Router) {
r.Use(cors.HandlerForOrigin("*"))
app(r.PathPrefix("/api").Subrouter(), cfg)
frontend := web.Frontend
r.PathPrefix("/").Handler(middlewares.AddPrefix("/dist/frontend/browser", http.FileServerFS(frontend)))
frontend(r.PathPrefix("/"))
r.Methods("OPTIONS").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// The CORS middleware should set up the headers for you
@ -31,15 +29,28 @@ func CreateMux(cfg *config.Config) (r *mux.Router) {
return
}
func frontend(r *mux.Route) {
frontend := web.Frontend
r.Handler(
middlewares.AddPrefix("/dist/frontend/browser",
middlewares.FallbackFile(
frontend,
"/dist/frontend/browser/index.html",
http.FileServerFS(frontend),
),
))
}
func app(r *mux.Router, cfg *config.Config) {
db := model.Init()
blg := posts.New(db)
auth := auth.New(&cfg.Auth, db)
// auth
r.HandleFunc("/login", auth.Login).Methods("POST")
r.HandleFunc("/signup", auth.Signup).Methods("POST")
r.Handle("/logout", auth.Authenticated(auth.Logout)).Methods("DELETE")
r.HandleFunc("/auth/login", auth.Login).Methods("POST")
r.HandleFunc("/auth/signup", auth.Signup).Methods("POST")
r.Handle("/auth/password", auth.Authenticated(auth.ChangePassword)).Methods("PUT")
r.Handle("/auth/logout", auth.Authenticated(auth.Logout)).Methods("DELETE")
// Posts
r.Handle("/posts", auth.Authenticated(blg.SavePost, model.RoleUser, model.RoleAdmin)).Methods("POST")