簡體   English   中英

如何在Android應用程序中使用Ruby API設置登錄名?

[英]How to setup login with Ruby API in Android application?

我正在嘗試使用Ruby on Rails應用程序中的api創建一個android應用程序。 我想知道如何將用戶登錄信息保留在android應用程序中。 而且,一旦用戶登錄,設備(移動設備)便可以使用Ruby應用程序訪問數據庫。

在Ruby應用程序中,用戶帳戶方法是通過Devise gem實現的。

當我使用電子郵件和密碼請求帖子時,輸出將收集在“緩沖區”中。 該緩沖區具有數據(當用戶輸入正確的信息時,即密碼和電子郵件地址,輸出是令牌或類似字符串的“ ok”。)但是我不知道在這種情況下我必須保留哪種類型的信息。

在此之后,我有點迷失下一步的工作。 在Rails應用程序中,“ current_user”存儲在會話中,並且每當用戶瀏覽網站時,除非注銷,否則此current_user存在。

但是我在這里使用API​​,我不知道Android App如何在此處保留用戶信息。

Rails中的用戶模型具有以下屬性:電子郵件,名字,姓氏等。

URL url = new URL("https://www.sitename.com/api/v1/sessions");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
urlConnection.setReadTimeout(10000);
urlConnection.setConnectTimeout(15000);
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);

String urlParameters = "email=emailaddress@gmail.com&password=123123123";
DataOutputStream dStream = new DataOutputStream(urlConnection.getOutputStream());
dStream.writeBytes(urlParameters);
dStream.flush();
dStream.close();

InputStream in = urlConnection.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
    buffer.append(line).append("\n");
}

reader.close();
return buffer.toString();
//I fill something needs to be done here.

然后,我們如何告知Ruby應用程序用戶已登錄? 在這種情況下,我必須制作哪種類型的API?

以下是用於API(會話)的ruby方法。 訪問地址是“ / api / v1 / sessions”。

module Beta
    class Currency < Grape::API
        version 'v1', using: :path
        format :json #if we put like this, this page would have a json formatted output.

        rescue_from :all

        #http://localhost:3000/api/v1/beta/exchange?amount=4.5#
        #api/v1/+resource name/+method_name

        error_formatter :json, lambda { |message, backtrace, options, env|
            {
                status: 'failed',
                message: message,
                error_code: 123
            }
        }

       resource :sessions do
       desc "Authenticate user and return user object / access token"

       params do
         requires :email, type: String, desc: "User email"
         requires :password, type: String, desc: "User Password"
       end

       post do
         email = params[:email]
         password = params[:password]

         if email.nil? or password.nil?
           error!({error_code: 404, error_message: "Invalid Email or Password.type0"},401)
           return
         end

         user = User.where(email: email.downcase).first
         puts "되고 있나?"

         if user.nil?
           error!({error_code: 404, error_message: "Invalid Email or Password.type1"},401)
           return
         end

         if !user.valid_password?(password)
           error!({error_code: 404, error_message: "Invalid Email or Password.type2"},401)
           return
         else
           #user.ensure_authentication_token
           #user.save
           #{status: 'ok', token: user.authentication_token}.to_json
          {status: 'ok', token: "logged_in#{user.id}#{user.email}"}.to_json
         end
       end


    #curl -H "Content-Type: application/json" -X POST -d '{"email":"ddd@gmail.com,"password":"123123123"}' http://localhost:3000/api/v1/sessions

       desc "Destroy the access token"
       params do
         requires :access_token, type: String, desc: "User Access Token"
       end
       delete ':access_token' do
         access_token = params[:access_token]
         user = User.where(authentication_token: access_token).first
         if user.nil?
           error!({error_code: 404, error_message: "Invalid access token."},401)
           return
         else
           user.reset_authentication_token
           #{status: 'ok'}
         end
       end
     end

..

請給我任何預感! 我非常期待聽到像您這樣的專家的任何評論! (:

所有會話所做的就是訪問您瀏覽器上的cookie並存儲多達4kb的數據。 看來您的API方法的工作方式與Rails通常的工作方式相同。 您應該查看Rails指南,了解會話的實際工作方式http://guides.rubyonrails.org/security.html#what-are-sessions-questionmark

檢查有關會話存儲的部分。 然后,我將確定您的android應用程序是否能夠使用cookie保留數據,如果是這樣,那么您應該可以通過會話訪問它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM