Advertisement
Guest User

Untitled

a guest
Jul 24th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.85 KB | None | 0 0
  1. public void login(final String email, final String password, final ResponseHandler handler) {
  2.         Request getPublicKeyRequest = new Request("getPublicKey");
  3.         HttpClient client = new HttpClient();
  4.         client.makeRequestWithoutAuth(getPublicKeyRequest, new ResponseHandler() {
  5.             @Override
  6.             public void onFailure(Response response) {
  7.  
  8.             }
  9.  
  10.             @Override
  11.             public void onSuccess(Response response) {
  12.                 String publicKey = "";
  13.                 try {
  14.                     JSONObject pubKeyJSON = new JSONObject(response.getRawResponse());
  15.                     publicKey = pubKeyJSON.getString("key");
  16.                 } catch (JSONException e) {
  17.                     e.printStackTrace();
  18.                 }
  19.  
  20.                 Request loginRequest = new Request("auth", Request.HttpMethod.POST);
  21.  
  22.                 JSONObject credJSON = new JSONObject();
  23.  
  24.                 try {
  25.                     credJSON.put("email", email);
  26.                     credJSON.put("pass", password);
  27.                 } catch (JSONException e) {
  28.                     e.printStackTrace();
  29.                 }
  30.  
  31.                 String credentialsData = credJSON.toString();
  32.                 String credentials = Encryption.encryptData(credentialsData, publicKey);
  33.  
  34.                 loginRequest
  35.                         .addParameter(new Parameter("data", credentials));
  36.  
  37.                 HttpClient client = new HttpClient();
  38.                 client.makeRequestWithoutAuth(loginRequest, new ResponseHandler() {
  39.                     @Override
  40.                     public void onFailure(Response response) {
  41.  
  42.                     }
  43.  
  44.                     @Override
  45.                     public void onSuccess(Response r) {
  46.                         String jsonResponseString = r.getRawResponse();
  47.                         Log.d("resp", jsonResponseString);
  48.                         try {
  49.                             JSONObject responseJSONObject = new JSONObject(jsonResponseString);
  50.  
  51.                             if (responseJSONObject.getString("status").equals("ok")) {
  52.                                 SharedPreferences sp = mContext.getSharedPreferences(AUTH_PREFS, mContext.MODE_PRIVATE);
  53.                                 SharedPreferences.Editor ed = sp.edit();
  54.                                 ed.putString(PREF_TOKEN, responseJSONObject.getString("token"));
  55.                                 ed.putInt(PREF_UID, responseJSONObject.getInt("uid"));
  56.                                 ed.commit();
  57.                             }
  58.  
  59.                             handler.onSuccess(null);
  60.                         } catch (JSONException e) {
  61.                             handler.onFailure(null);
  62.                             e.printStackTrace();
  63.                         }
  64.  
  65.                     }
  66.                 });
  67.             }
  68.         });
  69.  
  70.  
  71.  
  72.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement