Browse Source

working logout

main
Stephanie Gredell 6 months ago
parent
commit
501ecc6d8f
  1. 8
      handlers/auth.go
  2. 4
      main.go
  3. 2
      templates/welcome.html
  4. BIN
      tmp/main

8
handlers/auth.go

@ -50,6 +50,7 @@ func (h *Handler) Callback(w http.ResponseWriter, r *http.Request) { @@ -50,6 +50,7 @@ func (h *Handler) Callback(w http.ResponseWriter, r *http.Request) {
func (h *Handler) Logout(w http.ResponseWriter, r *http.Request) {
session, err := gothic.Store.Get(r, "user-session")
if err != nil {
fmt.Printf("error retrieving session: %v", err)
return
}
@ -62,5 +63,12 @@ func (h *Handler) Logout(w http.ResponseWriter, r *http.Request) { @@ -62,5 +63,12 @@ func (h *Handler) Logout(w http.ResponseWriter, r *http.Request) {
return
}
http.SetCookie(w, &http.Cookie{
Name: "user-session", // or whatever your session name is
Value: "",
Path: "/",
MaxAge: -1, // This deletes the cookie
})
http.Redirect(w, r, "/", http.StatusFound)
}

4
main.go

@ -22,8 +22,8 @@ func main() { @@ -22,8 +22,8 @@ func main() {
if err != nil {
panic("HELP ME LOAD SOME ENV FILE")
}
tmpl, err := template.ParseGlob("templates/*.html")
tmpl, err := template.ParseGlob("templates/*.html")
if err != nil {
panic(fmt.Errorf("failed to parse templates: %w\n", err))
}
@ -42,7 +42,7 @@ func main() { @@ -42,7 +42,7 @@ func main() {
// auth routes
mux.HandleFunc("/auth/twitch", h.Auth)
mux.HandleFunc("/auth/twitch/callback", h.Callback)
mux.HandleFunc("/logout/", h.Logout)
mux.HandleFunc("/logout", h.Logout)
// pages route
mux.HandleFunc("/", h.Home)

2
templates/welcome.html

@ -9,7 +9,7 @@ @@ -9,7 +9,7 @@
</head>
<body>
<header>WELCOME PAGE</header>
<header><a href="/logout">Logout</a></header>
<section id="home" class="hero">
<div class="container">

BIN
tmp/main

Binary file not shown.
Loading…
Cancel
Save