Sessions

Create a main.go file and copy-paste the following code snippets:

package main

import (
	"fmt"
	"time"

	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"github.com/kataras/iris/v12/sessions"
)

func main() {
    app := iris.New()

	// Configure sessions manager as we used to.
	sess := sessions.New(sessions.Config{Cookie: "mysession_cookie_name"})
	app.Use(sess.Handler())

    visitApp := mvc.New(app)
    visitApp.Register(time.Now())
	visitApp.Handle(new(VisitController))

	app.Listen(":8080")
}

Controller

  • The VisitController.Session is automatically binded to the current sessions.Session.

  • The VisitController.StartTime is statically set to the server's start time with .Register(time.Now()) above.

Run

Open a terminal session and execute:

  1. Prepare a client, e.g. your browser

  2. navigate to http://localhost:8080

  3. refresh the page some times

  4. close the browser

  5. re-open the browser (if it wasn't in private mode) and re-play 2.

Last updated

Was this helpful?