tsk-19: Service login (#23)

Closes #19

Reviewed-on: phoenix/textsender-auth#23
Co-authored-by: phoenix <kundeng00@pm.me>
Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
phoenix
2025-11-21 18:42:58 +00:00
committed by phoenix
parent c340693b24
commit 0a00282ba7
9 changed files with 205 additions and 29 deletions
+18 -6
View File
@@ -3,11 +3,11 @@ package mock
import (
"context"
"errors"
"fmt"
"sync"
"github.com/google/uuid"
"git.kundeng.us/phoenix/textsender-models/pkg/user"
"github.com/google/uuid"
)
type MockServiceUserStore struct {
@@ -53,10 +53,6 @@ func (m *MockServiceUserStore) CheckWithUsername(ctx context.Context, username s
return false, m.Error
}
if m.Error != nil {
return false, m.Error
}
var exists bool
for _, serviceUser := range m.ServiceUsers {
@@ -72,3 +68,19 @@ func (m *MockServiceUserStore) CheckWithUsername(ctx context.Context, username s
return exists, nil
}
}
func (m *MockServiceUserStore) GetWithUsername(ctx context.Context, username string) (*user.ServiceUser, error) {
m.mu.Lock()
defer m.mu.Unlock()
if m.Error != nil {
return nil, m.Error
}
serviceUser := m.ServiceUsersByUsername[username]
if serviceUser != nil {
return serviceUser, nil
} else {
return nil, fmt.Errorf("User not found")
}
}
+16
View File
@@ -11,6 +11,7 @@ import (
type ServiceStore interface {
CheckWithUsername(ctx context.Context, username string) (bool, error)
GetWithUsername(ctx context.Context, username string) (*user.ServiceUser, error)
Create(ctx context.Context, serviceUser *user.ServiceUser) error
}
@@ -37,6 +38,21 @@ func (s *PGServiceStore) CheckWithUsername(ctx context.Context, username string)
}
}
func (s *PGServiceStore) GetWithUsername(ctx context.Context, username string) (*user.ServiceUser, error) {
var serviceUser user.ServiceUser
query := `SELECT id, username, passphrase, created FROM service_users WHERE username = $1`
if err := s.db.QueryRow(ctx, query, username).Scan(&serviceUser.Id, &serviceUser.Username, &serviceUser.Passphrase, &serviceUser.Created); err != nil {
if err == pgx.ErrNoRows {
return nil, nil
} else {
return nil, fmt.Errorf("Error querying row: %v", err)
}
} else {
return &serviceUser, nil
}
}
func (s *PGServiceStore) Create(ctx context.Context, serviceUser *user.ServiceUser) error {
query := `
INSERT INTO service_users (username, passphrase)