1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-18 02:17:05 +00:00

organise sessions examples

Former-commit-id: 682472d2cf4ebfc740687522fe5eef77b5bb1a72
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-05-07 07:34:17 +03:00
parent b4365cee8d
commit cd62ba3712
11 changed files with 190 additions and 411 deletions

View File

@@ -0,0 +1,51 @@
package main
import (
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/sessions"
)
const cookieNameForSessionID = "session_id_cookie"
func secret(ctx iris.Context) {
// Check if user is authenticated
if auth, _ := sessions.Get(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 := sessions.Get(ctx)
// Authentication goes here
// ...
// Set user as authenticated
session.Set("authenticated", true)
}
func logout(ctx iris.Context) {
session := sessions.Get(ctx)
// Revoke users authentication
session.Set("authenticated", false)
}
func main() {
app := iris.New()
sess := sessions.New(sessions.Config{Cookie: cookieNameForSessionID, AllowReclaim: true})
app.Use(sess.Handler())
// ^ or comment this line and use sess.Start(ctx) inside your handlers
// instead of sessions.Get(ctx).
app.Get("/secret", secret)
app.Get("/login", login)
app.Get("/logout", logout)
app.Listen(":8080")
}