Skip to content

Commit

Permalink
Serve prompt data on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed Oct 6, 2023
1 parent 3d34862 commit 78f61bd
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion internal/cli/universal_login_customize.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"context"
"encoding/json"
"fmt"
"net"
"net/http"
Expand Down Expand Up @@ -40,7 +41,7 @@ type (
promptData struct {
Language string `json:"language"`
Prompt string `json:"prompt"`
CustomText map[string]map[string]interface{} `json:"custom_text"`
CustomText map[string]map[string]interface{} `json:"custom_tex,omitempty"`
}

webSocketHandler struct {
Expand All @@ -49,6 +50,11 @@ type (
api *auth0.API
brandingData *universalLoginBrandingData
}

webSocketMessage struct {
Type string `json:"type"`
Payload json.RawMessage `json:"payload"`
}
)

func customizeUniversalLoginCmd(cli *cli) *cobra.Command {
Expand Down Expand Up @@ -326,6 +332,41 @@ func (h *webSocketHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.shutdown()
return
}

for {
var message webSocketMessage
if err := connection.ReadJSON(&message); err != nil {
h.display.Errorf("failed to read WebSocket message: %v", err)
continue
}

switch message.Type {
case "fetch_prompt":
var promptToFetch promptData
if err := json.Unmarshal(message.Payload, &promptToFetch); err != nil {
h.display.Errorf("failed to unmarshal %q payload: %v", message.Type, err)
continue
}

promptToSend, err := fetchPromptCustomTextWithDefaults(
r.Context(),
h.api,
promptToFetch.Prompt,
promptToFetch.Language,
)
if err != nil {
h.display.Errorf("failed to fetch custom text for prompt: %v", err)
continue
}

if err = connection.WriteJSON(promptToSend); err != nil {
h.display.Errorf("failed to send prompt data message: %v", err)
continue
}
case "save_branding":
h.display.Warnf("not yet implemented")
}
}
}

func checkOriginFunc(r *http.Request) bool {
Expand Down

0 comments on commit 78f61bd

Please sign in to comment.