fix(api): return correct struct in /auth/discord/callback

This commit is contained in:
Sam 2022-06-17 15:18:44 +02:00
parent ecd049088a
commit ad2c527e0e
2 changed files with 31 additions and 3 deletions

View File

@ -32,8 +32,8 @@ type oauthCallbackRequest struct {
type discordCallbackResponse struct { type discordCallbackResponse struct {
HasAccount bool `json:"has_account"` // if true, Token and User will be set. if false, Ticket and Discord will be set HasAccount bool `json:"has_account"` // if true, Token and User will be set. if false, Ticket and Discord will be set
Token string `json:"token,omitempty"` Token string `json:"token,omitempty"`
User *db.User `json:"user,omitempty"` User *userResponse `json:"user,omitempty"`
Discord string `json:"discord,omitempty"` // username, for UI purposes Discord string `json:"discord,omitempty"` // username, for UI purposes
Ticket string `json:"ticket,omitempty"` Ticket string `json:"ticket,omitempty"`
@ -87,8 +87,9 @@ func (s *Server) discordCallback(w http.ResponseWriter, r *http.Request) error {
render.JSON(w, r, discordCallbackResponse{ render.JSON(w, r, discordCallbackResponse{
HasAccount: true, HasAccount: true,
Token: token, Token: token,
User: &u, User: dbUserToUserResponse(u),
}) })
return nil return nil
} else if err != db.ErrUserNotFound { // internal error } else if err != db.ErrUserNotFound { // internal error

View File

@ -4,11 +4,13 @@ import (
"net/http" "net/http"
"os" "os"
"codeberg.org/u1f320/pronouns.cc/backend/db"
"codeberg.org/u1f320/pronouns.cc/backend/log" "codeberg.org/u1f320/pronouns.cc/backend/log"
"codeberg.org/u1f320/pronouns.cc/backend/server" "codeberg.org/u1f320/pronouns.cc/backend/server"
"emperror.dev/errors" "emperror.dev/errors"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/go-chi/render" "github.com/go-chi/render"
"github.com/rs/xid"
) )
type Server struct { type Server struct {
@ -17,6 +19,31 @@ type Server struct {
RequireInvite bool RequireInvite bool
} }
type userResponse struct {
ID xid.ID `json:"id"`
Username string `json:"username"`
DisplayName *string `json:"display_name"`
Bio *string `json:"bio"`
AvatarURL *string `json:"avatar_url"`
Links []string `json:"links"`
Discord *string `json:"discord"`
DiscordUsername *string `json:"discord_username"`
}
func dbUserToUserResponse(u db.User) *userResponse {
return &userResponse{
ID: u.ID,
Username: u.Username,
DisplayName: u.DisplayName,
Bio: u.Bio,
AvatarURL: u.AvatarURL,
Links: u.Links,
Discord: u.Discord,
DiscordUsername: u.DiscordUsername,
}
}
func Mount(srv *server.Server, r chi.Router) { func Mount(srv *server.Server, r chi.Router) {
s := &Server{ s := &Server{
Server: srv, Server: srv,