-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add auth.BasicFunc #7
Conversation
Added documentation. Should be good to merge. |
I'm still not sure how to inject something into the middleware e.g. a DB object. |
@attilaolah @codegangsta could someone provide an example please? |
@schickling just make it available to the closure you pass in to db := dbSetup(…)
m.Use(auth.BasicFunc(func(username, password string) bool {
user := db.Get(username)
return user != nil && auth.SecureCompare(password, user.Password)
}) |
Is there another way by using the dependency injection inside via the |
Of course it doesn't have to be global. Currently type DBAuth struct {
db Database
}
func (a *DBAuth) auth(username, password string) bool {
user := a.db.get(username)
return user != nil && auth.SecureCompare(user.Password, password)
}
func main() {
// …
m.Use(auth.BasicFunc((&DBAuth{
db: dbSetup(),
}).auth))
} This has basically the same effect as using a closure, only it uses a struct instead. |
Thanks for the solution! Are there any plans to add DI to this project? |
There were some discussions earlier, but I'm not sure anyone's too keen on implementing this. See #18. I know the DI is cool but I still prefer good old closures or struct methods. But if you want to implement it, feel free to do so and send a PR. |
This only includes the implementation and some tests. Give me a moment and I'll update the README.