Skip to content

Commit

Permalink
Handle close settings page on success query param authgear#3813
Browse files Browse the repository at this point in the history
  • Loading branch information
IniZio committed Feb 23, 2024
1 parent f675193 commit 311bb3c
Show file tree
Hide file tree
Showing 10 changed files with 953 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/auth/handler/webapp/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ var DependencySet = wire.NewSet(
wire.Struct(new(SettingsChangeSecondaryPasswordHandler), "*"),
wire.Struct(new(SettingsDeleteAccountHandler), "*"),
wire.Struct(new(SettingsDeleteAccountSuccessHandler), "*"),
wire.Struct(new(SettingsCloseHandler), "*"),
wire.Struct(new(SettingsPasskeyHandler), "*"),
wire.Struct(new(AccountStatusHandler), "*"),
wire.Struct(new(LogoutHandler), "*"),
Expand Down
52 changes: 52 additions & 0 deletions pkg/auth/handler/webapp/settings_close.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package webapp

import (
"net/http"

"github.com/authgear/authgear-server/pkg/auth/handler/webapp/viewmodels"
"github.com/authgear/authgear-server/pkg/util/httproute"
"github.com/authgear/authgear-server/pkg/util/template"
)

var TemplateWebSettingsCloseHTML = template.RegisterHTML(
"web/settings_close.html",
Components...,
)

func ConfigureSettingsCloseRoute(route httproute.Route) httproute.Route {
return route.
WithMethods("OPTIONS", "GET").
WithPathPattern("/settings/close")
}

type SettingsCloseHandler struct {
ControllerFactory ControllerFactory
BaseViewModel *viewmodels.BaseViewModeler
Renderer Renderer
}

func (h *SettingsCloseHandler) GetData(r *http.Request, w http.ResponseWriter) (map[string]interface{}, error) {
data := make(map[string]interface{})
baseViewModel := h.BaseViewModel.ViewModel(r, w)
viewmodels.Embed(data, baseViewModel)
return data, nil
}

func (h *SettingsCloseHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctrl, err := h.ControllerFactory.New(r, w)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
defer ctrl.Serve()

ctrl.Get(func() error {
data, err := h.GetData(r, w)
if err != nil {
return err
}

h.Renderer.RenderHTML(w, r, TemplateWebSettingsCloseHTML, data)
return nil
})
}
1 change: 1 addition & 0 deletions pkg/auth/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ func NewRouter(p *deps.RootProvider, configSource *configsource.ConfigSource) *h
router.Add(webapphandler.ConfigureSettingsChangePasswordRoute(webappSettingsSubRoutesRoute), p.Handler(newWebAppSettingsChangePasswordHandler))
router.Add(webapphandler.ConfigureSettingsChangeSecondaryPasswordRoute(webappSettingsSubRoutesRoute), p.Handler(newWebAppSettingsChangeSecondaryPasswordHandler))
router.Add(webapphandler.ConfigureSettingsDeleteAccountRoute(webappSettingsSubRoutesRoute), p.Handler(newWebAppSettingsDeleteAccountHandler))
router.Add(webapphandler.ConfigureSettingsCloseRoute(webappSettingsSubRoutesRoute), p.Handler(newWebAppSettingsCloseHandler))

router.Add(webapphandler.ConfigureTesterRoute(webappTesterRouter), p.Handler(newWebAppTesterHandler))

Expand Down
5 changes: 5 additions & 0 deletions pkg/auth/webapp/redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ func DeriveSettingsRedirectURIFromRequest(r *http.Request, defaultURI string) st
// 2. Default redirect URL
// 3. `/settings`
redirectURIFromQuery := func() string {
closeOnSuccess := r.URL.Query().Get("close_on_success")
if closeOnSuccess == "true" {
return "/settings/close"
}

redirectURI := r.URL.Query().Get("redirect_uri")
allowed := false

Expand Down
Loading

0 comments on commit 311bb3c

Please sign in to comment.