sonarqube-badges/router/api/login.go
2025-03-15 01:49:52 +01:00

35 lines
780 B
Go

package api
import (
"github.com/gorilla/mux"
"net/http"
"sonarqube-badge/router/utils"
)
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")
ctx := req.Context()
if utils.UserExists(ctx, email, password) {
user := utils.GetUser(ctx, email)
cookie, err := utils.CreateJWTCookie(user, req)
if err != nil {
res.WriteHeader(http.StatusInternalServerError)
return
}
http.SetCookie(res, cookie)
} else {
res.WriteHeader(http.StatusUnauthorized)
res.Write([]byte("Wrong email or password"))
}
}
func LoginRouter(r *mux.Router) {
r.HandleFunc("", postLogin).Methods("POST")
}