store session data manually

pull/19/head
Doflatango 2017-06-30 12:03:54 +08:00
parent e3fcfb59b7
commit e4c5cac014
4 changed files with 16 additions and 4 deletions

View File

@ -235,6 +235,8 @@ func (this *Administrator) UpdateAccount(ctx *Context) {
if ctx.Session.Email == originAccount.Email {
ctx.Session.Email = ""
delete(ctx.Session.Data, "role")
ctx.Session.Store()
outJSONWithCode(ctx.W, http.StatusUnauthorized, nil)
return
}

View File

@ -123,6 +123,8 @@ func (this *Authentication) GetAuthSession(ctx *Context) {
ctx.Session.Email = u.Email
ctx.Session.Data["role"] = u.Role
ctx.Session.Store()
authInfo.Role = u.Role
authInfo.Email = u.Email
@ -133,6 +135,8 @@ func (this *Authentication) GetAuthSession(ctx *Context) {
func (this *Authentication) DeleteAuthSession(ctx *Context) {
ctx.Session.Email = ""
delete(ctx.Session.Data, "role")
ctx.Session.Store()
outJSONWithCode(ctx.W, http.StatusOK, nil)
}

View File

@ -83,9 +83,6 @@ func authHandler(needAuth bool) func(*Context) bool {
ctx.Session, err = sessManager.Get(ctx.W, ctx.R)
if ctx.Session != nil {
ctx.Todo(func() {
if ctx.Session.Email == "" {
return
}
if err := sessManager.Store(ctx.Session); err != nil {
log.Errorf("Failed to store session: %s.", err.Error())
}
@ -124,7 +121,7 @@ func (b BaseHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
buf.Write(debug.Stack())
stack = buf.String()
// outJSONWithCode(ctx.W, http.StatusInternalServerError, "Internal Server Error")
outJSONWithCode(w, http.StatusInternalServerError, "Internal Server Error")
log.Errorf("%v\n\n%s\n", err_, stack)
return

View File

@ -34,6 +34,7 @@ type storeData struct {
}
type Session struct {
m SessionManager
key string
storeData
}
@ -41,6 +42,13 @@ type Session struct {
func (s *Session) ID() string {
return s.key
}
func (s *Session) Store() error {
err := s.m.Store(s)
if err != nil {
log.Errorf("Failed to store session[%s]: %s", s.key, err.Error())
}
return err
}
type EtcdStore struct {
client *cronsun.Client
@ -63,6 +71,7 @@ func (this *EtcdStore) Get(w http.ResponseWriter, r *http.Request) (sess *Sessio
}
sess = &Session{
m: this,
storeData: storeData{
Data: make(map[interface{}]interface{}, 2),
},