Phase 10, part 2: the first owner, from a one-time setup link

A new server prints a link to create its owner (supermarket setup-link
makes another); until it's used, every page says the setup isn't
finished. One-time links (schema 33) are kept as hashes and used up in
one statement; resets and invitations will use them too. The server
edition opens Postgres.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 08:53:14 -07:00
co-authored by Claude Opus 5.5
parent 96404a3580
commit 72455438c9
16 changed files with 553 additions and 10 deletions
+64 -1
View File
@@ -49,6 +49,7 @@ import (
"supermarket/internal/secrets"
"supermarket/internal/storage"
"supermarket/internal/transfer"
"supermarket/internal/users"
"supermarket/internal/web"
)
@@ -72,6 +73,13 @@ func main() {
if len(os.Args) > 1 && os.Args[1] == "config" {
os.Exit(configCommand())
}
if len(os.Args) > 1 && os.Args[1] == "setup-link" {
if err := setupLinkCommand(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
return
}
if len(os.Args) > 1 && (os.Args[1] == "export" || os.Args[1] == "import") {
if err := transferCommand(os.Args[1], os.Args[2:]); err != nil {
fmt.Fprintln(os.Stderr, err)
@@ -208,7 +216,12 @@ func run(log *slog.Logger) error {
return err
}
db.AppVersion = version
database, err := db.Open(filepath.Join(dataDir, "app.db"))
var database *db.DB
if cfg.DatabaseURL != "" {
database, err = db.OpenPostgres(cfg.DatabaseURL)
} else {
database, err = db.Open(filepath.Join(dataDir, "app.db"))
}
if err != nil {
return err
}
@@ -316,6 +329,15 @@ func run(log *slog.Logger) error {
return err
}
// A new server has no owner yet: a link to create one.
if cfg.Edition != config.Local {
if link, err := setupLink(database, publicURL); err != nil {
return err
} else if link != "" {
log.Warn("Supermarket isn't set up yet. To create its owner, open this link (it works once, for a day): " + link)
}
}
httpServer := &http.Server{
Addr: addr,
Handler: handler,
@@ -379,6 +401,47 @@ func run(log *slog.Logger) error {
return nil
}
// setupLink makes a new one-time link to create the owner, or returns ""
// if there's an owner already.
func setupLink(d *db.DB, publicURL string) (string, error) {
done, err := users.HasOwner(d, users.Team)
if err != nil || done {
return "", err
}
token, err := users.NewLink(d, users.SetupLink, 0, time.Now())
if err != nil {
return "", err
}
return publicURL + "/setup/" + token, nil
}
// setupLinkCommand runs `supermarket setup-link`: a new link to create the
// owner, replacing the one in the start-up log.
func setupLinkCommand() error {
cfg, err := config.Load(os.Getenv)
if err != nil {
return fmt.Errorf("the settings need fixing (supermarket config shows them):\n%w", err)
}
if cfg.Edition == config.Local {
return errors.New("the local edition has no sign-in, so nothing to set up: open " + cfg.PublicURL)
}
d, _, err := openForCommand(cfg)
if err != nil {
return err
}
defer d.Close()
link, err := setupLink(d, cfg.PublicURL)
if err != nil {
return err
}
if link == "" {
return errors.New("Supermarket is set up already: its owner signs in, and invites the others (Settings → Users)")
}
fmt.Println("Open this link to create Supermarket's owner. It works once, for a day, and replaces any earlier link:")
fmt.Println(link)
return nil
}
// mcpToken reads the bearer token agents use for /mcp, creating it if there
// is none. install.sh may create it first, to give it to Hermes.
func mcpToken(path string) (string, error) {