Skip to content

Commit

Permalink
Update Auth API based on session updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Sep 7, 2017
1 parent 23af472 commit 272e5f1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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")
}

Expand Down
24 changes: 12 additions & 12 deletions session_storer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 272e5f1

Please sign in to comment.