package
2.1.0
Repository: https://github.com/flannel-dev-lab/cyclops.git
Documentation: pkg.go.dev

# README

Handling Cookies

  • Cookies in cyclops are described using the structure CyclopsCookie
  • The fields in the structure are as follows:
AttributeOptional
NameNo
ValueNo
PathYes
DomainNo
SecureYes
HttpOnlyYes
SameSiteYes
ExpiresYes
MaxAgeYes

Creating a Cookie:

import "github.com/flannel-dev-lab/cyclops/v2/cookie"

func Login(w http.ResponseWriter, r *http.Request) {
    cookieObj := cookie.CyclopsCookie{
            Name:           "test",
            Value:          "test",
            Secure:         false,
            HttpOnly:       true,
            StrictSameSite: false,
        }
    
    cookieObj.SetCookie(w)
}

Once the cookie is created, we use the method SetCookie which takes in a http.ResponseWriter to write the cookie

Reading a cookie:

import "github.com/flannel-dev-lab/cyclops/v2/cookie"

func Login(w http.ResponseWriter, r *http.Request) {
    cookieObj := cookie.CyclopsCookie{}

    fmt.Println(cookieObj.GetCookie(r, "test"))
}

To read a cookie, we create an empty cookie object and call the GetCookie method which takes in *http.Request, the Name of the cookie and returns a *http.Cookie

Reading all cookies

import "github.com/flannel-dev-lab/cyclops/v2/cookie"

func Login(w http.ResponseWriter, r *http.Request) {
    cookieObj := cookie.CyclopsCookie{}

    fmt.Println(cookieObj.GetAll(r, "test"))
}

To read a cookie, we create an empty cookie object and call the GetAll method which takes in *http.Request and returns a array of *http.Cookie

Deleting a Cookie

Deletes a cookie by setting max-age to 0

import "github.com/flannel-dev-lab/cyclops/v2/cookie"

func Login(w http.ResponseWriter, r *http.Request) {
    cyclopsCookie := cookie.CyclopsCookie{}

    cookie, _ := cookieObj.GetCookie(r, "test")
    
    cyclopsCookie.Delete(w, cookie)
}

# Constants

DefaultExpiry is the default expiry for a cookie if a user does not sets it.

# Structs

CyclopsCookie is a struct to hold cookie info.