Advertisement
Guest User

Untitled

a guest
Aug 16th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1. require_once 'include/DB_Functions.php';
  2. $db = new DB_Functions();
  3.  
  4. // json response array
  5. $response = array("error" => FALSE);
  6.  
  7. if (isset($_POST['email']) && isset($_POST['password'])) {
  8.  
  9. // receiving the post params
  10. $email = $_POST['email'];
  11. $password = $_POST['password'];
  12.  
  13. // get the user by email and password
  14. $user = $db->getUserByEmailAndPassword($email, $password);
  15.  
  16. if ($user != false) {
  17. // use is found
  18. $response["error"] = FALSE;
  19. $response["uid"] = $user["unique_id"];
  20. $response["user"]["name"] = $user["name"];
  21. $response["user"]["cognome"] = $user["cognome"];
  22. $response["user"]["email"] = $user["email"];
  23. $response["user"]["email2"] = $user["email2"];
  24. $response["user"]["numero_appartamento"] = $user["numero_appartamento"];
  25. $response["user"]["nome_edificio"] = $user["nome_edificio"];
  26. $response["user"]["zona_metropolitana"] = $user["zona_metropolitana"];
  27. $response["user"]["created_at"] = $user["created_at"];
  28. $response["user"]["updated_at"] = $user["updated_at"];
  29. echo json_encode($response);
  30. } else {
  31. // user is not found with the credentials
  32. $response["error"] = TRUE;
  33. $response["error_msg"] = "Login credentials are wrong. Please try again!";
  34. echo json_encode($response);
  35. }
  36. } else {
  37. // required post params is missing
  38. $response["error"] = TRUE;
  39. $response["error_msg"] = "Required parameters email or password is missing!";
  40. echo json_encode($response);
  41. }
  42. ?>
  43.  
  44. private void checkLogin(final String email, final String password) {
  45. // Tag used to cancel the request
  46. String tag_string_req = "req_login";
  47.  
  48. pDialog.setMessage("Logging in ...");
  49. showDialog();
  50.  
  51. StringRequest strReq = new StringRequest(Method.POST,
  52. AppConfig.URL_LOGIN, new Response.Listener<String>() {
  53.  
  54. @Override
  55. public void onResponse(String response) {
  56. Log.d(TAG, "Login Response: " + response.toString());
  57. hideDialog();
  58.  
  59. try {
  60. JSONObject jObj = new JSONObject(response);
  61. boolean error = jObj.getBoolean("error");
  62.  
  63. // Check for error node in json
  64. if (!error) {
  65. // user successfully logged in
  66. // Create login session
  67. session.setLogin(true);
  68.  
  69. // Now store the user in SQLite
  70. String uid = jObj.getString("uid");
  71.  
  72. JSONObject user = jObj.getJSONObject("user");
  73. String name = user.getString("name");
  74. String cognome = user.getString("cognome");
  75.  
  76. String email = user.getString("email");
  77. String email2 = user.getString("email2");
  78. String numero_appartamento = user.getString("numero_appartamento");
  79. String nome_edificio = user.getString("nome_edificio");
  80. String zona_metropolitana = user.getString("zona_metropolitana");
  81.  
  82.  
  83.  
  84. String created_at = user
  85. .getString("created_at");
  86.  
  87. // Inserting row in users table
  88. db.addUser(name,cognome, email,email2,numero_appartamento,nome_edificio,zona_metropolitana,uid, created_at);
  89.  
  90. // Launch main activity
  91. Intent intent = new Intent(LoginActivity.this,
  92. // MainActivity.class);
  93. ScrollableTabsActivity.class);
  94.  
  95. startActivity(intent);
  96. finish();
  97. } else {
  98. // Error in login. Get the error message
  99. String errorMsg = jObj.getString("error_msg");
  100. Toast.makeText(getApplicationContext(),
  101. errorMsg, Toast.LENGTH_LONG).show();
  102. }
  103. } catch (JSONException e) {
  104. // JSON error
  105. e.printStackTrace();
  106. Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
  107. }
  108.  
  109. }
  110. }, new Response.ErrorListener() {
  111.  
  112. @Override
  113. public void onErrorResponse(VolleyError error) {
  114. Log.e(TAG, "Login Error: " + error.getMessage());
  115. Toast.makeText(getApplicationContext(),
  116. error.getMessage(), Toast.LENGTH_LONG).show();
  117. hideDialog();
  118. }
  119. }) {
  120.  
  121. @Override
  122. protected Map<String, String> getParams() {
  123. // Posting parameters to login url
  124. Map<String, String> params = new HashMap<String, String>();
  125. params.put("email", email);
  126. params.put("password", password);
  127. System.out.println(params);
  128. return params;
  129. }
  130.  
  131. };
  132.  
  133. // Adding request to request queue
  134. AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
  135. }
  136.  
  137. JSONObject jObj = new JSONObject(response);
  138.  
  139. AppConfig.URL_LOGIN, new Response.Listener<String>()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement