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 (
"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 *")

View File

@ -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}

View File

@ -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,

View File

@ -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 });
}