Change magic link request to use url builder

Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
Émile Ré
2026-01-16 09:29:44 +04:00
committed by Bryan Frimin
parent a6826826b0
commit 5beceb58d2
3 changed files with 8 additions and 39 deletions

View File

@@ -116,15 +116,12 @@ type URLBuilder struct {
} }
// WithPath returns a URLBuilder with the specified path. // WithPath returns a URLBuilder with the specified path.
// The path will be properly joined with the base URL. // The path will be properly joined with the base URL's path.
func (b *BaseURL) WithPath(path string) *URLBuilder { func (b *BaseURL) WithPath(path string) *URLBuilder {
if b == nil { if b == nil {
return &URLBuilder{err: fmt.Errorf("base URL is nil")} return &URLBuilder{err: fmt.Errorf("base URL is nil")}
} }
// Ensure path does not end with /
basePath := strings.TrimSuffix(b.parsed.Path, "/")
// Ensure path starts with / // Ensure path starts with /
if !strings.HasPrefix(path, "/") { if !strings.HasPrefix(path, "/") {
path = "/" + path path = "/" + path
@@ -135,30 +132,11 @@ func (b *BaseURL) WithPath(path string) *URLBuilder {
return &URLBuilder{ return &URLBuilder{
base: b, base: b,
path: basePath + path, path: path,
query: make(url.Values), query: make(url.Values),
} }
} }
// WithPath returns a URLBuilder with the specified path.
// The path will be properly joined with the base URL.
func (ub *URLBuilder) WithPath(path string) *URLBuilder {
if ub == nil {
return ub
}
// Ensure path starts with /
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
// Ensure path does not end with /
path = strings.TrimSuffix(path, "/")
ub.path = ub.path + path
return ub
}
// WithQuery adds a query parameter to the URL. // WithQuery adds a query parameter to the URL.
func (ub *URLBuilder) WithQuery(key, value string) *URLBuilder { func (ub *URLBuilder) WithQuery(key, value string) *URLBuilder {
if ub.err != nil { if ub.err != nil {

View File

@@ -64,7 +64,7 @@ type (
SendMagicLinkRequest struct { SendMagicLinkRequest struct {
Email mail.Addr Email mail.Addr
BaseURL *baseurl.BaseURL BaseURL *baseurl.URLBuilder
} }
PasswordResetData struct { PasswordResetData struct {
@@ -560,7 +560,6 @@ func (s AuthService) SendMagicLink(ctx context.Context, req *SendMagicLinkReques
} }
magicLinkURL := req.BaseURL. magicLinkURL := req.BaseURL.
WithPath("/verify-magic-link").
WithQuery("token", token). WithQuery("token", token).
MustString() MustString()

View File

@@ -165,11 +165,7 @@ func (r *mutationResolver) SendMagicLink(ctx context.Context, input types.SendMa
} }
customDomain, err := r.trust.GetCustomDomainByOrganizationID(ctx, organization.ID) customDomain, err := r.trust.GetCustomDomainByOrganizationID(ctx, organization.ID)
if err != nil { if err != nil && !errors.Is(err, trust.ErrCustomDomainNotFound) {
if errors.Is(err, trust.ErrCustomDomainNotFound) {
return nil, gqlutils.NotFoundf(ctx, "custom domain not found")
}
r.logger.ErrorCtx(ctx, "cannot get custom domain", log.Error(err)) r.logger.ErrorCtx(ctx, "cannot get custom domain", log.Error(err))
return nil, gqlutils.Internal(ctx) return nil, gqlutils.Internal(ctx)
} }
@@ -185,15 +181,11 @@ func (r *mutationResolver) SendMagicLink(ctx context.Context, input types.SendMa
return nil, gqlutils.Internal(ctx) return nil, gqlutils.Internal(ctx)
} }
req.BaseURL = baseURL req.BaseURL = baseURL.WithPath("/verify-magic-link")
} else { } else {
baseURL, err := baseurl.Parse(r.baseURL.WithPath("/trust/" + trustCenter.ID.String()).MustString()) req.BaseURL = r.baseURL.WithPath(
if err != nil { fmt.Sprintf("/trust/%s/verify-magic-link", trustCenter.ID.String()),
r.logger.ErrorCtx(ctx, "cannot parse url", log.Error(err)) )
return nil, gqlutils.Internal(ctx)
}
req.BaseURL = baseURL
} }
if err := r.iam.AuthService.SendMagicLink(ctx, req); err != nil { if err := r.iam.AuthService.SendMagicLink(ctx, req); err != nil {