forked from mtusi2026/mtusi2026lab2
fixed a bug with NUL in strings
This commit is contained in:
@@ -24,7 +24,7 @@ func (u *Users) CreateUser(ctx context.Context, username, displayName, passwordH
|
||||
INSERT INTO users (username, display_name, password_hash)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING id::text, created_at`,
|
||||
username, displayName, passwordHash,
|
||||
username, encodeText(displayName), passwordHash,
|
||||
).Scan(&user.ID, &user.CreatedAt)
|
||||
if err != nil {
|
||||
return domain.User{}, mapErr(err)
|
||||
@@ -51,6 +51,7 @@ func (u *Users) UserByID(ctx context.Context, id string) (domain.User, error) {
|
||||
}
|
||||
return domain.User{}, err
|
||||
}
|
||||
user.DisplayName = decodeText(user.DisplayName)
|
||||
user.CreatedAt = user.CreatedAt.UTC()
|
||||
return user, nil
|
||||
}
|
||||
@@ -65,11 +66,12 @@ func (u *Users) CredentialsByUsername(ctx context.Context, username string) (dom
|
||||
SELECT id::text, username, display_name, created_at, password_hash
|
||||
FROM users
|
||||
WHERE username = $1`,
|
||||
username,
|
||||
encodeText(username),
|
||||
).Scan(&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt, &hash)
|
||||
if err != nil {
|
||||
return domain.User{}, "", mapErr(err)
|
||||
}
|
||||
user.DisplayName = decodeText(user.DisplayName)
|
||||
user.CreatedAt = user.CreatedAt.UTC()
|
||||
return user, hash, nil
|
||||
}
|
||||
@@ -81,7 +83,7 @@ func (u *Users) UsernameExists(ctx context.Context, username string) bool {
|
||||
var exists bool
|
||||
err := u.pool.QueryRow(ctx,
|
||||
`SELECT EXISTS (SELECT 1 FROM users WHERE username = $1)`,
|
||||
username,
|
||||
encodeText(username),
|
||||
).Scan(&exists)
|
||||
return err == nil && exists
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user