Files
dex/examples/example-app/server/logout.go
Maksim Nabokikh 6f2e233c7a feat: example app session refactoring (#4712)
Signed-off-by: maksim.nabokikh <max.nabokih@gmail.com>
2026-04-02 14:19:10 +02:00

37 lines
911 B
Go

package server
import (
"net/http"
"net/url"
)
// handleAppLogout clears the local session and redirects to the provider's
// end_session_endpoint for RP-Initiated Logout (if available).
func (s *Server) handleAppLogout(w http.ResponseWriter, r *http.Request) {
idToken := s.auth.Clear()
if s.endSessionEndpoint == "" {
http.Redirect(w, r, "/", http.StatusFound)
return
}
logoutURL, err := url.Parse(s.endSessionEndpoint)
if err != nil {
http.Redirect(w, r, "/", http.StatusFound)
return
}
q := logoutURL.Query()
if idToken != "" {
q.Set("id_token_hint", idToken)
}
// Derive app base URL from redirect URI for post-logout redirect.
if appURL, err := url.Parse(s.redirectURI); err == nil {
appURL.Path = "/"
appURL.RawQuery = ""
q.Set("post_logout_redirect_uri", appURL.String())
}
logoutURL.RawQuery = q.Encode()
http.Redirect(w, r, logoutURL.String(), http.StatusFound)
}