mirror of
https://github.com/kataras/iris.git
synced 2026-01-18 01:15:59 +00:00
organise sessions examples
Former-commit-id: 682472d2cf4ebfc740687522fe5eef77b5bb1a72
This commit is contained in:
163
_examples/sessions/overview/example/example.go
Normal file
163
_examples/sessions/overview/example/example.go
Normal file
@@ -0,0 +1,163 @@
|
||||
package example
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/kataras/iris/v12/sessions"
|
||||
)
|
||||
|
||||
// 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 {
|
||||
Name string
|
||||
}
|
||||
|
||||
// NewApp returns a new application for showcasing the sessions feature.
|
||||
func NewApp(sess *sessions.Sessions) *iris.Application {
|
||||
app := iris.New()
|
||||
app.Use(sess.Handler()) // session is always non-nil inside handlers now.
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
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>")
|
||||
})
|
||||
|
||||
// set session values.
|
||||
app.Get("/set", func(ctx iris.Context) {
|
||||
session := sessions.Get(ctx)
|
||||
session.Set("name", "iris")
|
||||
|
||||
ctx.Writef("All ok session set to: %s", session.GetString("name"))
|
||||
})
|
||||
|
||||
app.Get("/get", func(ctx iris.Context) {
|
||||
session := sessions.Get(ctx)
|
||||
|
||||
// get a specific value, as string,
|
||||
// if not found then it returns just an empty string.
|
||||
name := session.GetString("name")
|
||||
|
||||
ctx.Writef("The name on the /set was: %s", name)
|
||||
})
|
||||
|
||||
app.Get("/set-struct", func(ctx iris.Context) {
|
||||
session := sessions.Get(ctx)
|
||||
session.Set("struct", BusinessModel{Name: "John Doe"})
|
||||
|
||||
ctx.Writef("All ok session value of the 'struct' is: %v", session.Get("struct"))
|
||||
})
|
||||
|
||||
app.Get("/get-struct", func(ctx iris.Context) {
|
||||
session := sessions.Get(ctx)
|
||||
ctx.Writef("Session value of the 'struct' is: %v", session.Get("struct"))
|
||||
})
|
||||
|
||||
app.Get("/set/{key}/{value}", func(ctx iris.Context) {
|
||||
session := sessions.Get(ctx)
|
||||
|
||||
key := ctx.Params().Get("key")
|
||||
value := ctx.Params().Get("value")
|
||||
session.Set(key, value)
|
||||
|
||||
ctx.Writef("All ok session value of the '%s' is: %s", key, session.GetString(key))
|
||||
})
|
||||
|
||||
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")
|
||||
name := session.GetString(key)
|
||||
|
||||
ctx.Writef("The name on the /set was: %s", name)
|
||||
})
|
||||
|
||||
app.Get("/delete", func(ctx iris.Context) {
|
||||
session := sessions.Get(ctx)
|
||||
// delete a specific key
|
||||
session.Delete("name")
|
||||
})
|
||||
|
||||
app.Get("/clear", func(ctx iris.Context) {
|
||||
session := sessions.Get(ctx)
|
||||
// removes all entries.
|
||||
session.Clear()
|
||||
})
|
||||
|
||||
app.Get("/update", func(ctx iris.Context) {
|
||||
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)
|
||||
}
|
||||
})
|
||||
|
||||
app.Get("/destroy", func(ctx iris.Context) {
|
||||
session := sessions.Get(ctx)
|
||||
// Man(anager)'s Destroy, removes the entire session data and cookie
|
||||
session.Man.Destroy(ctx)
|
||||
})
|
||||
|
||||
// Note about Destroy:
|
||||
//
|
||||
// You can destroy a session outside of a handler too, using the:
|
||||
// sess.DestroyByID
|
||||
// sess.DestroyAll
|
||||
|
||||
// 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
|
||||
app.Get("/set-immutable", func(ctx iris.Context) {
|
||||
session := sessions.Get(ctx)
|
||||
|
||||
business := []BusinessModel{{Name: "Edward"}, {Name: "value 2"}}
|
||||
session.SetImmutable("businessEdit", business)
|
||||
businessGet := session.Get("businessEdit").([]BusinessModel)
|
||||
|
||||
// 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.
|
||||
businessGet[0].Name = "Gabriel"
|
||||
})
|
||||
|
||||
app.Get("/get-immutable", func(ctx iris.Context) {
|
||||
valSlice := sessions.Get(ctx).Get("businessEdit")
|
||||
if valSlice == nil {
|
||||
ctx.HTML("please navigate to the <a href='/set_immutable'>/set-immutable</a> first")
|
||||
return
|
||||
}
|
||||
|
||||
firstModel := valSlice.([]BusinessModel)[0]
|
||||
// businessGet[0].Name is equal to Edward initially
|
||||
if firstModel.Name != "Edward" {
|
||||
panic("Report this as a bug, immutable data cannot be changed from the caller without re-SetImmutable")
|
||||
}
|
||||
|
||||
ctx.Writef("[]businessModel[0].Name remains: %s", firstModel.Name)
|
||||
|
||||
// the name should remains "Edward"
|
||||
})
|
||||
|
||||
return app
|
||||
}
|
||||
@@ -1,50 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12/_examples/sessions/overview/example"
|
||||
"github.com/kataras/iris/v12/sessions"
|
||||
)
|
||||
|
||||
var (
|
||||
cookieNameForSessionID = "mycookiesessionnameid"
|
||||
sess = sessions.New(sessions.Config{Cookie: cookieNameForSessionID, AllowReclaim: true})
|
||||
)
|
||||
|
||||
func secret(ctx iris.Context) {
|
||||
// Check if user is authenticated
|
||||
if auth, _ := sess.Start(ctx).GetBoolean("authenticated"); !auth {
|
||||
ctx.StatusCode(iris.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
// Print secret message
|
||||
ctx.WriteString("The cake is a lie!")
|
||||
}
|
||||
|
||||
func login(ctx iris.Context) {
|
||||
session := sess.Start(ctx)
|
||||
|
||||
// Authentication goes here
|
||||
// ...
|
||||
|
||||
// Set user as authenticated
|
||||
session.Set("authenticated", true)
|
||||
}
|
||||
|
||||
func logout(ctx iris.Context) {
|
||||
session := sess.Start(ctx)
|
||||
|
||||
// Revoke users authentication
|
||||
session.Set("authenticated", false)
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/secret", secret)
|
||||
app.Get("/login", login)
|
||||
app.Get("/logout", logout)
|
||||
sess := sessions.New(sessions.Config{
|
||||
// Cookie string, the session's client cookie name, for example: "_session_id"
|
||||
//
|
||||
// Defaults to "irissessionid"
|
||||
Cookie: "_session_id",
|
||||
// it's time.Duration, from the time cookie is created, how long it can be alive?
|
||||
// 0 means no expire, unlimited life.
|
||||
// -1 means expire when browser closes
|
||||
// or set a value, like 2 hours:
|
||||
Expires: time.Hour * 2,
|
||||
// if you want to invalid cookies on different subdomains
|
||||
// of the same host, then enable it.
|
||||
// Defaults to false.
|
||||
DisableSubdomainPersistence: false,
|
||||
// Allow getting the session value stored by the request from the same request.
|
||||
AllowReclaim: true,
|
||||
})
|
||||
|
||||
app := example.NewApp(sess)
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user