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 {
|
func CreateClient(ctx context.Context, client *Client) error {
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
defer mu.Unlock()
|
defer mu.Unlock()
|
||||||
err := SaveClientWithoutGroups(ctx, client)
|
clients[client.Id] = client
|
||||||
|
|
||||||
|
hashed, err := bcrypt.GenerateFromPassword([]byte(client.PasswordHash), bcrypt.DefaultCost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
client.PasswordHash = string(hashed)
|
||||||
|
|
||||||
|
//err := DbSaveClientWithoutGroups(ctx, client)
|
||||||
|
//if err != nil {
|
||||||
|
// return err
|
||||||
|
//}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+14
-14
@@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5"
|
"github.com/jackc/pgx/v5"
|
||||||
"golang.org/x/crypto/bcrypt"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var dbConn *pgx.Conn
|
var dbConn *pgx.Conn
|
||||||
@@ -46,23 +45,24 @@ func InitDatabase(ctx context.Context) {
|
|||||||
dbConn = conn
|
dbConn = conn
|
||||||
}
|
}
|
||||||
|
|
||||||
func SaveClientWithoutGroups(ctx context.Context, client *Client) error {
|
func DbSaveClientWithoutGroups(ctx context.Context, client *Client) error {
|
||||||
var id uint64
|
var id uint64
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
var hashed []byte
|
err = dbConn.QueryRow(ctx, `
|
||||||
hashed, err = bcrypt.GenerateFromPassword([]byte(client.PasswordHash), bcrypt.DefaultCost)
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ func RegisterHandler(response http.ResponseWriter, request *http.Request) {
|
|||||||
newClient := Client{
|
newClient := Client{
|
||||||
Name: username,
|
Name: username,
|
||||||
PasswordHash: password,
|
PasswordHash: password,
|
||||||
|
Color: [3]uint8{120, 120, 120}, //"xxx"
|
||||||
}
|
}
|
||||||
|
|
||||||
err := CreateClient(ctx, &newClient)
|
err := CreateClient(ctx, &newClient)
|
||||||
@@ -61,19 +62,24 @@ func LoginHandler(response http.ResponseWriter, request *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var client Client
|
||||||
|
|
||||||
id, err := GetIdFromClientName(ctx, username)
|
id, err := GetIdFromClientName(ctx, username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(response, "bad login", http.StatusBadRequest)
|
http.Error(response, "bad login", http.StatusBadRequest)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = CheckPassword(ctx, id, password)
|
err = DbGetClient(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(response, "bad login", http.StatusBadRequest)
|
http.Error(response, "bad login", http.StatusBadRequest)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
token, err := GetToken(id)
|
token, err := GetToken(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(response, "Internal error", http.StatusInternalServerError)
|
http.Error(response, "Internal error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
response.Write([]byte(token))
|
response.Write([]byte(token))
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ func main() {
|
|||||||
InitDatabase(ctx)
|
InitDatabase(ctx)
|
||||||
|
|
||||||
http.HandleFunc("/ws", ServeWsConnection)
|
http.HandleFunc("/ws", ServeWsConnection)
|
||||||
http.HandleFunc("/register", RegisterHandler)
|
http.HandleFunc("/new/client", RegisterHandler)
|
||||||
http.HandleFunc("/login", LoginHandler)
|
http.HandleFunc("/new/token", LoginHandler)
|
||||||
|
|
||||||
log.Println("listening on :8080")
|
log.Println("listening on :8080")
|
||||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||||
|
|||||||
+3
-1
@@ -12,7 +12,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func ServeWsConnection(responseWriter http.ResponseWriter, request *http.Request) {
|
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 {
|
if err != nil {
|
||||||
log.Printf("websocket accept error: %v", err)
|
log.Printf("websocket accept error: %v", err)
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user