Skip to content

Commit 821987e

Browse files
committed
Delete cookie
1 parent c1ad6c8 commit 821987e

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

GoBlog.exe

8.05 MB
Binary file not shown.

goblog.db

32 KB
Binary file not shown.

main.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,22 @@ func LoginHandler(w http.ResponseWriter, req *http.Request, p httprouter.Params)
6262
}
6363

6464
func LogoutHandler(w http.ResponseWriter, req *http.Request, p httprouter.Params) {
65+
cookie, err := req.Cookie("goblog")
66+
if err != nil {
67+
fmt.Println(err)
68+
}
6569
delete := http.Cookie{Name: "goblog", Value: "delete", Expires: time.Now(), HttpOnly: true, Path: "/"}
6670
http.SetCookie(w, &delete)
67-
// Delete cookie from DB
68-
71+
db, err := bolt.Open("goblog.db", 0600, nil)
72+
if err != nil {
73+
fmt.Println(err)
74+
}
75+
defer db.Close()
76+
db.Update(func(tx *bolt.Tx) error {
77+
b := tx.Bucket([]byte("CookieBucket"))
78+
err := b.Delete([]byte(cookie.Value))
79+
return err
80+
})
6981
http.Redirect(w, req, "/", http.StatusFound)
7082
}
7183

0 commit comments

Comments
 (0)