Skip to content

Commit

Permalink
Refactor LoginHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Jun 13, 2017
1 parent 4ea1142 commit 522290b
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 45 deletions.
6 changes: 3 additions & 3 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ type Config struct {
AuthIdentityModel interface{}
Encryptor EncryptorInterface

LoginHandler func(*http.Request, http.ResponseWriter, func(*http.Request, http.ResponseWriter) (currentUser interface{}, err error))
LogoutHandler func(request *http.Request, writer http.ResponseWriter, currentUser interface{}, claims *Claims)
RegisterHandler func(request *http.Request, writer http.ResponseWriter, currentUser interface{}, claims *Claims)
LoginHandler func(*http.Request, http.ResponseWriter, *Session, func(*http.Request, http.ResponseWriter, *Session) (interface{}, error))
LogoutHandler func(request *http.Request, writer http.ResponseWriter, currentUser interface{}, session *Session)
RegisterHandler func(request *http.Request, writer http.ResponseWriter, currentUser interface{}, session *Session)
}

// New initialize Auth
Expand Down
34 changes: 21 additions & 13 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,13 @@ type serveMux struct {
*Auth
}

// URL generate URL for auth
func (serveMux *serveMux) AuthURL(pth string) string {
return path.Join(serveMux.Auth.Prefix, pth)
}

// ServeHTTP dispatches the handler registered in the matched route
func (serveMux *serveMux) ServeHTTP(w http.ResponseWriter, req *http.Request) {
var (
claims *Claims
reqPath = strings.TrimPrefix(req.URL.Path, serveMux.Prefix)
paths = strings.Split(reqPath, "/")
claims *Claims
session = &Session{Auth: serveMux.Auth, Claims: claims}
)

if len(paths) >= 2 {
Expand All @@ -35,15 +31,15 @@ func (serveMux *serveMux) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// serve mux
switch paths[1] {
case "login":
provider.Login(req, w, claims)
provider.Login(req, w, session)
case "logout":
provider.Logout(req, w, claims)
provider.Logout(req, w, session)
case "register":
provider.Register(req, w, claims)
provider.Register(req, w, session)
case "callback":
provider.Callback(req, w, claims)
provider.Callback(req, w, session)
default:
provider.ServeHTTP(req, w, claims)
provider.ServeHTTP(req, w, session)
}
return
}
Expand All @@ -53,17 +49,29 @@ func (serveMux *serveMux) ServeHTTP(w http.ResponseWriter, req *http.Request) {
switch paths[0] {
case "login":
// render login page
serveMux.Auth.Render.Execute("auth/login", serveMux, req, w)
serveMux.Auth.Render.Execute("auth/login", session, req, w)
return
case "logout":
// destroy login session
return
case "register":
// render register page
serveMux.Auth.Render.Execute("auth/register", serveMux, req, w)
serveMux.Auth.Render.Execute("auth/register", session, req, w)
return
}
}

http.NotFound(w, req)
}

// Session session
type Session struct {
*Auth
*Claims
Params map[string]interface{}
}

// AuthURL generate URL for auth
func (session *Session) AuthURL(pth string) string {
return path.Join(session.Auth.Prefix, pth)
}
20 changes: 10 additions & 10 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func New() *DatabaseProvider {
// DatabaseProvider provide login with database method
type DatabaseProvider struct {
Auth *auth.Auth
Authorize func(request *http.Request, writer http.ResponseWriter) (interface{}, error)
Authorize func(request *http.Request, writer http.ResponseWriter, session *auth.Session) (interface{}, error)
}

// GetName return provider name
Expand All @@ -30,7 +30,7 @@ func (provider *DatabaseProvider) ConfigAuth(Auth *auth.Auth) {
provider.Auth = Auth

if provider.Authorize == nil {
provider.Authorize = func(request *http.Request, writer http.ResponseWriter) (interface{}, error) {
provider.Authorize = func(request *http.Request, writer http.ResponseWriter, session *auth.Session) (interface{}, error) {
var (
authInfo auth_identity.Basic
tx = Auth.GetDB(request)
Expand All @@ -53,24 +53,24 @@ func (provider *DatabaseProvider) ConfigAuth(Auth *auth.Auth) {
}

// Login implemented login with database provider
func (provider DatabaseProvider) Login(request *http.Request, writer http.ResponseWriter, claims *auth.Claims) {
provider.Auth.LoginHandler(request, writer, provider.Authorize)
func (provider DatabaseProvider) Login(request *http.Request, writer http.ResponseWriter, session *auth.Session) {
provider.Auth.LoginHandler(request, writer, session, provider.Authorize)
}

// Logout implemented logout with database provider
func (provider DatabaseProvider) Logout(request *http.Request, writer http.ResponseWriter, claims *auth.Claims) {
provider.Auth.LogoutHandler(request, writer, nil, claims)
func (provider DatabaseProvider) Logout(request *http.Request, writer http.ResponseWriter, session *auth.Session) {
provider.Auth.LogoutHandler(request, writer, nil, session)
}

// Register implemented register with database provider
func (provider DatabaseProvider) Register(request *http.Request, writer http.ResponseWriter, claims *auth.Claims) {
provider.Auth.RegisterHandler(request, writer, nil, claims)
func (provider DatabaseProvider) Register(request *http.Request, writer http.ResponseWriter, session *auth.Session) {
provider.Auth.RegisterHandler(request, writer, nil, session)
}

// Callback implement Callback with database provider
func (provider DatabaseProvider) Callback(*http.Request, http.ResponseWriter, *auth.Claims) {
func (provider DatabaseProvider) Callback(req *http.Request, writer http.ResponseWriter, session *auth.Session) {
}

// ServeHTTP implement ServeHTTP with database provider
func (provider DatabaseProvider) ServeHTTP(*http.Request, http.ResponseWriter, *auth.Claims) {
func (provider DatabaseProvider) ServeHTTP(req *http.Request, writer http.ResponseWriter, session *auth.Session) {
}
15 changes: 11 additions & 4 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ import (
)

// DefaultLoginHandler default behaviour after logged in
var DefaultLoginHandler = func(req *http.Request, w http.ResponseWriter, authorize func(*http.Request, http.ResponseWriter) (interface{}, error)) {
currentUser, err := authorize(req, w)
fmt.Println(currentUser)
fmt.Println(err)
var DefaultLoginHandler = func(req *http.Request, w http.ResponseWriter, session *Session, authorize func(*http.Request, http.ResponseWriter, *Session) (interface{}, error)) {
currentUser, err := authorize(req, w, session)
if err == nil {
if currentUser != nil {
// write cookie, json
} else {
}
}

fmt.Println("cccc")
session.Auth.Config.Render.Execute("auth/login", session, req, w)
}
10 changes: 5 additions & 5 deletions oauth/twitter/twitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ func (TwitterProvider) ConfigAuth(*auth.Auth) {
}

// Login implemented login with twitter provider
func (TwitterProvider) Login(request *http.Request, writer http.ResponseWriter, claims *auth.Claims) {
func (TwitterProvider) Login(request *http.Request, writer http.ResponseWriter, session *auth.Session) {
}

// Logout implemented logout with twitter provider
func (TwitterProvider) Logout(request *http.Request, writer http.ResponseWriter, claims *auth.Claims) {
func (TwitterProvider) Logout(request *http.Request, writer http.ResponseWriter, session *auth.Session) {
}

// Register implemented register with twitter provider
func (TwitterProvider) Register(request *http.Request, writer http.ResponseWriter, claims *auth.Claims) {
func (TwitterProvider) Register(request *http.Request, writer http.ResponseWriter, session *auth.Session) {
}

// Callback implement Callback with twitter provider
func (TwitterProvider) Callback(*http.Request, http.ResponseWriter, *auth.Claims) {
func (TwitterProvider) Callback(*http.Request, http.ResponseWriter, *auth.Session) {
}

// ServeHTTP implement ServeHTTP with twitter provider
func (TwitterProvider) ServeHTTP(*http.Request, http.ResponseWriter, *auth.Claims) {
func (TwitterProvider) ServeHTTP(*http.Request, http.ResponseWriter, *auth.Session) {
}
10 changes: 5 additions & 5 deletions phone/phone.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ func (PhoneProvider) ConfigAuth(*auth.Auth) {
}

// Login implemented login with phone provider
func (PhoneProvider) Login(request *http.Request, writer http.ResponseWriter, claims *auth.Claims) {
func (PhoneProvider) Login(request *http.Request, writer http.ResponseWriter, session *auth.Session) {
}

// Logout implemented logout with phone provider
func (PhoneProvider) Logout(request *http.Request, writer http.ResponseWriter, claims *auth.Claims) {
func (PhoneProvider) Logout(request *http.Request, writer http.ResponseWriter, session *auth.Session) {
}

// Register implemented register with phone provider
func (PhoneProvider) Register(request *http.Request, writer http.ResponseWriter, claims *auth.Claims) {
func (PhoneProvider) Register(request *http.Request, writer http.ResponseWriter, session *auth.Session) {
}

// Callback implement Callback with phone provider
func (PhoneProvider) Callback(*http.Request, http.ResponseWriter, *auth.Claims) {
func (PhoneProvider) Callback(*http.Request, http.ResponseWriter, *auth.Session) {
}

// ServeHTTP implement ServeHTTP with phone provider
func (PhoneProvider) ServeHTTP(*http.Request, http.ResponseWriter, *auth.Claims) {
func (PhoneProvider) ServeHTTP(*http.Request, http.ResponseWriter, *auth.Session) {
}
10 changes: 5 additions & 5 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ type Provider interface {
GetName() string
ConfigAuth(*Auth)

Login(*http.Request, http.ResponseWriter, *Claims)
Logout(*http.Request, http.ResponseWriter, *Claims)
Register(*http.Request, http.ResponseWriter, *Claims)
Callback(*http.Request, http.ResponseWriter, *Claims)
ServeHTTP(*http.Request, http.ResponseWriter, *Claims)
Login(*http.Request, http.ResponseWriter, *Session)
Logout(*http.Request, http.ResponseWriter, *Session)
Register(*http.Request, http.ResponseWriter, *Session)
Callback(*http.Request, http.ResponseWriter, *Session)
ServeHTTP(*http.Request, http.ResponseWriter, *Session)
}

0 comments on commit 522290b

Please sign in to comment.