Add saving user, add nonpersistant register login handling
This commit is contained in:
+23
-2
@@ -3,9 +3,8 @@ package main
|
||||
import (
|
||||
"context"
|
||||
|
||||
//"golang.org/x/crypto/bcrypt"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
var dbConn *pgx.Conn
|
||||
@@ -21,6 +20,7 @@ func InitDatabase(ctx context.Context) {
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(20) UNIQUE NOT NULL,
|
||||
pass_hash VARCHAR(60) NOT NULL,
|
||||
pronouns VARCHAR(15) DEFAULT NULL,
|
||||
color VARCHAR(3) DEFAULT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS chat_groups (
|
||||
@@ -45,3 +45,24 @@ func InitDatabase(ctx context.Context) {
|
||||
|
||||
dbConn = conn
|
||||
}
|
||||
|
||||
func SaveClientWithoutGroups(ctx context.Context, client *Client) error {
|
||||
var id uint64
|
||||
var err error
|
||||
|
||||
var hashed []byte
|
||||
hashed, err = bcrypt.GenerateFromPassword([]byte(client.PasswordHash), bcrypt.DefaultCost)
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user