39 lines
865 B
Go
39 lines
865 B
Go
package api
|
|
|
|
import (
|
|
"github.com/gorilla/mux"
|
|
"net/http"
|
|
"sonarqube-badge/security"
|
|
)
|
|
|
|
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")
|
|
|
|
if email == "1@example.com" && password == "hello" {
|
|
token, err := security.CreateToken(email)
|
|
if err != nil {
|
|
http.Error(res, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
cookie := http.Cookie{
|
|
Name: "jwt-token",
|
|
Value: token,
|
|
Path: "/",
|
|
Secure: true,
|
|
HttpOnly: true,
|
|
}
|
|
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")
|
|
}
|