Code from http://railscasts.com/episodes/250-authentication-from-scratch