diff --git a/go.mod b/go.mod index b33d17d..ed636cb 100644 --- a/go.mod +++ b/go.mod @@ -5,3 +5,5 @@ go 1.18 require gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b require github.com/mattn/go-sqlite3 v1.14.12 + +require github.com/google/uuid v1.3.0 // indirect diff --git a/go.sum b/go.sum index 4d5935b..4fd6d04 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/mattn/go-sqlite3 v1.14.12 h1:TJ1bhYJPV44phC+IMu1u2K/i5RriLTPe+yc68XDJ1Z0= github.com/mattn/go-sqlite3 v1.14.12/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= diff --git a/server/cmd/main.go b/server/cmd/main.go index ced1611..19eccc3 100644 --- a/server/cmd/main.go +++ b/server/cmd/main.go @@ -11,7 +11,9 @@ import ( "net/http" "os" "strings" + "time" + "github.com/google/uuid" _ "github.com/mattn/go-sqlite3" ) @@ -193,7 +195,8 @@ func setupAPI(opts Opts) { }) })) - serverErr := func(w http.ResponseWriter) { + serverErr := func(w http.ResponseWriter, err error) { + opts.Logf(err.Error()) writeErrorResponse(w, 500, BBJResponse{ Error: true, Data: "server error", @@ -231,11 +234,15 @@ func setupAPI(opts Opts) { return } + if args.AuthHash == "" || args.Username == "" { + invalidArgs(w) + return + } + db := opts.DB stmt, err := db.Prepare("select auth_hash from users where user_name = ?") if err != nil { - opts.Logf("user_register error: %s", err.Error()) - serverErr(w) + serverErr(w, err) return } defer stmt.Close() @@ -253,12 +260,21 @@ func setupAPI(opts Opts) { }) return } else if err != nil && !strings.Contains(err.Error(), "no rows in result") { - opts.Logf("user_register error: %s", err.Error()) - serverErr(w) + serverErr(w, err) return } - // TODO register + stmt, err = db.Prepare(`INSERT INTO users VALUES (?, ?, ?, "", "", 0, 0, ?)`) + id, err := uuid.NewRandom() + if err != nil { + serverErr(w, err) + return + } + + _, err = stmt.Exec(id, args.Username, args.AuthHash, time.Now()) + if err != nil { + serverErr(w, err) + } writeResponse(w, BBJResponse{ Data: true, // TODO probably something else @@ -289,8 +305,7 @@ func setupAPI(opts Opts) { stmt, err := db.Prepare("select auth_hash from users where user_name = ?") if err != nil { - opts.Logf("check_auth error: %s", err.Error()) - serverErr(w) + serverErr(w, err) return } defer stmt.Close() @@ -305,8 +320,7 @@ func setupAPI(opts Opts) { Data: "user not found", }) } else { - opts.Logf("check_auth error: %s", err.Error()) - serverErr(w) + serverErr(w, err) } return }