mid chat group ccreation implementation

This commit is contained in:
2026-03-12 22:54:31 +01:00
parent b7b5788f98
commit eeea54492b
3 changed files with 131 additions and 17 deletions
+71 -11
View File
@@ -7,9 +7,16 @@ import (
"golang.org/x/crypto/bcrypt"
)
func RegisterHandler(response http.ResponseWriter, request *http.Request) {
func isMethodAllowed(response *http.ResponseWriter, request *http.Request) bool {
if request.Method != http.MethodPost {
http.Error(response, "POST only", http.StatusMethodNotAllowed)
http.Error(*response, "POST only", http.StatusMethodNotAllowed)
return false
}
return true
}
func RegisterHandler(response http.ResponseWriter, request *http.Request) {
if !isMethodAllowed(&response, request) {
return
}
ctx := request.Context()
@@ -20,18 +27,21 @@ func RegisterHandler(response http.ResponseWriter, request *http.Request) {
http.Error(response, "short username", http.StatusBadRequest)
return
}
if username == "server" {
http.Error(response, "only server can use such name", http.StatusBadRequest)
}
if len(password) < 8 {
http.Error(response, "short password", http.StatusBadRequest)
return
}
if _, err := GetUserDataByName(ctx, username); err == nil {
http.Error(response, "User already exists", http.StatusBadRequest)
http.Error(response, "user already exists", http.StatusBadRequest)
return
}
if _, err := AddNewUser(ctx, User{0, username, password, "xxx", false}); err != nil {
http.Error(response, "Internal server error", http.StatusInternalServerError)
if _, err := AddNewUser(ctx, &User{username, password, "xxx", 0, false}); err != nil {
http.Error(response, "internal server error", http.StatusInternalServerError)
log.Fatal(err)
return
}
@@ -39,39 +49,89 @@ func RegisterHandler(response http.ResponseWriter, request *http.Request) {
response.WriteHeader(http.StatusCreated)
_, err := response.Write([]byte("registered"))
if err != nil {
http.Error(response, "Internal server error", http.StatusInternalServerError)
http.Error(response, "internal server error", http.StatusInternalServerError)
return
}
}
func LoginHandler(response http.ResponseWriter, request *http.Request) {
if request.Method != http.MethodPost {
http.Error(response, "POST only", http.StatusMethodNotAllowed)
if !isMethodAllowed(&response, request) {
return
}
ctx := request.Context()
username := request.FormValue("username")
password := request.FormValue("password")
respondBadLogin := func() {
http.Error(response, "bad login", http.StatusConflict)
}
if len(username) < 2 {
http.Error(response, "short username", http.StatusBadRequest)
respondBadLogin()
return
}
user, err := GetUserDataByName(ctx, username)
if err != nil {
http.Error(response, "Bad login", http.StatusBadRequest)
respondBadLogin()
return
}
if bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)) == nil {
token, err := GetToken(user)
if err != nil {
http.Error(response, "Internal server error", http.StatusInternalServerError)
respondBadLogin()
return
}
if _, err = response.Write([]byte(token)); err != nil {
return
}
return
}
respondBadLogin()
}
func CreateGroupHandler(response http.ResponseWriter, request *http.Request) {
if !isMethodAllowed(&response, request) {
return
}
var anyAuthDone bool = false
var user User
ctx := request.Context()
username := request.FormValue("username")
password := request.FormValue("password")
token := request.FormValue("token")
respondBadLogin := func() {
http.Error(response, "bad login", http.StatusConflict)
}
if len(username) < 2 {
respondBadLogin()
}
user, err := GetUserDataByName(ctx, username)
if err != nil {
respondBadLogin()
return
}
if len(password) > 0 {
if bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)) != nil {
respondBadLogin()
return
}
anyAuthDone = true
} else if (len(token) > 0) {
user, err := GetUserFromToken(token)
if err != nil {
respondBadLogin()
}
}
CreateChatGroupWithoutMembers(ctx, &ChatGroup{
Name:
})
}