feat: implement logout functionality
This commit is contained in:
@@ -108,3 +108,18 @@ func SetSessionCookie(w http.ResponseWriter, sessionID string, expiresAt time.Ti
|
|||||||
Path: "/",
|
Path: "/",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Service) Logout(ctx context.Context, sessionID string) error {
|
||||||
|
return s.db.DeleteSession(ctx, sessionID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ClearSessionCookie(w http.ResponseWriter) {
|
||||||
|
http.SetCookie(w, &http.Cookie{
|
||||||
|
Name: "session_id",
|
||||||
|
Value: "",
|
||||||
|
Expires: time.Unix(0, 0),
|
||||||
|
MaxAge: -1,
|
||||||
|
HttpOnly: true,
|
||||||
|
Path: "/",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -68,3 +68,13 @@ func (h *Handler) HandleLogin(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *Handler) HandleLogout(w http.ResponseWriter, r *http.Request) {
|
||||||
|
cookie, err := r.Cookie("session_id")
|
||||||
|
if err == nil {
|
||||||
|
_ = h.authService.Logout(r.Context(), cookie.Value)
|
||||||
|
}
|
||||||
|
|
||||||
|
ClearSessionCookie(w)
|
||||||
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||||
|
}
|
||||||
|
|||||||
@@ -96,6 +96,7 @@ func NewRouter(cfg Config) http.Handler {
|
|||||||
pkgmiddleware.RateLimitAuth(pkgmiddleware.VerifyOrigin(http.HandlerFunc(authHandler.HandleLogin))).ServeHTTP(w, r)
|
pkgmiddleware.RateLimitAuth(pkgmiddleware.VerifyOrigin(http.HandlerFunc(authHandler.HandleLogin))).ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
mux.HandleFunc("/logout", authHandler.HandleLogout)
|
||||||
|
|
||||||
// Watchlist Endpoints
|
// Watchlist Endpoints
|
||||||
mux.HandleFunc("/api/watchlist/card", watchlistHandler.HandleCardWatchlist)
|
mux.HandleFunc("/api/watchlist/card", watchlistHandler.HandleCardWatchlist)
|
||||||
|
|||||||
Reference in New Issue
Block a user