sonarqube-badges/router/api/login.go

36 lines
780 B
Go
Raw Normal View History

2025-03-13 08:25:39 +01:00
package api
import (
"github.com/gorilla/mux"
"net/http"
2025-03-15 01:49:52 +01:00
"sonarqube-badge/router/utils"
2025-03-13 08:25:39 +01:00
)
func postLogin(res http.ResponseWriter, req *http.Request) {
if err := req.ParseForm(); err != nil {
http.Error(res, err.Error(), http.StatusBadRequest)
}
email := req.Form.Get("email")
password := req.Form.Get("password")
2025-03-15 01:49:52 +01:00
ctx := req.Context()
if utils.UserExists(ctx, email, password) {
user := utils.GetUser(ctx, email)
cookie, err := utils.CreateJWTCookie(user, req)
2025-03-13 08:25:39 +01:00
if err != nil {
2025-03-15 01:49:52 +01:00
res.WriteHeader(http.StatusInternalServerError)
2025-03-13 08:25:39 +01:00
return
}
2025-03-15 01:49:52 +01:00
http.SetCookie(res, cookie)
2025-03-13 08:25:39 +01:00
} else {
res.WriteHeader(http.StatusUnauthorized)
res.Write([]byte("Wrong email or password"))
}
}
func LoginRouter(r *mux.Router) {
r.HandleFunc("", postLogin).Methods("POST")
}