user registration

trunk
vilmibm 2022-04-28 14:08:05 -05:00
parent 9703d88c66
commit 3ed669f560
3 changed files with 28 additions and 10 deletions

2
go.mod
View File

@ -5,3 +5,5 @@ go 1.18
require gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b require gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
require github.com/mattn/go-sqlite3 v1.14.12 require github.com/mattn/go-sqlite3 v1.14.12
require github.com/google/uuid v1.3.0 // indirect

2
go.sum
View File

@ -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 h1:TJ1bhYJPV44phC+IMu1u2K/i5RriLTPe+yc68XDJ1Z0=
github.com/mattn/go-sqlite3 v1.14.12/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= 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= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

View File

@ -11,7 +11,9 @@ import (
"net/http" "net/http"
"os" "os"
"strings" "strings"
"time"
"github.com/google/uuid"
_ "github.com/mattn/go-sqlite3" _ "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{ writeErrorResponse(w, 500, BBJResponse{
Error: true, Error: true,
Data: "server error", Data: "server error",
@ -231,11 +234,15 @@ func setupAPI(opts Opts) {
return return
} }
if args.AuthHash == "" || args.Username == "" {
invalidArgs(w)
return
}
db := opts.DB db := opts.DB
stmt, err := db.Prepare("select auth_hash from users where user_name = ?") stmt, err := db.Prepare("select auth_hash from users where user_name = ?")
if err != nil { if err != nil {
opts.Logf("user_register error: %s", err.Error()) serverErr(w, err)
serverErr(w)
return return
} }
defer stmt.Close() defer stmt.Close()
@ -253,12 +260,21 @@ func setupAPI(opts Opts) {
}) })
return return
} else if err != nil && !strings.Contains(err.Error(), "no rows in result") { } else if err != nil && !strings.Contains(err.Error(), "no rows in result") {
opts.Logf("user_register error: %s", err.Error()) serverErr(w, err)
serverErr(w)
return 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{ writeResponse(w, BBJResponse{
Data: true, // TODO probably something else 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 = ?") stmt, err := db.Prepare("select auth_hash from users where user_name = ?")
if err != nil { if err != nil {
opts.Logf("check_auth error: %s", err.Error()) serverErr(w, err)
serverErr(w)
return return
} }
defer stmt.Close() defer stmt.Close()
@ -305,8 +320,7 @@ func setupAPI(opts Opts) {
Data: "user not found", Data: "user not found",
}) })
} else { } else {
opts.Logf("check_auth error: %s", err.Error()) serverErr(w, err)
serverErr(w)
} }
return return
} }