diff --git a/README.md b/README.md index beb55d7..e710577 100644 --- a/README.md +++ b/README.md @@ -38,10 +38,10 @@ Your user type should meet the login.User interface: Logout() // Return the unique identifier of this user object - UniqueId() interface{} + UniqueID() interface{} // Populate this user object with values - GetById(id interface{}) error + GetByID(id interface{}) error } ``` diff --git a/example/auth_example.go b/example/auth_example.go index f916196..9ea0b14 100644 --- a/example/auth_example.go +++ b/example/auth_example.go @@ -6,11 +6,11 @@ package main import ( "database/sql" "github.com/codegangsta/martini" + "github.com/coopernurse/gorp" "github.com/martini-contrib/binding" "github.com/martini-contrib/render" "github.com/martini-contrib/sessionauth" "github.com/martini-contrib/sessions" - "github.com/coopernurse/gorp" _ "github.com/mattn/go-sqlite3" "log" "net/http" @@ -32,7 +32,7 @@ func initDb() *gorp.DbMap { } dbmap := &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}} - dbmap.AddTableWithName(MyUserModel{}, "users").SetKeys(true, "Id") + dbmap.AddTableWithName(MyUserModel{}, "users").SetKeys(true, "ID") err = dbmap.CreateTablesIfNotExists() if err != nil { log.Fatalln("Could not build tables", err) @@ -60,7 +60,7 @@ func main() { }) m.Use(sessions.Sessions("my_session", store)) m.Use(sessionauth.SessionUser(GenerateAnonymousUser)) - sessionauth.RedirectUrl = "/new-login" + sessionauth.RedirectURL = "/new-login" sessionauth.RedirectParam = "new-next" m.Get("/", func(r render.Render) { @@ -77,7 +77,7 @@ func main() { user := MyUserModel{} err := dbmap.SelectOne(&user, "SELECT * FROM users WHERE username = $1 and password = $2", postedUser.Username, postedUser.Password) if err != nil { - r.Redirect(sessionauth.RedirectUrl) + r.Redirect(sessionauth.RedirectURL) return } else { err := sessionauth.AuthenticateSession(session, &user) diff --git a/example/user.go b/example/user.go index fd41bf5..c06a505 100644 --- a/example/user.go +++ b/example/user.go @@ -6,7 +6,7 @@ import ( // MyUserModel can be any struct that represents a user in my system type MyUserModel struct { - Id int64 `form:"id" db:"id"` + ID int64 `form:"id" db:"id"` Username string `form:"name" db:"username"` Password string `form:"password" db:"password"` authenticated bool `form:"-" db:"-"` @@ -39,13 +39,13 @@ func (u *MyUserModel) IsAuthenticated() bool { return u.authenticated } -func (u *MyUserModel) UniqueId() interface{} { - return u.Id +func (u *MyUserModel) UniqueID() interface{} { + return u.ID } -// GetById will populate a user object from a database model with +// GetByID will populate a user object from a database model with // a matching id. -func (u *MyUserModel) GetById(id interface{}) error { +func (u *MyUserModel) GetByID(id interface{}) error { err := dbmap.SelectOne(u, "SELECT * FROM users WHERE id = $1", id) if err != nil { return err diff --git a/sessionauth.go b/sessionauth.go index 932c3f5..9a6f638 100644 --- a/sessionauth.go +++ b/sessionauth.go @@ -1,4 +1,4 @@ -// Package login is a middleware for Martini that provides a simple way to track user sessions +// Package sessionauth is a middleware for Martini that provides a simple way to track user sessions // in on a website. Please see https://github.com/codegangsta/martini-contrib/blob/master/sessionauth/README.md // for a more detailed description of the package. package sessionauth @@ -15,16 +15,16 @@ import ( // These are the default configuration values for this package. They // can be set at anytime, probably during the initial setup of Martini. var ( - // RedirectUrl should be the relative URL for your login route - RedirectUrl string = "/login" + // RedirectURL should be the relative URL for your login route + RedirectURL = "/login" // RedirectParam is the query string parameter that will be set // with the page the user was trying to visit before they were // intercepted. - RedirectParam string = "next" + RedirectParam = "next" // SessionKey is the key containing the unique ID in your session - SessionKey string = "AUTHUNIQUEID" + SessionKey = "AUTHUNIQUEID" ) // User defines all the functions necessary to work with the user's authentication. @@ -41,10 +41,10 @@ type User interface { Logout() // Return the unique identifier of this user object - UniqueId() interface{} + UniqueID() interface{} // Populate this user object with values - GetById(id interface{}) error + GetByID(id interface{}) error } // SessionUser will try to read a unique user ID out of the session. Then it tries @@ -55,11 +55,11 @@ type User interface { // user type. func SessionUser(newUser func() User) martini.Handler { return func(s sessions.Session, c martini.Context, l *log.Logger) { - userId := s.Get(SessionKey) + userID := s.Get(SessionKey) user := newUser() - if userId != nil { - err := user.GetById(userId) + if userID != nil { + err := user.GetByID(userID) if err != nil { l.Printf("Login Error: %v\n", err) } else { @@ -91,7 +91,7 @@ func Logout(s sessions.Session, user User) { // set to the attempted URL. func LoginRequired(r render.Render, user User, req *http.Request) { if user.IsAuthenticated() == false { - path := fmt.Sprintf("%s?%s=%s", RedirectUrl, RedirectParam, req.URL.Path) + path := fmt.Sprintf("%s?%s=%s", RedirectURL, RedirectParam, req.URL.Path) r.Redirect(path, 302) } } @@ -99,6 +99,6 @@ func LoginRequired(r render.Render, user User, req *http.Request) { // UpdateUser updates the User object stored in the session. This is useful incase a change // is made to the user model that needs to persist across requests. func UpdateUser(s sessions.Session, user User) error { - s.Set(SessionKey, user.UniqueId()) + s.Set(SessionKey, user.UniqueID()) return nil } diff --git a/sessionauth_test.go b/sessionauth_test.go index 813f598..f99b4b9 100644 --- a/sessionauth_test.go +++ b/sessionauth_test.go @@ -10,7 +10,7 @@ import ( ) type TestUser struct { - Id int `json:"id"` + ID int `json:"id"` Name string `json:"name"` Age int `json:"age"` authenticated bool `json:"-"` @@ -28,12 +28,12 @@ func (u *TestUser) Logout() { u.authenticated = false } -func (u *TestUser) UniqueId() interface{} { - return u.Id +func (u *TestUser) UniqueID() interface{} { + return u.ID } -func (u *TestUser) GetById(id interface{}) error { - u.Id = id.(int) +func (u *TestUser) GetByID(id interface{}) error { + u.ID = id.(int) u.Name = "My Test User" u.Age = 42