56 lines
1.1 KiB
Go
56 lines
1.1 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"projectreshoot/internal/models"
|
|
"time"
|
|
|
|
"git.haelnorr.com/h/golib/hws"
|
|
"git.haelnorr.com/h/golib/hwsauth"
|
|
"github.com/uptrace/bun"
|
|
)
|
|
|
|
// Handle a logout request
|
|
func Logout(
|
|
server *hws.Server,
|
|
auth *hwsauth.Authenticator[*models.UserBun, bun.Tx],
|
|
db *bun.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 := db.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
err := server.ThrowError(w, r, hws.HWSError{
|
|
StatusCode: http.StatusInternalServerError,
|
|
Message: "Logout failed",
|
|
Error: err,
|
|
})
|
|
if err != nil {
|
|
server.ThrowFatal(w, err)
|
|
}
|
|
return
|
|
}
|
|
defer tx.Rollback()
|
|
|
|
err = auth.Logout(tx, w, r)
|
|
if err != nil {
|
|
err := server.ThrowError(w, r, hws.HWSError{
|
|
StatusCode: http.StatusInternalServerError,
|
|
Message: "Logout failed",
|
|
Error: err,
|
|
})
|
|
if err != nil {
|
|
server.ThrowFatal(w, err)
|
|
}
|
|
return
|
|
}
|
|
tx.Commit()
|
|
w.Header().Set("HX-Redirect", "/login")
|
|
},
|
|
)
|
|
}
|