Compare commits
2 Commits
fbacfdb0d9
...
13fdc84801
| Author | SHA1 | Date | |
|---|---|---|---|
| 13fdc84801 | |||
|
|
45ccb19245 |
@ -14,18 +14,14 @@ import (
|
||||
func Login(w http.ResponseWriter, r *http.Request) {
|
||||
var u *types.LoginUser
|
||||
|
||||
isHtmx := r.Header.Get("HX-Request")
|
||||
|
||||
if isHtmx == "true" {
|
||||
u = &types.LoginUser{
|
||||
Email: r.PostFormValue("email"),
|
||||
Password: r.PostFormValue("password"),
|
||||
}
|
||||
if r.Header.Get("HX-Request") == "true" {
|
||||
u = handleHtmxLogin(r)
|
||||
} else {
|
||||
if err := json.NewDecoder(r.Body).Decode(&u); err != nil {
|
||||
InternalServerErrorHandler(w, err)
|
||||
return
|
||||
u = handleHttpLogin(w, r, u)
|
||||
}
|
||||
|
||||
if u == nil {
|
||||
return
|
||||
}
|
||||
|
||||
User, err := query.GetLogin(context.Background(), u)
|
||||
@ -34,16 +30,18 @@ func Login(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if bcrypt.CheckPasswordHash(u.Password, User.Password) {
|
||||
if !bcrypt.CheckPasswordHash(u.Password, User.Password) {
|
||||
UnauthorizedHandler(w)
|
||||
return
|
||||
}
|
||||
|
||||
jwtToken := jwt.CreateUserJWT(User.Name, User.ID, string(User.Role))
|
||||
|
||||
if jwtToken != "" {
|
||||
|
||||
cookie := &http.Cookie{Name: "jwt",
|
||||
cookie := &http.Cookie{
|
||||
Name: "jwt",
|
||||
Value: jwtToken,
|
||||
//HttpOnly: true,
|
||||
//Secure: true,
|
||||
HttpOnly: true,
|
||||
Secure: true,
|
||||
SameSite: http.SameSiteLaxMode,
|
||||
Expires: time.Now().Add(24 * time.Hour),
|
||||
}
|
||||
@ -52,17 +50,21 @@ func Login(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, err = w.Write([]byte("login success"))
|
||||
return
|
||||
} else {
|
||||
}
|
||||
|
||||
func handleHtmxLogin(r *http.Request) *types.LoginUser {
|
||||
return &types.LoginUser{
|
||||
Email: r.PostFormValue("email"),
|
||||
Password: r.PostFormValue("password"),
|
||||
}
|
||||
}
|
||||
|
||||
func handleHttpLogin(w http.ResponseWriter, r *http.Request, u *types.LoginUser) *types.LoginUser {
|
||||
if err := json.NewDecoder(r.Body).Decode(&u); err != nil {
|
||||
InternalServerErrorHandler(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
} else {
|
||||
UnauthorizedHandler(w)
|
||||
|
||||
println("unauthorized")
|
||||
return nil
|
||||
}
|
||||
return u
|
||||
}
|
||||
|
||||
func CanEdit(w http.ResponseWriter, r *http.Request) {
|
||||
@ -71,14 +73,12 @@ func CanEdit(w http.ResponseWriter, r *http.Request) {
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(""))
|
||||
return
|
||||
}
|
||||
if audience == "owner" || audience == "visitor" {
|
||||
if audience == "owner" || audience == "admin" {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("<button class=\"button is-link\">Edit</button>"))
|
||||
} else {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(""))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@ -22,6 +22,7 @@ func UnprocessableEntityHandler(w http.ResponseWriter, err error) {
|
||||
}
|
||||
|
||||
func UnauthorizedHandler(w http.ResponseWriter) {
|
||||
log.Println("unauthorized")
|
||||
setError(w, http.StatusUnauthorized, "Unauthorized")
|
||||
}
|
||||
|
||||
|
||||
@ -15,11 +15,11 @@ func CreateUserHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var u *types.RegisterUser
|
||||
|
||||
isHtmx := r.Header.Get("HX-Request")
|
||||
|
||||
if isHtmx == "true" {
|
||||
if r.Header.Get("HX-Request") == "true" {
|
||||
u = &types.RegisterUser{
|
||||
Name: r.PostFormValue("name"),
|
||||
Password: r.PostFormValue("password"),
|
||||
Email: r.PostFormValue("email"),
|
||||
//Role: user.Role(r.PostFormValue("role")),
|
||||
}
|
||||
} else {
|
||||
@ -28,7 +28,6 @@ func CreateUserHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
}
|
||||
u.Password = "123"
|
||||
if !validate.UserIsValid(u) {
|
||||
BadRequestHandler(w)
|
||||
return
|
||||
|
||||
@ -69,9 +69,5 @@ func EditProject(project *ent.Project) g.Node {
|
||||
b.Content(
|
||||
b.Textarea(project.Description, e.Name("project_description")),
|
||||
),
|
||||
|
||||
//b.CardFooter(
|
||||
//Save(),
|
||||
//),
|
||||
)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user