{"id":410,"date":"2009-11-04T11:24:25","date_gmt":"2009-11-04T13:24:25","guid":{"rendered":"http:\/\/blog.plataformatec.com.br\/?p=410"},"modified":"2009-11-15T23:04:13","modified_gmt":"2009-11-16T01:04:13","slug":"devise-authentication-for-lazy-programmers","status":"publish","type":"post","link":"https:\/\/blog.plataformatec.com.br\/2009\/11\/devise-authentication-for-lazy-programmers\/","title":{"rendered":"Devise: authentication for lazy programmers"},"content":{"rendered":"

It has been a couple weeks since we first bloged<\/a> about Devise<\/a>. At that time, we released version 0.1 and now, after some great feedback, some enhancements and a few bugs fixes, we reached Devise 0.4. So, what changed since then?<\/p>\n

I’m lazy, you’re lazy<\/h3>\n

Devise now comes with generators, so adding up authentication to your app is even easier and quicker to do. First, let’s install Devise<\/a> if you haven’t yet:<\/p>\n

\r\ngem sources -a http:\/\/gemcutter.org\/\r\ngem install devise\r\n<\/pre>\n

And let’s add it to your environment, all together with warden<\/a>:<\/p>\n

\r\nconfig.gem \"warden\", :version => \"0.5.1\"\r\nconfig.gem \"devise\", :version => \"0.4.1\"\r\n<\/pre>\n

After setting the gem up, the first generator can be invoked:<\/p>\n

\r\nscript\/generate devise_install\r\n<\/pre>\n

And it simply places an initializer at config\/initializers\/devise.rb. You can check there all devise configuration options, so the initializer fits well as documentation tool too. Some of the new things you can configure since 0.1 release is the :confirm_within period (the time the user can access the site even without confirming his account) and :remember_for period (the time the remember token is valid). <\/p>\n

After we configured Devise<\/a>, we are able create our models. And how hard can that be if we have a generator? So just do:<\/p>\n

\r\nscript\/generate devise User\r\n<\/pre>\n

It’s going to create a model User, add map.devise_for :users in routes.rb and a migration file. And before we proceed, we just need to configure default_url_options for ActionMailer in config\/environments\/development.rb (the config below is for development, be sure to properly set them for test and production too):<\/p>\n

\r\nconfig.action_mailer.default_url_options = { :host => 'localhost:3000' }\r\n<\/pre>\n

And have at least a route named root in our config\/routes.rb (by default, devise will look for user_root_path, if none is defined, is uses root_path):<\/p>\n

\r\nmap.root :controller => \"your_controller\"\r\n<\/pre>\n

Now we just need to run migrations and we will be able to create our first user in the console:<\/p>\n

\r\nUser.create!(:email => \"your@email.com\", :password => \"secret\")\r\n<\/pre>\n

Now just start your web server and you will be able to sign your user in at \/users\/sign_in, request for another confirmation code to be sent, request a code to reset your password and so forth.<\/p>\n

Just remember that Devise does not say anything about the sign up process<\/span>, that’s why you have to create your users in console.<\/p>\n

Customization<\/h3>\n

We also know that you want to customize your views, to use something like Formtastic<\/a> instead of the default markup. Well, guess who is here to help you?<\/p>\n

Generators! The command below will make a copy of all devise views to your application, including locale files, for flash messages configuration:<\/p>\n

\r\nscript\/generate devise_views\r\n<\/pre>\n

Laziness for all!<\/h3>\n

We know that you are lazy and not just with ActiveRecord. You want to be lazy with Datamapper, Mongomapper, Couchrest… This is why we worked on making Devise a little bit more agnostic<\/a>, all your ORM has to do is have an API similar to ActiveRecord (finder and callbacks mainly).<\/p>\n

Besides, if you already travelled a bit on Warden world, you will see that there is a lot more than Devise, including some strategies for OAuth<\/a>. So we are also working on making Devise compatible with such new strategies as well, that’s why you can already see a config.warden hook on the initializer.<\/p>\n

Deprecations<\/h3>\n

Since things are getting really easy, we need to ask something back from you. Just watch out for to deprecations in Devise<\/a> 0.4.0:<\/p>\n

1) :authenticable is a typo, so we fixed that renaming it to :authenticatable. If you used Devise 0.3.x, you could see some deprecation warnings. However, in Devise 0.4.0 such warnings were removed. Tip: be sure to check your migration!<\/p>\n

2) We had a notifier inside Devise<\/a> called Notifier. We decided to follow Clearance<\/a> convention and rename it to DeviseMailer. You will need to change your views from notifier to devise_mailer and your locale yml from notifier to mailer.<\/p>\n

We hope you enjoy Devise<\/a> as much as we do! And, as previously, we also have an example app in Github<\/a> to help you get started too.<\/p>\n","protected":false},"excerpt":{"rendered":"

It has been a couple weeks since we first bloged about Devise. At that time, we released version 0.1 and now, after some great feedback, some enhancements and a few bugs fixes, we reached Devise 0.4. So, what changed since then? I’m lazy, you’re lazy Devise now comes with generators, so adding up authentication to … \u00bb<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[1],"tags":[37,36,10,46,7],"aioseo_notices":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/410"}],"collection":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/comments?post=410"}],"version-history":[{"count":12,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/410\/revisions"}],"predecessor-version":[{"id":428,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/410\/revisions\/428"}],"wp:attachment":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/media?parent=410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/categories?post=410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/tags?post=410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}