diff --git a/static/assets/icons/logout.svg b/static/assets/icons/logout.svg new file mode 100644 index 0000000..d6ec081 --- /dev/null +++ b/static/assets/icons/logout.svg @@ -0,0 +1,11 @@ + + + + + diff --git a/templates/base.gohtml b/templates/base.gohtml index 61fe3f8..5815835 100644 --- a/templates/base.gohtml +++ b/templates/base.gohtml @@ -31,6 +31,10 @@ Profile My Profile + + Logout + Logout + {{else}} Login diff --git a/web/session.go b/web/session.go index ff2047f..f83d709 100644 --- a/web/session.go +++ b/web/session.go @@ -76,3 +76,19 @@ func getUserIdByUsername(ctx context.Context, username string) (int, error) { Scan(&userId) return userId, err } + +func logoutHandler() http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + cookie, err := r.Cookie("session") + if err == nil { + deleteSession(cookie.Value) + } + http.SetCookie(w, &http.Cookie{ + Name: "session", + Value: "", + Path: "/", + MaxAge: -1, + }) + http.Redirect(w, r, "/login", http.StatusSeeOther) + } +} diff --git a/web/web.go b/web/web.go index 619647c..f145552 100644 --- a/web/web.go +++ b/web/web.go @@ -88,6 +88,7 @@ func Start() { r.Handle("/files/*", http.StripPrefix("/files", http.FileServer(http.Dir("./static")))) r.Get("/", rootHandler()) r.Get("/login", loginPageHandler()) + r.Get("/logout", logoutHandler()) r.Get("/createaccount", createAccountPageHandler()) r.Get("/profile/{username}", profilePageHandler()) r.Get("/profile/{username}/artist/{artist}", artistPageHandler())