func TestDataSessions(t *testing.T) { pool := newConnPool(t) userID, err := data.CreateUser(pool, newUser()) if err != nil { t.Fatal(err) } sessionID := []byte("deadbeef") err = data.InsertSession(pool, &data.Session{ ID: data.NewBytes(sessionID), UserID: data.NewInt32(userID), }, ) if err != nil { t.Fatal(err) } user, err := data.SelectUserBySessionID(pool, sessionID) if err != nil { t.Fatal(err) } if user.ID.Value != userID { t.Errorf("Expected %v, got %v", userID, user.ID) } err = data.DeleteSession(pool, sessionID) if err != nil { t.Fatal(err) } _, err = data.SelectUserBySessionID(pool, sessionID) if err != data.ErrNotFound { t.Fatalf("Expected %v, got %v", data.ErrNotFound, err) } err = data.DeleteSession(pool, sessionID) if err != data.ErrNotFound { t.Fatalf("Expected %v, got %v", notFound, err) } }
func DeleteSessionHandler(w http.ResponseWriter, req *http.Request, env *environment) { sessionID, err := hex.DecodeString(req.FormValue("id")) if err != nil { http.Error(w, "Internal server error", http.StatusInternalServerError) return } err = data.DeleteSession(env.pool, sessionID) if err != nil { http.Error(w, "Internal server error", http.StatusInternalServerError) return } cookie := &http.Cookie{Name: "sessionId", Value: "logged out", Expires: time.Unix(0, 0)} http.SetCookie(w, cookie) }