Search code examples
ruby-on-railsauthenticationdevisetitanium-mobile

Devise based user authentication programmatically


I am making application for user login based authentication programmatically that is using a devise gem but couldn't succeed. I know that for devise based login, I need to pass user email and password. Do I need any other parameter to pass. On browser, I am able to login with user email and password but programmatically I am getting Unauthorized access.

Although the program is in titanium but I am thinking that it doesn't matter and what matters is what parameters you are passing to the rails server.

My console output 1 without adding user params in titanium:

Started POST "/users/sign_in" for 192.168.0.187 at 2012-12-21 12:57:32 +0530
Processing by Devise::SessionsController#create as URL_ENCODED_FORM
  Parameters: {"password"=>"[FILTERED]", "email"=>"Gjpu "}
Completed 401 Unauthorized in 1ms

My console output 2 after adding user params in titanium:

Started POST "/users/sign_in" for 192.168.0.187 at 2012-12-21 13:03:19 +0530
Processing by Devise::SessionsController#create as URL_ENCODED_FORM
  Parameters: {"user"=>"{password=6f278077d841b3385e06bfa99acb374f, email=gjmwtp
gj}"}
Completed 401 Unauthorized in 1ms

My titanium code to hit the rails server -

loginBtn.addEventListener('click',function(e)
{
    if (username.value != '' && password.value != '')
    {
        loginReq.open("POST","http://192.168.0.187:3000/users/sign_in");

        var params = {user: { email: username.value , password: Ti.Utils.md5HexDigest(password.value)}};

        //var params=JSON.parse(myparams);
        loginReq.send(params);
    }
    else
    {
        alert("Email/Password are required");
    }
});

Solution

  • I am now able to authenticate using the custom devise session controller and adding two lines in the create method.

    @user = User.find_by_email params[:email]
    sign_in_and_redirect @user, :event => :authentication
    

    The trick worked. Thanks!