Creating your first session variable
Whenever we need to pass on the user data from one HTTP request to another, we can make use of HTTP sessions, which we will be covering in this recipe.
Getting ready…
This recipe assumes you have Redis installed and running locally on port 6379.
How to do it…
- Install the
github.com/astaxie/beego/session/redispackage using thego getcommand, as follows:
$ go get -u github.com/astaxie/beego/session/redis- Move to
$GOPATH/src/my-first-beego-project/controllersand createsessioncontroller.go, where we will define handlers which make sure that only authenticated users can view the home page, as follows:
package controllers
import "github.com/astaxie/beego"
type SessionController struct
{
beego.Controller
}
func (this *SessionController) Home()
{
isAuthenticated := this.GetSession("authenticated")
if isAuthenticated == nil || isAuthenticated == false
{
this.Ctx.WriteString("You are unauthorized to
view the page.")
return
}
this.Ctx...