simplify ID handling

This commit is contained in:
Up committed 2024-05-14 14:30:04 +02:00
1 parent 983e17c894
commit c0aade2e65
3 files changed
+29 -31

No files matched your search

+10 -5
View File
@@ -18,6 +18,8 @@
package db
import (
"database/sql"
"errors"
"fmt"
"slices"
@@ -208,14 +210,17 @@ func UpdateTrainerIds(trainerId, secretId int, uuid []byte) error {
return nil
}
func IsActiveSession(token []byte, clientSessionId string) (bool, error) {
func IsActiveSession(uuid []byte, clientSessionId string) (bool, error) {
var storedId string
err := handle.QueryRow("SELECT `clientSessionId` FROM sessions WHERE token = ?", token).Scan(&storedId)
err := handle.QueryRow("SELECT clientSessionId FROM activeClientSessions WHERE sessions.uuid = ?", uuid).Scan(&storedId)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return false, nil
}
return false, err
}
if storedId == "" {
err = UpdateActiveSession(token, clientSessionId)
err = UpdateActiveSession(uuid, clientSessionId)
if err != nil {
return false, err
}
@@ -225,8 +230,8 @@ func IsActiveSession(token []byte, clientSessionId string) (bool, error) {
return storedId == clientSessionId, nil
}
func UpdateActiveSession(token []byte, clientSessionId string) error {
_, err := handle.Exec("UPDATE sessions SET clientSessionId = ? WHERE token = ?", clientSessionId, token)
func UpdateActiveSession(uuid []byte, clientSessionId string) error {
_, err := handle.Exec("REPLACE INTO activeClientSessions VALUES (?, ?)", uuid, clientSessionId)
if err != nil {
return err
}
+1 -1
View File
@@ -175,7 +175,7 @@ func setupDb(tx *sql.Tx) error {
// MIGRATION 001
`ALTER TABLE sessions DROP COLUMN IF EXISTS active`,
`ALTER TABLE sessions ADD COLUMN IF NOT EXISTS clientSessionId VARCHAR(32)`,
`CREATE TABLE IF NOT EXISTS activeClientSessions (uuid BINARY(16) NOT NULL PRIMARY KEY, clientSessionId VARCHAR(32) NOT NULL)`,
}
for _, q := range queries {