diff options
author | Michael Hunteman <michael@huntm.net> | 2024-06-23 13:55:42 -0700 |
---|---|---|
committer | Michael Hunteman <michael@huntm.net> | 2024-06-23 13:55:42 -0700 |
commit | 07752babb4e692452e1cd7f2133c4d8dde1b3b1c (patch) | |
tree | b3be7698f1af43f83bccd3bbbf6e19cd03532f1b /server/guests | |
parent | 4bf5d1a620dfe96ea9593d44cfcd0f142fcdec61 (diff) |
Authenticate UI users
Diffstat (limited to 'server/guests')
-rw-r--r-- | server/guests/models.go | 17 | ||||
-rw-r--r-- | server/guests/store.go | 31 |
2 files changed, 46 insertions, 2 deletions
diff --git a/server/guests/models.go b/server/guests/models.go index c68a4c3..5915f81 100644 --- a/server/guests/models.go +++ b/server/guests/models.go @@ -1,5 +1,7 @@ package guests +import "github.com/golang-jwt/jwt/v5" + type Guest struct { Id int `json:"id"` FirstName string `json:"firstName"` @@ -15,3 +17,18 @@ type PartyGuest struct { FirstName string `json:"firstName"` LastName string `json:"lastName"` } + +type Credentials struct { + FirstName string `json:"firstName"` + LastName string `json:"lastName"` +} + +type Claims struct { + Guest Guest `json:"guest"` + jwt.RegisteredClaims +} + +type LoginResponse struct { + Guest Guest `json:"guest"` + Token string `json:"token"` +} diff --git a/server/guests/store.go b/server/guests/store.go index 597cc80..bedc646 100644 --- a/server/guests/store.go +++ b/server/guests/store.go @@ -2,6 +2,7 @@ package guests import ( "context" + "errors" "github.com/jackc/pgx/v5/pgxpool" ) @@ -16,6 +17,26 @@ func NewMemStore(db *pgxpool.Pool) *MemStore { } } +func (m MemStore) FindGuest(creds Credentials) (Guest, error) { + rows, err := m.db.Query(context.Background(), "select * from guest") + var guest Guest + if err != nil { + return guest, err + } + defer rows.Close() + + for rows.Next() { + err := rows.Scan(&guest.Id, &guest.FirstName, &guest.LastName, &guest.Attendance, &guest.Email, &guest.Message, &guest.PartySize) + if err != nil { + return guest, err + } + if guest.FirstName == creds.FirstName && guest.LastName == creds.LastName { + return guest, nil + } + } + return guest, errors.New("Guest does not exist") +} + func (m MemStore) Get() ([]Guest, error) { rows, err := m.db.Query(context.Background(), "select * from guest") if err != nil { @@ -79,9 +100,15 @@ func (m MemStore) Update(guest Guest) error { return err } - statement = "update party set first_name = $1, last_name = $2 where guest_id = $3" + statement = "delete from party where guest_id = $1" + _, err = m.db.Exec(context.Background(), statement, guest.Id) + if err != nil { + return err + } + + statement = "insert into party (guest_id, first_name, last_name) values ($1, $2, $3)" for _, pg := range guest.PartyList { - _, err = m.db.Exec(context.Background(), statement, pg.FirstName, pg.LastName, guest.Id) + _, err = m.db.Exec(context.Background(), statement, guest.Id, pg.FirstName, pg.LastName) if err != nil { return err } |