account page stubbed: link to etsy sign up

This commit is contained in:
2025-12-29 06:06:13 -07:00
parent 61f9afb39c
commit c9100383e4
35 changed files with 1326 additions and 158 deletions
+62 -1
View File
@@ -4,12 +4,22 @@ import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
"ruben/inventory2/internal/domains/platforms/etsy"
"ruben/inventory2/internal/domains/raw_events"
)
func NewWebhookHandler(db *raw_events.Store) http.Handler {
type Config struct {
OAuthRedirectURIWithAcctIDParam string
}
func NewWebhookHandler(
db *raw_events.Store,
platform *etsy.Platform,
cfg Config,
) http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("POST /test", func(w http.ResponseWriter, r *http.Request) {
@@ -44,5 +54,56 @@ func NewWebhookHandler(db *raw_events.Store) http.Handler {
w.WriteHeader(201)
})
mux.HandleFunc("GET "+cfg.OAuthRedirectURIWithAcctIDParam, func(w http.ResponseWriter, r *http.Request) {
// get account id for the request
acctID, err := strconv.ParseInt(r.PathValue("acctID"), 10, 64)
if err != nil || acctID <= 0 {
w.WriteHeader(http.StatusNotFound)
return
}
ctx := r.Context()
q := r.URL.Query()
state := q.Get("state")
// handle failed, potentially non-consenting, request
if errCode := q.Get("error"); errCode != "" {
errDesc := q.Get("error_description")
errURI := q.Get("error_uri")
fmt.Printf(
"error in obtaining an OAuth Token: error=%s, error_desc=%s, error_uri=%s, account_id=%d\n",
errCode,
errDesc,
errURI,
acctID,
)
platform.InvalidateState(ctx, state)
return
}
// validate the state to prevent CSRF attacks
ok, err := platform.HandleNewAuthCode(ctx, acctID, state, q.Get("code"))
if err != nil {
w.WriteHeader(http.StatusForbidden)
fmt.Println("failed to handle new auth code:", err)
return
}
if !ok {
w.WriteHeader(http.StatusForbidden)
return
}
// TODO: response with a redirect to the user's account page (SUCCESS - new sign up or login)!
http.Redirect(w, r, fmt.Sprintf("/site/accounts/%d", acctID), http.StatusSeeOther)
})
return mux
}
+9 -4
View File
@@ -3,18 +3,23 @@ package webhooks
import (
"net/http"
etsy_platform "ruben/inventory2/internal/domains/platforms/etsy"
"ruben/inventory2/internal/domains/raw_events"
"ruben/inventory2/internal/webhooks/etsy"
"ruben/inventory2/internal/webhooks/tiktok"
"ruben/inventory2/internal/webhooks/wix"
)
func New(db *raw_events.Store) http.Handler {
type Config struct {
Etsy etsy.Config
}
func New(eventsDB *raw_events.Store, etsyPlatform *etsy_platform.Platform, cfg Config) http.Handler {
wh := http.NewServeMux()
wh.Handle("/etsy/", http.StripPrefix("/etsy", etsy.NewWebhookHandler(db)))
wh.Handle("/tiktok/", http.StripPrefix("/tiktok", tiktok.NewWebhookHandler(db)))
wh.Handle("/wix/", http.StripPrefix("/wix", wix.NewWebhookHandler(db)))
wh.Handle("/etsy/", http.StripPrefix("/etsy", etsy.NewWebhookHandler(eventsDB, etsyPlatform, cfg.Etsy)))
wh.Handle("/tiktok/", http.StripPrefix("/tiktok", tiktok.NewWebhookHandler(eventsDB)))
wh.Handle("/wix/", http.StripPrefix("/wix", wix.NewWebhookHandler(eventsDB)))
return wh
}