From 272e5f1fac0e80269a480cb2cc0ea45be699657f Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Thu, 7 Sep 2017 11:18:18 +0800 Subject: [PATCH] Update Auth API based on session updates --- context.go | 2 +- handlers.go | 10 +++++----- session_storer.go | 24 ++++++++++++------------ utils.go | 6 +++--- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/context.go b/context.go index f74ceb3..8c2a203 100644 --- a/context.go +++ b/context.go @@ -18,7 +18,7 @@ type Context struct { // Flashes get flash messages func (context Context) Flashes() []session.Message { - return context.Auth.SessionStorer.Flashes(context.Request) + return context.Auth.SessionStorer.Flashes(context.Writer, context.Request) } // FormValue get form value with name diff --git a/handlers.go b/handlers.go index b55bd7a..d102fe4 100644 --- a/handlers.go +++ b/handlers.go @@ -18,7 +18,7 @@ import ( func respondAfterLogged(claims *claims.Claims, context *Context) { // login user - context.Auth.Login(claims, context.Request) + context.Auth.Login(context.Writer, context.Request, claims) responder.With("html", func() { // write cookie @@ -37,12 +37,12 @@ var DefaultLoginHandler = func(context *Context, authorize func(*Context) (*clai ) if err == nil && claims != nil { - context.SessionStorer.Flash(req, session.Message{Message: "logged"}) + context.SessionStorer.Flash(w, req, session.Message{Message: "logged"}) respondAfterLogged(claims, context) return } - context.SessionStorer.Flash(req, session.Message{Message: template.HTML(err.Error()), Type: "error"}) + context.SessionStorer.Flash(w, req, session.Message{Message: template.HTML(err.Error()), Type: "error"}) // error handling responder.With("html", func() { @@ -65,7 +65,7 @@ var DefaultRegisterHandler = func(context *Context, register func(*Context) (*cl return } - context.SessionStorer.Flash(req, session.Message{Message: template.HTML(err.Error()), Type: "error"}) + context.SessionStorer.Flash(w, req, session.Message{Message: template.HTML(err.Error()), Type: "error"}) // error handling responder.With("html", func() { @@ -78,7 +78,7 @@ var DefaultRegisterHandler = func(context *Context, register func(*Context) (*cl // DefaultLogoutHandler default logout behaviour var DefaultLogoutHandler = func(context *Context) { // Clear auth session - context.SessionStorer.Delete(context.Request) + context.SessionStorer.Delete(context.Writer, context.Request) context.Auth.Redirector.Redirect(context.Writer, context.Request, "logout") } diff --git a/session_storer.go b/session_storer.go index 5b015b6..60d52e5 100644 --- a/session_storer.go +++ b/session_storer.go @@ -15,14 +15,14 @@ type SessionStorerInterface interface { // Get get claims from request Get(req *http.Request) (*claims.Claims, error) // Update update claims with session manager - Update(claims *claims.Claims, req *http.Request) error + Update(w http.ResponseWriter, req *http.Request, claims *claims.Claims) error // Delete delete session - Delete(req *http.Request) error + Delete(w http.ResponseWriter, req *http.Request) error // Flash add flash message to session data - Flash(req *http.Request, message session.Message) error + Flash(w http.ResponseWriter, req *http.Request, message session.Message) error // Flashes returns a slice of flash messages from session data - Flashes(req *http.Request) []session.Message + Flashes(w http.ResponseWriter, req *http.Request) []session.Message // SignedToken generate signed token with Claims SignedToken(claims *claims.Claims) string @@ -51,25 +51,25 @@ func (sessionStorer *SessionStorer) Get(req *http.Request) (*claims.Claims, erro } // Update update claims with session manager -func (sessionStorer *SessionStorer) Update(claims *claims.Claims, req *http.Request) error { +func (sessionStorer *SessionStorer) Update(w http.ResponseWriter, req *http.Request, claims *claims.Claims) error { token := sessionStorer.SignedToken(claims) - return sessionStorer.SessionManager.Add(req, sessionStorer.SessionName, token) + return sessionStorer.SessionManager.Add(w, req, sessionStorer.SessionName, token) } // Delete delete claims from session manager -func (sessionStorer *SessionStorer) Delete(req *http.Request) error { - sessionStorer.SessionManager.Pop(req, sessionStorer.SessionName) +func (sessionStorer *SessionStorer) Delete(w http.ResponseWriter, req *http.Request) error { + sessionStorer.SessionManager.Pop(w, req, sessionStorer.SessionName) return nil } // Flash add flash message to session data -func (sessionStorer *SessionStorer) Flash(req *http.Request, message session.Message) error { - return sessionStorer.SessionManager.Flash(req, message) +func (sessionStorer *SessionStorer) Flash(w http.ResponseWriter, req *http.Request, message session.Message) error { + return sessionStorer.SessionManager.Flash(w, req, message) } // Flashes returns a slice of flash messages from session data -func (sessionStorer *SessionStorer) Flashes(req *http.Request) []session.Message { - return sessionStorer.SessionManager.Flashes(req) +func (sessionStorer *SessionStorer) Flashes(w http.ResponseWriter, req *http.Request) []session.Message { + return sessionStorer.SessionManager.Flashes(w, req) } // SignedToken generate signed token with Claims diff --git a/utils.go b/utils.go index 2f9a111..9638dbb 100644 --- a/utils.go +++ b/utils.go @@ -39,15 +39,15 @@ func (auth *Auth) GetDB(request *http.Request) *gorm.DB { } // Login sign user in -func (auth *Auth) Login(claimer claims.ClaimerInterface, req *http.Request) error { +func (auth *Auth) Login(w http.ResponseWriter, req *http.Request, claimer claims.ClaimerInterface) error { claims := claimer.ToClaims() now := time.Now() claims.LastLoginAt = &now - return auth.SessionStorer.Update(claims, req) + return auth.SessionStorer.Update(w, req, claims) } // Logout sign current user out func (auth *Auth) Logout(w http.ResponseWriter, req *http.Request) { - auth.SessionStorer.Delete(req) + auth.SessionStorer.Delete(w, req) }