2020-05-07 06:34:17 +02:00
|
|
|
package example
|
2017-02-15 19:06:19 +01:00
|
|
|
|
|
|
|
import (
|
2020-05-07 06:34:17 +02:00
|
|
|
"errors"
|
2017-02-15 19:06:19 +01:00
|
|
|
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12"
|
|
|
|
"github.com/kataras/iris/v12/sessions"
|
2017-02-15 19:06:19 +01:00
|
|
|
)
|
|
|
|
|
2020-05-07 06:34:17 +02:00
|
|
|
// BusinessModel is just a Go struct value that we will use in our session example,
|
|
|
|
// never save sensitive information, like passwords, here.
|
|
|
|
type BusinessModel struct {
|
2017-06-08 13:39:55 +02:00
|
|
|
Name string
|
2017-06-08 02:39:15 +02:00
|
|
|
}
|
|
|
|
|
2020-05-07 06:34:17 +02:00
|
|
|
// NewApp returns a new application for showcasing the sessions feature.
|
|
|
|
func NewApp(sess *sessions.Sessions) *iris.Application {
|
2017-02-15 19:06:19 +01:00
|
|
|
app := iris.New()
|
2019-07-24 18:51:42 +02:00
|
|
|
app.Use(sess.Handler()) // session is always non-nil inside handlers now.
|
|
|
|
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Get("/", func(ctx iris.Context) {
|
2019-07-24 18:51:42 +02:00
|
|
|
session := sessions.Get(ctx) // same as sess.Start(ctx, cookieOptions...)
|
|
|
|
if session.Len() == 0 {
|
|
|
|
ctx.HTML(`no session values stored yet. Navigate to: <a href="/set">set page</a>`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.HTML("<ul>")
|
|
|
|
session.Visit(func(key string, value interface{}) {
|
|
|
|
ctx.HTML("<li> %s = %v </li>", key, value)
|
|
|
|
})
|
|
|
|
|
|
|
|
ctx.HTML("</ul>")
|
2017-02-15 19:06:19 +01:00
|
|
|
})
|
2019-07-24 18:51:42 +02:00
|
|
|
|
2019-08-17 09:06:20 +02:00
|
|
|
// set session values.
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Get("/set", func(ctx iris.Context) {
|
2019-07-24 18:51:42 +02:00
|
|
|
session := sessions.Get(ctx)
|
2020-06-19 04:54:21 +02:00
|
|
|
isNew := session.IsNew()
|
|
|
|
|
2020-10-17 05:40:17 +02:00
|
|
|
session.Set("username", "iris")
|
2017-02-15 19:06:19 +01:00
|
|
|
|
2020-10-17 05:40:17 +02:00
|
|
|
ctx.Writef("All ok session set to: %s [isNew=%t]", session.GetString("username"), isNew)
|
2020-05-07 06:34:17 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
app.Get("/get", func(ctx iris.Context) {
|
|
|
|
session := sessions.Get(ctx)
|
2017-06-08 02:39:15 +02:00
|
|
|
|
2020-05-07 06:34:17 +02:00
|
|
|
// get a specific value, as string,
|
|
|
|
// if not found then it returns just an empty string.
|
2020-10-17 05:40:17 +02:00
|
|
|
name := session.GetString("username")
|
2020-05-07 06:34:17 +02:00
|
|
|
|
2020-10-17 05:40:17 +02:00
|
|
|
ctx.Writef("The username on the /set was: %s", name)
|
2017-02-15 19:06:19 +01:00
|
|
|
})
|
|
|
|
|
2020-05-07 06:34:17 +02:00
|
|
|
app.Get("/set-struct", func(ctx iris.Context) {
|
|
|
|
session := sessions.Get(ctx)
|
|
|
|
session.Set("struct", BusinessModel{Name: "John Doe"})
|
|
|
|
|
2020-10-04 15:50:21 +02:00
|
|
|
ctx.WriteString("All ok session value of the 'struct' was set.")
|
2020-05-07 06:34:17 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
app.Get("/get-struct", func(ctx iris.Context) {
|
|
|
|
session := sessions.Get(ctx)
|
2020-10-04 15:50:21 +02:00
|
|
|
var v BusinessModel
|
|
|
|
if err := session.Decode("struct", &v); err != nil {
|
|
|
|
ctx.StopWithError(iris.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Writef("Session value of the 'struct' is: %#+v", v)
|
2020-05-07 06:34:17 +02:00
|
|
|
})
|
2019-07-24 18:51:42 +02:00
|
|
|
|
2020-05-07 06:34:17 +02:00
|
|
|
app.Get("/set/{key}/{value}", func(ctx iris.Context) {
|
2019-07-24 18:51:42 +02:00
|
|
|
session := sessions.Get(ctx)
|
2020-05-07 06:34:17 +02:00
|
|
|
|
|
|
|
key := ctx.Params().Get("key")
|
|
|
|
value := ctx.Params().Get("value")
|
2020-06-19 04:54:21 +02:00
|
|
|
isNew := session.IsNew()
|
|
|
|
|
2019-07-24 18:51:42 +02:00
|
|
|
session.Set(key, value)
|
|
|
|
|
2020-06-19 04:54:21 +02:00
|
|
|
ctx.Writef("All ok session value of the '%s' is: %s [isNew=%t]", key, session.GetString(key), isNew)
|
2019-07-24 18:51:42 +02:00
|
|
|
})
|
|
|
|
|
2020-05-07 06:34:17 +02:00
|
|
|
app.Get("/get/{key}", func(ctx iris.Context) {
|
|
|
|
session := sessions.Get(ctx)
|
|
|
|
// get a specific key, as string, if no found returns just an empty string
|
|
|
|
key := ctx.Params().Get("key")
|
2020-05-18 17:43:39 +02:00
|
|
|
value := session.Get(key)
|
2017-02-15 19:06:19 +01:00
|
|
|
|
2020-05-18 17:43:39 +02:00
|
|
|
ctx.Writef("The [%s:%T] on the /set was: %v", key, value, value)
|
|
|
|
})
|
|
|
|
|
|
|
|
app.Get("/set/{type}/{key}/{value}", func(ctx iris.Context) {
|
|
|
|
session := sessions.Get(ctx)
|
|
|
|
|
|
|
|
key := ctx.Params().Get("key")
|
|
|
|
var value interface{}
|
|
|
|
|
|
|
|
switch ctx.Params().Get("type") {
|
|
|
|
case "int":
|
|
|
|
value = ctx.Params().GetIntDefault("value", 0)
|
|
|
|
case "float64":
|
|
|
|
value = ctx.Params().GetFloat64Default("value", 0.0)
|
|
|
|
default:
|
|
|
|
value = ctx.Params().Get("value")
|
|
|
|
}
|
|
|
|
session.Set(key, value)
|
|
|
|
|
|
|
|
value = session.Get(key)
|
|
|
|
ctx.Writef("Key: %s, Type: %T, Value: %v", key, value, value)
|
2017-02-15 19:06:19 +01:00
|
|
|
})
|
|
|
|
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Get("/delete", func(ctx iris.Context) {
|
2020-05-07 06:34:17 +02:00
|
|
|
session := sessions.Get(ctx)
|
2017-02-15 19:06:19 +01:00
|
|
|
// delete a specific key
|
2021-01-06 00:52:39 +01:00
|
|
|
session.Delete("username")
|
2017-02-15 19:06:19 +01:00
|
|
|
})
|
|
|
|
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Get("/clear", func(ctx iris.Context) {
|
2020-05-07 06:34:17 +02:00
|
|
|
session := sessions.Get(ctx)
|
2017-12-24 05:24:45 +01:00
|
|
|
// removes all entries.
|
2020-05-07 06:34:17 +02:00
|
|
|
session.Clear()
|
2017-02-15 19:06:19 +01:00
|
|
|
})
|
|
|
|
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Get("/update", func(ctx iris.Context) {
|
2020-05-07 06:34:17 +02:00
|
|
|
session := sessions.Get(ctx)
|
|
|
|
// shifts the expiration based on the session's `Lifetime`.
|
|
|
|
if err := session.Man.ShiftExpiration(ctx); err != nil {
|
|
|
|
if errors.Is(err, sessions.ErrNotFound) {
|
|
|
|
ctx.StatusCode(iris.StatusNotFound)
|
|
|
|
} else if errors.Is(err, sessions.ErrNotImplemented) {
|
|
|
|
ctx.StatusCode(iris.StatusNotImplemented)
|
|
|
|
} else {
|
|
|
|
ctx.StatusCode(iris.StatusNotModified)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Writef("%v", err)
|
|
|
|
ctx.Application().Logger().Error(err)
|
|
|
|
}
|
2017-08-01 07:34:18 +02:00
|
|
|
})
|
|
|
|
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Get("/destroy", func(ctx iris.Context) {
|
2020-05-07 06:34:17 +02:00
|
|
|
session := sessions.Get(ctx)
|
|
|
|
// Man(anager)'s Destroy, removes the entire session data and cookie
|
|
|
|
session.Man.Destroy(ctx)
|
2017-03-18 22:43:04 +01:00
|
|
|
})
|
2020-05-07 06:34:17 +02:00
|
|
|
|
2017-03-18 22:43:04 +01:00
|
|
|
// Note about Destroy:
|
2017-02-15 19:06:19 +01:00
|
|
|
//
|
|
|
|
// You can destroy a session outside of a handler too, using the:
|
2019-07-24 18:51:42 +02:00
|
|
|
// sess.DestroyByID
|
|
|
|
// sess.DestroyAll
|
2017-02-15 19:06:19 +01:00
|
|
|
|
2017-06-08 14:19:04 +02:00
|
|
|
// remember: slices and maps are muttable by-design
|
|
|
|
// The `SetImmutable` makes sure that they will be stored and received
|
|
|
|
// as immutable, so you can't change them directly by mistake.
|
|
|
|
//
|
|
|
|
// Use `SetImmutable` consistently, it's slower than `Set`.
|
|
|
|
// Read more about muttable and immutable go types: https://stackoverflow.com/a/8021081
|
2020-05-07 06:34:17 +02:00
|
|
|
app.Get("/set-immutable", func(ctx iris.Context) {
|
2019-07-24 18:51:42 +02:00
|
|
|
session := sessions.Get(ctx)
|
2020-05-07 06:34:17 +02:00
|
|
|
|
|
|
|
business := []BusinessModel{{Name: "Edward"}, {Name: "value 2"}}
|
2019-07-24 18:51:42 +02:00
|
|
|
session.SetImmutable("businessEdit", business)
|
2020-10-04 15:50:21 +02:00
|
|
|
var businessGet []BusinessModel
|
|
|
|
err := session.Decode("businessEdit", &businessGet)
|
|
|
|
if err != nil {
|
|
|
|
ctx.StopWithError(iris.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
2017-06-08 14:19:04 +02:00
|
|
|
|
|
|
|
// try to change it, if we used `Set` instead of `SetImmutable` this
|
|
|
|
// change will affect the underline array of the session's value "businessEdit", but now it will not.
|
2017-06-08 13:39:55 +02:00
|
|
|
businessGet[0].Name = "Gabriel"
|
|
|
|
})
|
|
|
|
|
2020-05-07 06:34:17 +02:00
|
|
|
app.Get("/get-immutable", func(ctx iris.Context) {
|
2020-10-04 15:50:21 +02:00
|
|
|
var models []BusinessModel
|
|
|
|
err := sessions.Get(ctx).Decode("businessEdit", &models)
|
|
|
|
if err != nil {
|
|
|
|
ctx.StopWithError(iris.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if models == nil {
|
2020-05-07 06:34:17 +02:00
|
|
|
ctx.HTML("please navigate to the <a href='/set_immutable'>/set-immutable</a> first")
|
2017-06-08 14:19:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-04 15:50:21 +02:00
|
|
|
firstModel := models[0]
|
2017-06-08 14:19:04 +02:00
|
|
|
// businessGet[0].Name is equal to Edward initially
|
|
|
|
if firstModel.Name != "Edward" {
|
2017-06-08 13:39:55 +02:00
|
|
|
panic("Report this as a bug, immutable data cannot be changed from the caller without re-SetImmutable")
|
|
|
|
}
|
2017-06-08 14:19:04 +02:00
|
|
|
|
|
|
|
ctx.Writef("[]businessModel[0].Name remains: %s", firstModel.Name)
|
|
|
|
|
2017-06-08 13:39:55 +02:00
|
|
|
// the name should remains "Edward"
|
|
|
|
})
|
|
|
|
|
2020-05-07 06:34:17 +02:00
|
|
|
return app
|
2017-02-15 19:06:19 +01:00
|
|
|
}
|