nie wiedzieju
This commit is contained in:
@@ -29,10 +29,19 @@ func DeleteGroup(ctx context.Context, chatGroup *ChatGroup) {
|
||||
func CreateClient(ctx context.Context, client *Client) error {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
err := SaveClientWithoutGroups(ctx, client)
|
||||
clients[client.Id] = client
|
||||
|
||||
hashed, err := bcrypt.GenerateFromPassword([]byte(client.PasswordHash), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
client.PasswordHash = string(hashed)
|
||||
|
||||
//err := DbSaveClientWithoutGroups(ctx, client)
|
||||
//if err != nil {
|
||||
// return err
|
||||
//}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
+14
-14
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
var dbConn *pgx.Conn
|
||||
@@ -46,23 +45,24 @@ func InitDatabase(ctx context.Context) {
|
||||
dbConn = conn
|
||||
}
|
||||
|
||||
func SaveClientWithoutGroups(ctx context.Context, client *Client) error {
|
||||
func DbSaveClientWithoutGroups(ctx context.Context, client *Client) error {
|
||||
var id uint64
|
||||
var err error
|
||||
|
||||
var hashed []byte
|
||||
hashed, err = bcrypt.GenerateFromPassword([]byte(client.PasswordHash), bcrypt.DefaultCost)
|
||||
err = dbConn.QueryRow(ctx, `
|
||||
INSERT INTO users (name, pass_hash, pronouns)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING id
|
||||
`, client.Name, client.PasswordHash, client.Pronouns).Scan(&id)
|
||||
return err
|
||||
}
|
||||
|
||||
func DbGetClient(ctx context.Context, id uint32, client *Client) error {
|
||||
err := dbConn.QueryRow(ctx, `
|
||||
SELECT name, pass_hash, pronouns, color WHERE id = $1
|
||||
`, id).Scan(client.Name, client.PasswordHash, client.Pronouns, client.Color)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
password := string(hashed)
|
||||
|
||||
c := string(client.Color[:])
|
||||
err = dbConn.QueryRow(ctx, `
|
||||
INSERT INTO users (name, pass_hash, pronouns, color)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING id
|
||||
`, client.Name, password, client.Pronouns, c).Scan(&id)
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ func RegisterHandler(response http.ResponseWriter, request *http.Request) {
|
||||
newClient := Client{
|
||||
Name: username,
|
||||
PasswordHash: password,
|
||||
Color: [3]uint8{120, 120, 120}, //"xxx"
|
||||
}
|
||||
|
||||
err := CreateClient(ctx, &newClient)
|
||||
@@ -61,19 +62,24 @@ func LoginHandler(response http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
var client Client
|
||||
|
||||
id, err := GetIdFromClientName(ctx, username)
|
||||
if err != nil {
|
||||
http.Error(response, "bad login", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = CheckPassword(ctx, id, password)
|
||||
err = DbGetClient(ctx)
|
||||
if err != nil {
|
||||
http.Error(response, "bad login", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
token, err := GetToken(id)
|
||||
if err != nil {
|
||||
http.Error(response, "Internal error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
response.Write([]byte(token))
|
||||
|
||||
@@ -11,8 +11,8 @@ func main() {
|
||||
InitDatabase(ctx)
|
||||
|
||||
http.HandleFunc("/ws", ServeWsConnection)
|
||||
http.HandleFunc("/register", RegisterHandler)
|
||||
http.HandleFunc("/login", LoginHandler)
|
||||
http.HandleFunc("/new/client", RegisterHandler)
|
||||
http.HandleFunc("/new/token", LoginHandler)
|
||||
|
||||
log.Println("listening on :8080")
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
|
||||
+3
-1
@@ -12,7 +12,9 @@ import (
|
||||
)
|
||||
|
||||
func ServeWsConnection(responseWriter http.ResponseWriter, request *http.Request) {
|
||||
connection, err := websocket.Accept(responseWriter, request, nil)
|
||||
connection, err := websocket.Accept(responseWriter, request, &websocket.AcceptOptions{
|
||||
InsecureSkipVerify: true,
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("websocket accept error: %v", err)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user