Exemplo n.º 1
0
  // retrieves the intent from RegisterActivity
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // send this to the facebook button

    callbackManager.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_REGISTER) {
      if (resultCode == RESULT_OK) {
        String name = data.getStringExtra("Name");
        Log.d("this just kicked in", "");
        // TODO: Implement successful register logic here
        // By default we just finish the Activity and log them in automatically
        // this.finish();
        HashMap<String, String> user = session.getRegistrationDetails();
        _emailText.setText(user.get(SessionManager.KEY_EMAIL));
        _passwordText.setText(user.get(SessionManager.KEY_PASSWORD));
        session.clearRegistrationDetails();
        login();
      }
    } else if (requestCode == RC_SIGN_IN) {
      GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
      handleSignInResult(result);
    }
  }
Exemplo n.º 2
0
  public void login() {

    Log.d(TAG, "Login");
    // ensures that fields are correct by calling validate method
    // Toast.makeText(getApplicationContext(), "Will make login POST request soon!",
    // Toast.LENGTH_SHORT).show();
    Toast.makeText(
            getApplicationContext(),
            "User Login Status: " + session.isLoggedIn(),
            Toast.LENGTH_LONG)
        .show();

    final ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this, R.style.AppTheme);
    progressDialog.setIndeterminate(true);
    progressDialog.setMessage("Authenticating...");
    progressDialog.show();

    if (!validate()) {
      onLoginFailed(progressDialog);
      return;
    }

    _loginButton.setEnabled(false);

    // get fields and TODO: insert into HTTP POST request below
    final String email = _emailText.getText().toString();
    final String password = _passwordText.getText().toString();
    // TODO: Implement your own authentication logic here. (make HTTP POST request to
    // <server>/oauth/token)
    // JSON {"username":"******", "password":"******" or "Bob12345"}

    StringRequest jsonObjectRequest =
        new StringRequest(
            Request.Method.POST,
            LOGIN_URL,
            new Response.Listener<String>() {
              @Override
              public void onResponse(String response) {
                Log.d(TAG, "Valid Response: " + response.toString());

                try {
                  // Parsing json object response
                  JSONObject j = new JSONObject(response);
                  jsonResponse = "";
                  String token = j.getString("access_token");
                  jsonResponse += "Access token: " + token + "\n\n";
                  Log.d(TAG, jsonResponse);
                  session.createLoginSession(email, token);
                  onLoginSuccess();

                } catch (JSONException e) {
                  e.printStackTrace();
                }
              }
            },
            new Response.ErrorListener() {
              @Override
              public void onErrorResponse(VolleyError error) {
                Log.d(TAG, "Error Response: " + error.toString());
                onLoginFailed(progressDialog);

                if (error instanceof TimeoutError || error instanceof NoConnectionError) {
                  Log.d(TAG, "Timeout/no connection error: " + error.toString());
                  Toast.makeText(
                      getBaseContext(),
                      "Please make sure you have internet connection",
                      Toast.LENGTH_LONG);
                } else if (error instanceof AuthFailureError) {
                  Log.d(TAG, "AuthFailure error: " + error.toString());
                } else if (error instanceof ServerError) {
                  Log.d(TAG, "Server error: " + error.toString());
                } else if (error instanceof NetworkError) {
                  Log.d(TAG, "Network error: " + error.toString());
                } else if (error instanceof ParseError) {
                  Log.d(TAG, "Parse error: " + error.toString());
                }
              }
            }) {
          @Override
          protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put(KEY_PASSWORD, password);
            params.put(KEY_EMAIL, email);
            params.put(KEY_GRANT_TYPE, "password");
            Log.d("Params", "params: " + params.toString());
            return params;
          }

          @Override
          public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Accept", "application/json");
            return headers;
          }

          @Override
          public String getBodyContentType() {
            return "application/x-www-form-urlencoded; charset=utf-8";
          }
        };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(jsonObjectRequest);
    Log.d(TAG, jsonObjectRequest.toString());
  }