mirror of
https://codeberg.org/pronounscc/pronouns.cc.git
synced 2025-02-12 23:03:37 +01:00
56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
|
package auth
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
|
||
|
"codeberg.org/pronounscc/pronouns.cc/backend/common"
|
||
|
"codeberg.org/pronounscc/pronouns.cc/backend/db"
|
||
|
"codeberg.org/pronounscc/pronouns.cc/backend/log"
|
||
|
"codeberg.org/pronounscc/pronouns.cc/backend/server"
|
||
|
"emperror.dev/errors"
|
||
|
"github.com/go-chi/render"
|
||
|
)
|
||
|
|
||
|
type EmailResponse struct {
|
||
|
ID common.EmailID `json:"id"`
|
||
|
Email string `json:"email"`
|
||
|
Confirmed bool `json:"confirmed"`
|
||
|
}
|
||
|
|
||
|
func dbEmailToResponse(e db.UserEmail) EmailResponse {
|
||
|
return EmailResponse{
|
||
|
ID: e.ID,
|
||
|
Email: e.EmailAddress,
|
||
|
Confirmed: e.Confirmed,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (s *Server) getEmails(w http.ResponseWriter, r *http.Request) error {
|
||
|
ctx := r.Context()
|
||
|
claims, _ := server.ClaimsFromContext(ctx)
|
||
|
if claims.APIToken {
|
||
|
return server.APIError{Code: server.ErrMissingPermissions, Details: "This endpoint cannot be used by API tokens"}
|
||
|
}
|
||
|
|
||
|
u, err := s.DB.User(ctx, claims.UserID)
|
||
|
if err != nil {
|
||
|
// this should never fail
|
||
|
log.Errorf("getting user: %v", err)
|
||
|
return errors.Wrap(err, "getting user")
|
||
|
}
|
||
|
|
||
|
emails, err := s.DB.UserEmails(ctx, u.SnowflakeID)
|
||
|
if err != nil {
|
||
|
log.Errorf("getting user emails: %v", err)
|
||
|
return errors.Wrap(err, "getting user emails")
|
||
|
}
|
||
|
|
||
|
out := make([]EmailResponse, len(emails))
|
||
|
for i := range emails {
|
||
|
out[i] = dbEmailToResponse(emails[i])
|
||
|
}
|
||
|
|
||
|
render.JSON(w, r, out)
|
||
|
return nil
|
||
|
}
|