mirror of https://github.com/shunfei/cronsun
store session data manually
parent
e3fcfb59b7
commit
e4c5cac014
|
@ -235,6 +235,8 @@ func (this *Administrator) UpdateAccount(ctx *Context) {
|
||||||
if ctx.Session.Email == originAccount.Email {
|
if ctx.Session.Email == originAccount.Email {
|
||||||
ctx.Session.Email = ""
|
ctx.Session.Email = ""
|
||||||
delete(ctx.Session.Data, "role")
|
delete(ctx.Session.Data, "role")
|
||||||
|
ctx.Session.Store()
|
||||||
|
|
||||||
outJSONWithCode(ctx.W, http.StatusUnauthorized, nil)
|
outJSONWithCode(ctx.W, http.StatusUnauthorized, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -123,6 +123,8 @@ func (this *Authentication) GetAuthSession(ctx *Context) {
|
||||||
|
|
||||||
ctx.Session.Email = u.Email
|
ctx.Session.Email = u.Email
|
||||||
ctx.Session.Data["role"] = u.Role
|
ctx.Session.Data["role"] = u.Role
|
||||||
|
ctx.Session.Store()
|
||||||
|
|
||||||
authInfo.Role = u.Role
|
authInfo.Role = u.Role
|
||||||
authInfo.Email = u.Email
|
authInfo.Email = u.Email
|
||||||
|
|
||||||
|
@ -133,6 +135,8 @@ func (this *Authentication) GetAuthSession(ctx *Context) {
|
||||||
func (this *Authentication) DeleteAuthSession(ctx *Context) {
|
func (this *Authentication) DeleteAuthSession(ctx *Context) {
|
||||||
ctx.Session.Email = ""
|
ctx.Session.Email = ""
|
||||||
delete(ctx.Session.Data, "role")
|
delete(ctx.Session.Data, "role")
|
||||||
|
ctx.Session.Store()
|
||||||
|
|
||||||
outJSONWithCode(ctx.W, http.StatusOK, nil)
|
outJSONWithCode(ctx.W, http.StatusOK, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -83,9 +83,6 @@ func authHandler(needAuth bool) func(*Context) bool {
|
||||||
ctx.Session, err = sessManager.Get(ctx.W, ctx.R)
|
ctx.Session, err = sessManager.Get(ctx.W, ctx.R)
|
||||||
if ctx.Session != nil {
|
if ctx.Session != nil {
|
||||||
ctx.Todo(func() {
|
ctx.Todo(func() {
|
||||||
if ctx.Session.Email == "" {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err := sessManager.Store(ctx.Session); err != nil {
|
if err := sessManager.Store(ctx.Session); err != nil {
|
||||||
log.Errorf("Failed to store session: %s.", err.Error())
|
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())
|
buf.Write(debug.Stack())
|
||||||
stack = buf.String()
|
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)
|
log.Errorf("%v\n\n%s\n", err_, stack)
|
||||||
return
|
return
|
||||||
|
|
|
@ -34,6 +34,7 @@ type storeData struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Session struct {
|
type Session struct {
|
||||||
|
m SessionManager
|
||||||
key string
|
key string
|
||||||
storeData
|
storeData
|
||||||
}
|
}
|
||||||
|
@ -41,6 +42,13 @@ type Session struct {
|
||||||
func (s *Session) ID() string {
|
func (s *Session) ID() string {
|
||||||
return s.key
|
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 {
|
type EtcdStore struct {
|
||||||
client *cronsun.Client
|
client *cronsun.Client
|
||||||
|
@ -63,6 +71,7 @@ func (this *EtcdStore) Get(w http.ResponseWriter, r *http.Request) (sess *Sessio
|
||||||
}
|
}
|
||||||
|
|
||||||
sess = &Session{
|
sess = &Session{
|
||||||
|
m: this,
|
||||||
storeData: storeData{
|
storeData: storeData{
|
||||||
Data: make(map[interface{}]interface{}, 2),
|
Data: make(map[interface{}]interface{}, 2),
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue