fingernails in oatmeal

Lisp has all the visual appeal of oatmeal with fingernail clippings mixed in.

— Larry Wall

Authors

The Unobtrusive Login System in Rails - Part One

In my previous post I hinted at how an unobtrusive javascript login system might look. I’ve put together the following example to expand on the idea.

Here are the steps that we’ll cover:

  1. Create a working HTML-based login system
  2. Enhance the login links to show the login form in a lightbox
  3. Add error handling to lightbox login form
  4. Discuss some of the problems with a completely unobtrusive approach

I’ll be using the authlogic gem for authentication, jQuery for awesomeness, and facebox for a lightbox.

Simple HTML login

Rather than building an authentication system from scratch I’ve forked Ben Johnson’s authogic_example.

At this point we have a working app that will let us register and login along with all the javascript libraries we’ll need.

Enhancement

First, we need to change the UserSessions#new

What is happening here is that we’re choosing not to render the layout if the request is an AJAX request.

The next step is to add an identifying attribute to the login link to create a lightbox. We’ll go ahead and follow the Facebox idiom of adding rel="facebox":

Now we can move to the Javascript.

Facebox makes this super simple. However, in the next part we’ll have to take more granular control of displaying the lightbox in order to deal with AJAX-ifying the login form.

Behold!

Error handling

Now we have an awesome lightbox but when we submit bad data then the page changes and we end up at the regular old Login page. We need to add the appropriate javascript and change the create action to handle bad stuff.

The final step is to call the function we just defined in the success callback so we always attach this behavior when the form is loaded.

This might look complex but here is what is happening:

Now when we try to login with bad credentials…

If you’d like to see all of this put together then you can checkout the part_one branch (git checkout -b part_one origin/part_one).

Problems

I am not preaching the One True Way™, rather, I am trying to find a better way. Here’s some disclosure about the problems I see with this approach:

Next steps

In part two we’ll go over how to prompt a user with the lightbox for credentials when she clicks a link requiring an authenticated session.

Adam
  1. cheap-papers reblogged this from fingernailsinoatmeal
  2. free-bets reblogged this from fingernailsinoatmeal
  3. sherika-wizar reblogged this from fingernailsinoatmeal
  4. denis-reusch reblogged this from fingernailsinoatmeal
  5. gerald-dolfay reblogged this from fingernailsinoatmeal
  6. colby-lango reblogged this from fingernailsinoatmeal
  7. valentin-imrie reblogged this from fingernailsinoatmeal
  8. emmaline-tsakonas reblogged this from fingernailsinoatmeal
  9. keven-zickefoose reblogged this from fingernailsinoatmeal
  10. chance-delaroca reblogged this from fingernailsinoatmeal
  11. henrietta-grable reblogged this from fingernailsinoatmeal
  12. raymond-denes reblogged this from fingernailsinoatmeal
  13. debbie-rivinius reblogged this from fingernailsinoatmeal
  14. donnie-vickroy reblogged this from fingernailsinoatmeal