Rod McLaughlin
More
|
|
|
Thanks, Sean!
Now, where was I?
Oh yeah, testing...
Authentication
gist.github.com/wycats/merb-plugins/tree/ed3a3cf597477b8a0048922978ec6a1eda591168/merb_auth/slices/merb_auth_password_slice/README.textile
tells you how to set up authentication - except, as usual, Merb has changed since
it was written.
It says put
dependency ‘merb_auth_password_slice’
in init.rb, but what you really need to do is put
dependency 'merb-auth-slice-password'
in dependencies.rb.
Then it says
'If needed, configure which slices to load and in which order
Merb::Plugins.config[:merb_slices] = { :queue => [“MerbAuthPasswordSlice”, ...] }'
...but how do you know if it's needed?
Compared to this, acts_as_authenticated for Ruby on Rails was a breeze - see
robmayhew.com/todo-list-using-rails-201-with-‘act-as-authenticated’/.
Next:
'Setup your application to use MerbAuthPasswordSlice in an after_app_loads block'
The instructions only make sense if you already know how to do it,
and therefore don't need the instructions.
Bradly Feeley at
bradlyfeeley.com
told me in idiot-proof steps how to set up
Merb Authentication. I reproduce it below:
Lets start at the beginning and create an app
$ merb-gen app authentication_app
$ cd authentication_app
By default merb-gen app will create a user model for you in app/models/user.rb. The model is there, but we still need to create the table in the database and add a user to authenticate with. Lets do that now.
$ rake db:auto_migrate
To create our first user we can drop into Merb's irb console mode with merb -i. Like Rails' script/console, merb -i gives you access to your models and other classes.
$ merb -i
u = User.new
u.login = 'joe'
u.password = u.password_confirmation = 'password'
u.save
exit
In order to test authentication we need something to protect.
Any controller would work, but lets create a resource.
$ merb-gen resource secret
We will need to add a route to the resource also
# router.rb
Merb::Router.prepare do
resources :secrets
...
end
To protect a controller with authentication
we can use the before filter ensure_authenticated
# app/controllers/secrets.rb
class Secrets < Application
before :ensure_authenticated
...
end
Everything should now be setup in our app. Lets give it a run through.
First you need to start you merb server. Make sure you are in your merb app’s root directory.
$ merb
Now you can test access to your resource is denied before authentication.
http://localhost:4000/secrets
Login in and try to access the recourse again.
http://localhost:4000/login
http://localhost:4000/secrets
You should now be able to access the resource. Awesome.
This worked immediately.
Thanks Bradly!
I now have a Merb app which forces you to log in in order to access, edit or add articles, which you do with a wysiwyg html editor.
