fix(invite): Back to application lands in the workspace, not on /start
ci / shared (pull_request) Failing after 15s
ci / test (pull_request) Successful in 25m47s
ci / image (pull_request) Skipped

INVITE_REDIRECT_URI may contain {slug}; the registry replaces it with the
workspace slug at all three invite sites (new workspace, member invite,
resend). Stage and prod currently point at /start?invited=1, which the
portal does not handle, so invitees landed on the trial signup form after
setting their password. With https://app.breakpilot.com/{slug}/dashboard
they arrive signed in in their workspace. A redirect without {slug} stays
as configured, so this is safe to deploy before the env change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Sharang ParnerkarandClaude Opus 5.5 committed 2026-10-19 10:06:29 +02:00
1 parent 06ad8d9b55
commit bb414fd241
4 files changed
+33 -6

No files matched your search

+4 -1
View File
@@ -38,7 +38,10 @@ type HTTPConfig struct {
// Invite mail: which client + redirect the execute-actions email uses.
// Defaults keep the dev realm working; stage/prod set portal-{env} and
// the portal's public URL.
// the portal's public URL. "{slug}" in the redirect is replaced by the
// workspace slug, so "Back to application" lands in the workspace
// (e.g. https://app.breakpilot.com/start?invited=1&ws={slug}, which the
// portal forwards to /<slug>/dashboard; Keycloak only allows /start*).
InviteClientID string
InviteRedirectURI string
}
+2 -2
View File
@@ -148,7 +148,7 @@ func (a *HTTPAdapter) InviteMember(ctx context.Context, in MemberInviteInput) (*
_ = resp.Body.Close()
res := &MemberInviteResult{UserID: userID, Status: InviteStatusInvited, Created: created}
if _, merr := a.executeActionsEmail(ctx, userID, []string{"VERIFY_EMAIL", "UPDATE_PASSWORD"}, a.cfg.InviteRedirectURI); merr != nil {
if _, merr := a.executeActionsEmail(ctx, userID, []string{"VERIFY_EMAIL", "UPDATE_PASSWORD"}, a.inviteRedirect(in.Slug)); merr != nil {
res.MailError = merr
}
return res, nil
@@ -204,7 +204,7 @@ func (a *HTTPAdapter) ResendInvite(ctx context.Context, slug, email string) erro
}
for _, m := range members {
if strings.EqualFold(m.Email, email) {
_, err := a.executeActionsEmail(ctx, m.UserID, []string{"VERIFY_EMAIL", "UPDATE_PASSWORD"}, a.cfg.InviteRedirectURI)
_, err := a.executeActionsEmail(ctx, m.UserID, []string{"VERIFY_EMAIL", "UPDATE_PASSWORD"}, a.inviteRedirect(slug))
return err
}
}
+20 -2
View File
@@ -22,6 +22,7 @@ type membersKC struct {
users map[string]memberRepresentation // user id → user
brief bool // member list omits attributes
mails []string // user ids that got the execute-actions mail
redirects []string // redirect_uri of each execute-actions mail
created []userCreate
puts map[string]map[string][]string // user id → attributes PUT
createErr int // non-zero: POST /users answers this status
@@ -181,11 +182,12 @@ func newMembersKC(t *testing.T) (*membersKC, *HTTPAdapter) {
mux.HandleFunc("PUT /admin/realms/test-realm/users/{id}/execute-actions-email", func(w http.ResponseWriter, r *http.Request) {
k.mu.Lock()
defer k.mu.Unlock()
if r.URL.Query().Get("client_id") != "portal-test" || r.URL.Query().Get("redirect_uri") != "https://portal.test/onboard" {
if r.URL.Query().Get("client_id") != "portal-test" {
http.Error(w, "bad invite client", http.StatusBadRequest)
return
}
k.mails = append(k.mails, r.PathValue("id"))
k.redirects = append(k.redirects, r.URL.Query().Get("redirect_uri"))
w.WriteHeader(http.StatusNoContent)
})
mux.HandleFunc("GET /admin/realms/test-realm/users/{id}/credentials", func(w http.ResponseWriter, r *http.Request) {
@@ -261,7 +263,7 @@ func newMembersKC(t *testing.T) (*membersKC, *HTTPAdapter) {
t.Cleanup(srv.Close)
a := NewHTTPAdapter(HTTPConfig{
BaseURL: srv.URL, Realm: "test-realm", ClientID: "c", ClientSecret: "s", Timeout: 5 * time.Second,
InviteClientID: "portal-test", InviteRedirectURI: "https://portal.test/onboard",
InviteClientID: "portal-test", InviteRedirectURI: "https://portal.test/{slug}/dashboard",
})
return k, a
}
@@ -326,6 +328,10 @@ func TestHTTPAdapter_InviteMember_newUser(t *testing.T) {
if len(k.mails) != 1 || k.mails[0] != res.UserID {
t.Errorf("execute-actions mail: %v", k.mails)
}
// "Back to application" in the invite must land in the workspace, not on /start.
if len(k.redirects) != 1 || k.redirects[0] != "https://portal.test/acme/dashboard" {
t.Errorf("invite redirect: %v", k.redirects)
}
if got := k.members["org-acme"]; len(got) != 2 || got[1] != res.UserID {
t.Errorf("membership: %v", got)
}
@@ -441,3 +447,15 @@ func TestListOrgMembers_findsOrgByAliasNotName(t *testing.T) {
t.Fatalf("unknown alias: want ErrOrgNotFound, got %v", err)
}
}
func TestHTTPAdapter_InviteRedirect(t *testing.T) {
a := NewHTTPAdapter(HTTPConfig{InviteRedirectURI: "https://app.test/{slug}/dashboard"})
if got := a.inviteRedirect("acme"); got != "https://app.test/acme/dashboard" {
t.Errorf("slug placeholder: %s", got)
}
// A fixed redirect without the placeholder stays as configured.
b := NewHTTPAdapter(HTTPConfig{InviteRedirectURI: "https://app.test/start"})
if got := b.inviteRedirect("acme"); got != "https://app.test/start" {
t.Errorf("fixed redirect: %s", got)
}
}
+7 -1
View File
@@ -167,7 +167,7 @@ func (a *HTTPAdapter) CreateOrgAndInvite(ctx context.Context, in InviteInput) (*
if in.EmailVerified {
actions = []string{"UPDATE_PASSWORD"}
}
inviteURL, err := a.executeActionsEmail(ctx, userID, actions, a.cfg.InviteRedirectURI)
inviteURL, err := a.executeActionsEmail(ctx, userID, actions, a.inviteRedirect(in.Slug))
if err != nil {
// Non-fatal: the account exists, the mail can be resent.
return &InviteResult{OrganizationID: orgID, UserID: userID, MailError: err}, nil
@@ -296,6 +296,12 @@ func (a *HTTPAdapter) findUserByEmail(ctx context.Context, email string) (string
return "", errUserLookupMiss
}
// inviteRedirect is the configured invite redirect with "{slug}" replaced by
// the workspace slug.
func (a *HTTPAdapter) inviteRedirect(slug string) string {
return strings.ReplaceAll(a.cfg.InviteRedirectURI, "{slug}", url.PathEscape(slug))
}
func (a *HTTPAdapter) executeActionsEmail(ctx context.Context, userID string, actions []string, redirectURI string) (string, error) {
resp, err := a.adminCall(ctx, http.MethodPut,
fmt.Sprintf("/users/%s/execute-actions-email?client_id=%s&redirect_uri=%s", userID, url.QueryEscape(a.cfg.InviteClientID), url.QueryEscape(redirectURI)),