mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 18:51:03 +01:00
3945fa68d1
We have to do the same on iris-contrib/examples, iris-contrib/middleware and e.t.c. Former-commit-id: 0860688158f374bc137bc934b81b26dcd0e10964
51 lines
967 B
Go
51 lines
967 B
Go
package main
|
|
|
|
import (
|
|
"github.com/kataras/iris/v12"
|
|
|
|
"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)
|
|
|
|
app.Run(iris.Addr(":8080"))
|
|
}
|