3 Replies Latest reply on Apr 17, 2013 11:06 PM by Mohit Raheja

    RhoConnect authenticate method

         I have a webservice(using jsp) which returns a json response as under :

       

      {"message":[{"SessionId":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}],"type":"Response"}

       

       

      where my users already registered. I need to do login from rhomobile app through Rhoconnect; not directly from rhodes app. When I will open my application I will see an login page where i'll do login with my credential and and it will login and fetch the data from webservice to rhomobile app. The login section I need to do through Rhoconnect(where I saw the tutorial that need to do call from SyncEngine.login i.e call form Rhodes app and will call to Rhoconnect - application.rb - authenticate method). How to call the web service in rho connect's application.rb file and pass the session id back to rhodes application on successful login?

        • Re: RhoConnect authenticate method
          Kutir Mobility

          Hi Saurabh

           

          You can call SyncEngine.login in rhodes app as

           

            def do_login
            SyncEngine.login(@params['username'], @params['password'], (url_for :action => :login_callback) )
             end

           

          This will pass the username and password to authenticate method in rhoconnect application.rb file. In authenticate method, you can call JSP page using rest-client as

           

          def authenticate(username, password, session)
              resp = RestClient.post(:url => "mywebservice", :login => {:username => username, :password => password})
              result = JSON.parse(resp.body)['message']

            return result
          end

           

          In rhodes login_callback, you can take the response from params and use it

           

          Hope it helps

           

          Thanks

          Dhepthi

          Kutir Mobility

          • Re: RhoConnect authenticate method

            To clarify things up, understand that rhoconnect doesn't store http session id on its connection to any backend server. It just tries to make sure that a device connecting to rhoconnect is properly authenticated by calling the authenticate route to the backend server. But it can't store any session coming from the backend application.

            It is your rhodes application that needs to get a session directly from your backend. This means you have to directly login your rhodes app to your backend app by sending login request. This would allow your rhodes app to store a cookie session coming from your backend and use it on your succeeding http request. Remember your Rhoconnect server only functions as synchronization engine of your device local database and your backend database. It doesnt function as middleman in all of your request transaction. You should know what logic will send http request that requires to connect directly to your backend service and what logic requires synchronization via rhoconnect. In our rhodes app we did our login logic like these

             

            def sync_login_callback

                errCode = @params['error_code'].to_i

                if (errCode == 0)

                  # run sync if we were successful

                  #this is how to get rhoconnect session from local db

                  #res = Rho::RHO.get_user_db.execute_sql("select session from client_info")

                  #WebView.execute_js("console.log('res: #{res[0].inspect}')");

                  SyncEngine.dosync

                  return

                else

                  if errCode == Rho::RhoError::ERR_CUSTOMSYNCSERVER

                    @msg = @params['error_message']

                  end

             

                  if !@msg || @msg.length == 0

                    @msg = Rho::RhoError.new(errCode).message

                  end

                  #failed starting up offline sync service

                  WebView.execute_js("alert('Error occurred while starting offline sync service : #{@params['error_message']}');")

                end

              end

             

              def login_callback

                $cookies = @params['cookies']

                status = @params['status']

                @username = @params['username']

                @pwd = @params['password']

                if (@params['error_code'].to_i == 0 or status == "complete" or status == "ok")

                  Rho::RhoConfig.start_path = url_for(:action =>:home)

                  #store login info to runtime variable

                  $account = {"username" => @username, "password" => @pwd}

                  #run sync login if successful

                  SyncEngine.login(@username, @pwd, (url_for :action => :sync_login_callback) )

                  return

                end

                #login failed

              end

             

              def do_login

                clear_login

                jsonData = Rho::JSON.parse(get_json_data)

                if !blank?(jsonData['username'])  and jsonData['password']

                  @params[Global::PRE_CALLBACK] = "login_callback"

                  @params[Global::CB_PARAMS] = "username=" + jsonData['username'] + "&password=" + jsonData['password']

                  @params['url'] = "login"

                  async_post

                else

                  @msg = Rho::RhoError.err_message(Rho::RhoError::ERR_UNATHORIZED) unless @msg && @msg.length > 0

                  render :action => :login

                end

              end

            def async_post

                cookie = set_cookies

                headers = {"Content-Type" => "application/json","Accept" => "application/json", 'Cookie' => cookie}

                callback_param = set_callback_params

                Rho::AsyncHttp.post(

                :url => Global::SYNC_BASE_URL + @params['url'],

                :headers => headers,

                :body =>  body,

                :callback => @params[Global::PRE_CALLBACK],

                :callback_param => callback_param

                )

              end

             

            Once the rhodes application successfully login to our backend app, it then follows to login to rhoconnect to the get api token for synchronization purposes. Once your authenticated in your rhoconnect server, you have the privilege now to call SyncEngine.sync on any models that you have. Thats the purpose of Rhoconnect.

            • Re: RhoConnect authenticate method
              Mohit Raheja

              Hi Saurabh,

               

              Well i haven't seen such scenario till now. Authenticate method in application.rb file is used to login on rhoconnect. It always returns true or false depending on the scenarios. If you want to get the session id on login then you can by-pass the rhoconnect login & you can call the login web service in query method of source adapter and then you will easily get the value of session id from the local database of the model.

               

              By passing the rhoconnect login:

              SyncEngine.login(username,password)

               

              application.rb file:

                class << self

                  def authenticate(username,password,session)

                        true

                   end

               

              authenticate method is always used to login into your rhoconnect server. You can login into your backend as well in this function using Rest API but you will not be able to return a value from here to your rhodes application.

               

              Thanks

              Mohit Raheja