From 6fa11aba8e2827e4a2a7943d5b9a133e2f66ceb1 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Tue, 24 Oct 2023 19:23:35 +0000 Subject: [PATCH] code processing, TODOs --- cmd/help/main.go | 22 +++++++++++++++++++--- towndb/towndb.go | 6 +++--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/cmd/help/main.go b/cmd/help/main.go index 0ad2a31..bd80267 100644 --- a/cmd/help/main.go +++ b/cmd/help/main.go @@ -205,20 +205,36 @@ func redeemCode(db *sql.DB, cs colorScheme, p *Prompter) error { fmt.Println(cs.Header("redeem an auth code and add a new public key")) fmt.Println() c, err := p.String("paste your auth code:") + // TODO add Error to cs if err != nil { // TODO log + // TODO print an error, return nil return err } parts, err := codes.Decode(c) if err != nil { // TODO log + // TODO print an error, return nil return err } - code := parts[0] - email := parts[1] - fmt.Println(code, email) + code := &towndb.AuthCode{ + Code: parts[0], + Email: parts[1], + } + + err = code.Hydrate(db) + if err != nil { + // TODO log + // TODO print an error, return an opaque error about db + return err + } + + if code.Used { + fmt.Println("That code has already been redeemed. You'll have to request a new one.") + return nil + } // TODO verify code // TODO accept key diff --git a/towndb/towndb.go b/towndb/towndb.go index 074c069..01e175d 100644 --- a/towndb/towndb.go +++ b/towndb/towndb.go @@ -210,15 +210,15 @@ func (c *AuthCode) Insert(db *sql.DB) error { func (c *AuthCode) Hydrate(db *sql.DB) error { stmt, err := db.Prepare(` - SELECT id, email, used, created + SELECT id, used, created FROM auth_codes - WHERE code = ?`) + WHERE code = ? AND email = ?`) if err != nil { return err } defer stmt.Close() - return stmt.QueryRow(c.Code).Scan(&c.ID, &c.Email, &c.Used, &c.Created) + return stmt.QueryRow(c.Code).Scan(&c.ID, &c.Used, &c.Created) } // TODO other auth code as needed