Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void login(final String email, final String password, final ResponseHandler handler) {
- Request getPublicKeyRequest = new Request("getPublicKey");
- HttpClient client = new HttpClient();
- client.makeRequestWithoutAuth(getPublicKeyRequest, new ResponseHandler() {
- @Override
- public void onFailure(Response response) {
- }
- @Override
- public void onSuccess(Response response) {
- String publicKey = "";
- try {
- JSONObject pubKeyJSON = new JSONObject(response.getRawResponse());
- publicKey = pubKeyJSON.getString("key");
- } catch (JSONException e) {
- e.printStackTrace();
- }
- Request loginRequest = new Request("auth", Request.HttpMethod.POST);
- JSONObject credJSON = new JSONObject();
- try {
- credJSON.put("email", email);
- credJSON.put("pass", password);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- String credentialsData = credJSON.toString();
- String credentials = Encryption.encryptData(credentialsData, publicKey);
- loginRequest
- .addParameter(new Parameter("data", credentials));
- HttpClient client = new HttpClient();
- client.makeRequestWithoutAuth(loginRequest, new ResponseHandler() {
- @Override
- public void onFailure(Response response) {
- }
- @Override
- public void onSuccess(Response r) {
- String jsonResponseString = r.getRawResponse();
- Log.d("resp", jsonResponseString);
- try {
- JSONObject responseJSONObject = new JSONObject(jsonResponseString);
- if (responseJSONObject.getString("status").equals("ok")) {
- SharedPreferences sp = mContext.getSharedPreferences(AUTH_PREFS, mContext.MODE_PRIVATE);
- SharedPreferences.Editor ed = sp.edit();
- ed.putString(PREF_TOKEN, responseJSONObject.getString("token"));
- ed.putInt(PREF_UID, responseJSONObject.getInt("uid"));
- ed.commit();
- }
- handler.onSuccess(null);
- } catch (JSONException e) {
- handler.onFailure(null);
- e.printStackTrace();
- }
- }
- });
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement