E
E
Eugene2016-09-12 06:41:13
go
Eugene, 2016-09-12 06:41:13

Authorization (Beego/session). How does it work?

I can't figure out how the sessions module works in beego. (
github.com/astaxie/beego/session)session.go

package authSession

import (
 "encoding/json"
 "fmt"

 "github.com/astaxie/beego/session"
)

var (
 GlobalSessions *session.Manager
 conf           = &session.ManagerConfig{}
)

func Init() {

 config := `{"cookieName":"gosessionid", "gclifetime":3600, "enableSetCookie":true, "providerConfig":"./tmp"}`

 if err := json.Unmarshal([]byte(config), conf); err != nil {
 fmt.Println("json decode error", err)
 }

 GlobalSessions, _ = session.NewManager("file", conf)
 go GlobalSessions.GC()
}

Then in main.go
package main

import (
 "github.com/astaxie/beego"
 sess "github.com/ProjectDir/authSession"

 _ "github.com/ProjectDir/routers"
)

func main() {
 sess.Init()
 beego.Run()

}

router.go routes
package routers

import (
 "github.com/astaxie/beegor"

 "github.com/ProjectDir/controllers"
)

var (
 contr = &controllers.MainController{}
)

func init() {

 beego.Router("/", contr)
 beego.Router("/login", contr, "get:LoginHandler")
 beego.Router("/login", contr, "post:PostLoginHandler")
}

Then in the default.go controller
package controllers

import (
 "fmt"

 "github.com/astaxie/beego"

 s "github.com/ProjectDir/authSession"
 "github.com/ProjectDir/db/documents"
 "github.com/ProjectDir/models"
 "github.com/ProjectDir/models/db"
 "github.com/ProjectDir/utils"
)

type MainController struct {
 beego.Controller
}

func (c *MainController) Get() {

 c.Data["title"] = "Home page"
 c.TplName = "index.html"
}

func (c *MainController) LoginHandler() {
 
 c.Data["title"] = "Login"
 c.TplName = "login.html"
}

func (c *MainController) PostLoginHandler() {

 username := c.GetString("username")
 password := c.GetString("password")
 if username == "" && password == "" {
 c.LoginHandler()
 return
 }

 sess, err := s.GlobalSessions.SessionStart(c.Ctx.ResponseWriter, c.Ctx.Request)
 if err != nil {
 panic(err)
 }
 defer sess.SessionRelease(c.Ctx.ResponseWriter)
 err = sess.Set("username", username)
 err = sess.Set("IsAutorized", true)
 if err != nil {
 panic(err)
 }

 c.Data["isAutorized"] = sess.Get("IsAutorized")
 c.Data["title"] = "Login"
 c.TplName = "index.html"
 c.CruSession = sess

 c.Get()
}

There is nothing from session in c.Get()
This is after executing PostLoginHandler
fmt.Printf("%#v\n%#v\n%#v\n%#v\n", sess, username, password, c)
&session.FileSessionStore{sid:"10a706cb14c68b8fac778a0d1df85512", lock:sync.RWMutex{w:sync.Mutex{state:0, sema:0x0}, writerSem:0x0, readerSem:0x0, readerCount:0, readerWait:0}, values:map[interface {}]interface {}{"username":"1", "IsAutorized":true}}
"1"
"2"
&controllers.MainController{Controller:beego.Controller{Ctx:(*context.Context)(0xc420184b10), Data:map[interface {}]interface {}{"RouterPattern":"/login", "isAutorized":true, "title":"Login"}, controllerName:"MainController", actionName:"PostLoginHandler", methodMapping:map[string]func(){}, gotofunc:"", AppController:(*controllers.MainController)(0xc420304620), TplName:"index.html", Layout:"", LayoutSections:map[string]string(nil), TplPrefix:"", TplExt:"tpl", EnableRender:true, _xsrfToken:"", XSRFExpire:0, EnableXSRF:true, CruSession:(*session.FileSessionStore)(0xc42039c870)}}
<nil>
[SESSION]<nil>

And this is on any other page
fmt.Printf("%#v\n", c)
&controllers.MainController{Controller:beego.Controller{Ctx:(*context.Context)(0xc4202a3170), Data:map[interface {}]interface {}{"RouterPattern":"/"}, controllerName:"MainController", actionName:"GET", methodMapping:map[string]func(){}, gotofunc:"", AppController:(*controllers.MainController)(0xc4200dd340), TplName:"", Layout:"", LayoutSections:map[string]string(nil), TplPrefix:"", TplExt:"tpl", EnableRender:true, _xsrfToken:"", XSRFExpire:0, EnableXSRF:true, CruSession:session.Store(nil)}}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Chronic 86, 2016-09-12
@chronic86

It's a little unclear what you want to see in your files when you use a third party library?
If you want to see how it all works under the hood, go to github, open the source and read (for example , https://github.com/astaxie/beego/blob/master/sessi...
Or is it a rhetorical question from the category You explain to me on your fingers I'm too lazy to read someone else's code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question