Files
projectreshoot/internal/handler/logout.go

41 lines
898 B
Go

package handler
import (
"context"
"database/sql"
"git.haelnorr.com/h/golib/hws"
"git.haelnorr.com/h/golib/hwsauth"
"net/http"
"projectreshoot/internal/models"
"time"
)
// Handle a logout request
func Logout(
server *hws.Server,
auth *hwsauth.Authenticator[*models.User],
conn *sql.DB,
) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 15*time.Second)
defer cancel()
tx, err := conn.BeginTx(ctx, nil)
if err != nil {
server.ThrowError(w, r, hws.NewError(http.StatusInternalServerError, "Logout failed", err))
return
}
defer tx.Rollback()
err = auth.Logout(tx, w, r)
if err != nil {
server.ThrowError(w, r, hws.NewError(http.StatusInternalServerError, "Logout failed", err))
return
}
tx.Commit()
w.Header().Set("HX-Redirect", "/login")
},
)
}