2021-09-11 11:37:31 +00:00
|
|
|
/*
|
2024-05-12 08:20:06 +00:00
|
|
|
Copyright CAcert Inc.
|
2023-07-29 15:46:33 +00:00
|
|
|
SPDX-License-Identifier: Apache-2.0
|
2021-09-11 11:37:31 +00:00
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
2021-09-11 11:37:31 +00:00
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
https://www.apache.org/licenses/LICENSE-2.0
|
2021-09-11 11:37:31 +00:00
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
2021-09-11 11:37:31 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2024-05-12 10:02:27 +00:00
|
|
|
"log/slog"
|
2021-09-11 11:37:31 +00:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
type AfterLogoutHandler struct {
|
2024-05-12 10:02:27 +00:00
|
|
|
logger *slog.Logger
|
2021-09-11 11:37:31 +00:00
|
|
|
}
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
func (h *AfterLogoutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2023-08-03 14:46:28 +00:00
|
|
|
session, err := GetSession(r)
|
2021-09-11 11:37:31 +00:00
|
|
|
if err != nil {
|
2024-05-12 10:02:27 +00:00
|
|
|
h.logger.Error("could not get session", "error", err)
|
2021-09-11 11:37:31 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
return
|
|
|
|
}
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
session.Options.MaxAge = -1
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
if err = session.Save(r, w); err != nil {
|
2024-05-12 10:02:27 +00:00
|
|
|
h.logger.Error("could not save session", "error", err)
|
2021-09-11 11:37:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Location", "/")
|
|
|
|
w.WriteHeader(http.StatusFound)
|
|
|
|
}
|
|
|
|
|
2024-05-12 10:02:27 +00:00
|
|
|
func NewAfterLogoutHandler(logger *slog.Logger) *AfterLogoutHandler {
|
2023-07-29 15:46:33 +00:00
|
|
|
return &AfterLogoutHandler{logger: logger}
|
2021-09-11 11:37:31 +00:00
|
|
|
}
|