It is done by using http://railscasts.com/episodes/250-authentication-from-scratch