First pass of authentication

This commit is contained in:
Leon Mika 2026-02-25 22:04:47 +11:00
parent c943864edc
commit 01c6e9de87
15 changed files with 311 additions and 42 deletions

View file

@ -25,8 +25,19 @@ func (q *Queries) InsertUser(ctx context.Context, arg InsertUserParams) (int64,
return id, err
}
const selectUserByID = `-- name: SelectUserByID :one
SELECT id, username, password FROM users WHERE id = ? LIMIT 1
`
func (q *Queries) SelectUserByID(ctx context.Context, id int64) (User, error) {
row := q.db.QueryRowContext(ctx, selectUserByID, id)
var i User
err := row.Scan(&i.ID, &i.Username, &i.Password)
return i, err
}
const selectUserByUsername = `-- name: SelectUserByUsername :one
SELECT id, username, password FROM users WHERE username = ?
SELECT id, username, password FROM users WHERE username = ? LIMIT 1
`
func (q *Queries) SelectUserByUsername(ctx context.Context, username string) (User, error) {

View file

@ -14,16 +14,16 @@ func (db *Provider) SelectUserByUsername(ctx context.Context, username string) (
return models.User{}, err
}
pwdBytes, err := base64.StdEncoding.DecodeString(res.Password)
return dbUserToUser(res)
}
func (db *Provider) SelectUserByID(ctx context.Context, userID int64) (models.User, error) {
res, err := db.queries.SelectUserByID(ctx, userID)
if err != nil {
return models.User{}, err
}
return models.User{
ID: res.ID,
Username: res.Username,
PasswordHashed: pwdBytes,
}, nil
return dbUserToUser(res)
}
func (db *Provider) SaveUser(ctx context.Context, user *models.User) error {
@ -47,3 +47,16 @@ func (db *Provider) SaveUser(ctx context.Context, user *models.User) error {
Password: hashedPassword,
})
}
func dbUserToUser(res sqlgen.User) (models.User, error) {
pwdBytes, err := base64.StdEncoding.DecodeString(res.Password)
if err != nil {
return models.User{}, err
}
return models.User{
ID: res.ID,
Username: res.Username,
PasswordHashed: pwdBytes,
}, nil
}