fix: save discord ID in db, add HTTP status to new error codes

This commit is contained in:
Sam 2022-11-18 02:26:40 +01:00
parent 9a3c51459b
commit 1080d8a0cd
4 changed files with 15 additions and 2 deletions

View File

@ -2,6 +2,7 @@ package db
import ( import (
"context" "context"
"fmt"
"regexp" "regexp"
"emperror.dev/errors" "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 { func (u *User) UpdateFromDiscord(ctx context.Context, db pgxscan.Querier, du *discordgo.User) error {
fmt.Println(du.ID, du.String())
builder := sq.Update("users"). builder := sq.Update("users").
Set("discord", du.ID).
Set("discord_username", du.String()). Set("discord_username", du.String()).
Where("id = ?", u.ID). Where("id = ?", u.ID).
Suffix("RETURNING *") Suffix("RETURNING *")

View File

@ -1,6 +1,7 @@
package auth package auth
import ( import (
"fmt"
"net/http" "net/http"
"os" "os"
@ -156,7 +157,7 @@ func (s *Server) discordSignup(w http.ResponseWriter, r *http.Request) error {
} }
defer tx.Rollback(ctx) defer tx.Rollback(ctx)
var du discordgo.User du := new(discordgo.User)
err = s.DB.GetJSON(ctx, "discord:"+req.Ticket, &du) err = s.DB.GetJSON(ctx, "discord:"+req.Ticket, &du)
if err != nil { if err != nil {
log.Errorf("getting discord user for ticket: %v", err) 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} return server.APIError{Code: server.ErrInvalidTicket}
} }
fmt.Println(du.String())
u, err := s.DB.CreateUser(ctx, tx, req.Username) u, err := s.DB.CreateUser(ctx, tx, req.Username)
if err != nil { if err != nil {
return errors.Wrap(err, "creating user") return errors.Wrap(err, "creating user")
} }
err = u.UpdateFromDiscord(ctx, tx, &du) err = u.UpdateFromDiscord(ctx, tx, du)
if err != nil { if err != nil {
if errors.Cause(err) == db.ErrUsernameTaken { if errors.Cause(err) == db.ErrUsernameTaken {
return server.APIError{Code: server.ErrUsernameTaken} return server.APIError{Code: server.ErrUsernameTaken}

View File

@ -127,6 +127,10 @@ var errCodeStatuses = map[int]int{
ErrInvalidState: http.StatusBadRequest, ErrInvalidState: http.StatusBadRequest,
ErrInvalidOAuthCode: http.StatusForbidden, ErrInvalidOAuthCode: http.StatusForbidden,
ErrInvalidToken: http.StatusUnauthorized, ErrInvalidToken: http.StatusUnauthorized,
ErrInviteRequired: http.StatusBadRequest,
ErrInvalidTicket: http.StatusBadRequest,
ErrInvalidUsername: http.StatusBadRequest,
ErrUsernameTaken: http.StatusBadRequest,
ErrUserNotFound: http.StatusNotFound, ErrUserNotFound: http.StatusNotFound,

View File

@ -97,6 +97,8 @@ export default function Discord() {
setUser(resp.user); setUser(resp.user);
localStorage.setItem("pronouns-token", resp.token); localStorage.setItem("pronouns-token", resp.token);
router.push("/");
} catch (e) { } catch (e) {
setState({ ...state, error: e }); setState({ ...state, error: e });
} }