internal/auth is pure logic (bcrypt hashing, session token generation) with no DB access — persistence for account/app_user/session lives in internal/store like everything else, via migration 0003. account is the tenant (Mandant); app_user is a login inside one account; session is a real server-side row (not a signed stateless token) so logout can actually end a session rather than the client just forgetting a JWT. submission.account_id is NOT NULL — added directly rather than the nullable-then-backfill dance, since no submission rows exist anywhere yet (verified empty on the test server before writing the migration). Added as migration 0003 (new file), not folded into an earlier one, since 0001/0002 are already applied on the test server. store.ErrNotFound lets callers distinguish "wrong email" / "unknown session" from a genuine DB error — matters for login, where those two cases should both fail closed but for different reasons. Not yet wired into internal/web — that's the next commit. All of this is tested against real Postgres (14 store tests green) but isn't reachable from any HTTP handler yet. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
41 lines
1.0 KiB
Go
41 lines
1.0 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
// Account ist ein Mandant (Creator, Agentur, Marke oder Kanzlei als
|
|
// eigene Organisation). Jeder Beitrag gehört genau einem Account.
|
|
type Account struct {
|
|
ID string
|
|
Name string
|
|
CreatedAt time.Time
|
|
}
|
|
|
|
// CreateAccount legt einen neuen Mandanten an.
|
|
func (s *Store) CreateAccount(ctx context.Context, name string) (Account, error) {
|
|
var a Account
|
|
err := s.Pool.QueryRow(ctx, `
|
|
INSERT INTO account (name) VALUES ($1)
|
|
RETURNING id, name, created_at
|
|
`, name).Scan(&a.ID, &a.Name, &a.CreatedAt)
|
|
if err != nil {
|
|
return Account{}, fmt.Errorf("store: create account: %w", err)
|
|
}
|
|
return a, nil
|
|
}
|
|
|
|
// GetAccount liest einen Mandanten anhand seiner ID.
|
|
func (s *Store) GetAccount(ctx context.Context, id string) (Account, error) {
|
|
var a Account
|
|
err := s.Pool.QueryRow(ctx, `
|
|
SELECT id, name, created_at FROM account WHERE id = $1
|
|
`, id).Scan(&a.ID, &a.Name, &a.CreatedAt)
|
|
if err != nil {
|
|
return Account{}, fmt.Errorf("store: get account: %w", err)
|
|
}
|
|
return a, nil
|
|
}
|