diff options
author | Julien Dessaux | 2025-01-06 00:41:32 +0100 |
---|---|---|
committer | Julien Dessaux | 2025-01-06 00:41:32 +0100 |
commit | 6e069484cb0a911ba541e07bf04331fadbb76612 (patch) | |
tree | 3457c759d54ae91e5ac1e64fe0bbf9c4b8ac18f0 /pkg/database/accounts.go | |
parent | feat(tfstated): add syscall.SIGTERM handling (diff) | |
download | tfstated-6e069484cb0a911ba541e07bf04331fadbb76612.tar.gz tfstated-6e069484cb0a911ba541e07bf04331fadbb76612.tar.bz2 tfstated-6e069484cb0a911ba541e07bf04331fadbb76612.zip |
feat(webui): bootstrap session handling and login process
Diffstat (limited to '')
-rw-r--r-- | pkg/database/accounts.go | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/pkg/database/accounts.go b/pkg/database/accounts.go index d73fe27..377ca80 100644 --- a/pkg/database/accounts.go +++ b/pkg/database/accounts.go @@ -47,6 +47,38 @@ func (db *DB) InitAdminAccount() error { }) } +func (db *DB) LoadAccountById(id int) (*model.Account, error) { + account := model.Account{ + Id: id, + } + var ( + created int64 + lastLogin int64 + ) + err := db.QueryRow( + `SELECT username, salt, password_hash, is_admin, created, last_login, settings + FROM accounts + WHERE id = ?;`, + id, + ).Scan(&account.Username, + &account.Salt, + &account.PasswordHash, + &account.IsAdmin, + &created, + &lastLogin, + &account.Settings, + ) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return nil, nil + } + return nil, fmt.Errorf("failed to load account by id %d: %w", id, err) + } + account.Created = time.Unix(created, 0) + account.LastLogin = time.Unix(lastLogin, 0) + return &account, nil +} + func (db *DB) LoadAccountByUsername(username string) (*model.Account, error) { account := model.Account{ Username: username, @@ -72,7 +104,7 @@ func (db *DB) LoadAccountByUsername(username string) (*model.Account, error) { if errors.Is(err, sql.ErrNoRows) { return nil, nil } - return nil, err + return nil, fmt.Errorf("failed to load account by username %s: %w", username, err) } account.Created = time.Unix(created, 0) account.LastLogin = time.Unix(lastLogin, 0) |