From 1080d8a0cdaa4a6cddf7887f326889524c8b021c Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 18 Nov 2022 02:26:40 +0100 Subject: [PATCH] fix: save discord ID in db, add HTTP status to new error codes --- backend/db/user.go | 4 ++++ backend/routes/auth/discord.go | 7 +++++-- backend/server/errors.go | 4 ++++ frontend/pages/login/discord.tsx | 2 ++ 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/backend/db/user.go b/backend/db/user.go index f5b0a6d..db726c7 100644 --- a/backend/db/user.go +++ b/backend/db/user.go @@ -2,6 +2,7 @@ package db import ( "context" + "fmt" "regexp" "emperror.dev/errors" @@ -97,7 +98,10 @@ func (db *DB) DiscordUser(ctx context.Context, discordID string) (u User, err er } func (u *User) UpdateFromDiscord(ctx context.Context, db pgxscan.Querier, du *discordgo.User) error { + fmt.Println(du.ID, du.String()) + builder := sq.Update("users"). + Set("discord", du.ID). Set("discord_username", du.String()). Where("id = ?", u.ID). Suffix("RETURNING *") diff --git a/backend/routes/auth/discord.go b/backend/routes/auth/discord.go index bd6e33f..582cb43 100644 --- a/backend/routes/auth/discord.go +++ b/backend/routes/auth/discord.go @@ -1,6 +1,7 @@ package auth import ( + "fmt" "net/http" "os" @@ -156,7 +157,7 @@ func (s *Server) discordSignup(w http.ResponseWriter, r *http.Request) error { } defer tx.Rollback(ctx) - var du discordgo.User + du := new(discordgo.User) err = s.DB.GetJSON(ctx, "discord:"+req.Ticket, &du) if err != nil { log.Errorf("getting discord user for ticket: %v", err) @@ -164,12 +165,14 @@ func (s *Server) discordSignup(w http.ResponseWriter, r *http.Request) error { return server.APIError{Code: server.ErrInvalidTicket} } + fmt.Println(du.String()) + u, err := s.DB.CreateUser(ctx, tx, req.Username) if err != nil { return errors.Wrap(err, "creating user") } - err = u.UpdateFromDiscord(ctx, tx, &du) + err = u.UpdateFromDiscord(ctx, tx, du) if err != nil { if errors.Cause(err) == db.ErrUsernameTaken { return server.APIError{Code: server.ErrUsernameTaken} diff --git a/backend/server/errors.go b/backend/server/errors.go index 47ac3e4..f998387 100644 --- a/backend/server/errors.go +++ b/backend/server/errors.go @@ -127,6 +127,10 @@ var errCodeStatuses = map[int]int{ ErrInvalidState: http.StatusBadRequest, ErrInvalidOAuthCode: http.StatusForbidden, ErrInvalidToken: http.StatusUnauthorized, + ErrInviteRequired: http.StatusBadRequest, + ErrInvalidTicket: http.StatusBadRequest, + ErrInvalidUsername: http.StatusBadRequest, + ErrUsernameTaken: http.StatusBadRequest, ErrUserNotFound: http.StatusNotFound, diff --git a/frontend/pages/login/discord.tsx b/frontend/pages/login/discord.tsx index caa5a79..ce29552 100644 --- a/frontend/pages/login/discord.tsx +++ b/frontend/pages/login/discord.tsx @@ -97,6 +97,8 @@ export default function Discord() { setUser(resp.user); localStorage.setItem("pronouns-token", resp.token); + + router.push("/"); } catch (e) { setState({ ...state, error: e }); }