Skip to content

Commit 3807eb9

Browse files
authored
feat: Implement forgot password feature (#206)
1 parent 998fe07 commit 3807eb9

File tree

5 files changed

+148
-4
lines changed

5 files changed

+148
-4
lines changed

app/src/main/java/com/example/saar/ChangeCredentials/ChangePasswordFragment.java

+26-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.example.saar.ChangeCredentials;
22

33
import android.app.ProgressDialog;
4+
import android.content.Intent;
45
import android.content.SharedPreferences;
56
import android.os.Bundle;
67
import android.preference.PreferenceManager;
@@ -23,6 +24,7 @@
2324
import com.android.volley.toolbox.StringRequest;
2425
import com.android.volley.toolbox.Volley;
2526
import com.example.saar.Constant;
27+
import com.example.saar.Login_SignUp.LoginSignupActivity;
2628
import com.example.saar.R;
2729
import com.example.saar.Utils.Utils;
2830

@@ -42,6 +44,8 @@ public class ChangePasswordFragment extends Fragment {
4244
ProgressDialog progressDialog;
4345
SharedPreferences preferences;
4446
SharedPreferences.Editor editor;
47+
Boolean forgot_password = false;
48+
String rollno;
4549

4650
@Override
4751
public View onCreateView(LayoutInflater inflater, ViewGroup container,
@@ -62,6 +66,13 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
6266
super.onViewCreated(view, savedInstanceState);
6367
getActivity().setTitle(R.string.change_password_fragment);
6468

69+
if(getActivity().getIntent().hasExtra("rollno")){
70+
forgot_password=true;
71+
old_password.setText("Dummy");
72+
old_password.setVisibility(View.GONE);
73+
rollno=getActivity().getIntent().getStringExtra("rollno");
74+
}
75+
6576
reset_password.setOnClickListener(new View.OnClickListener() {
6677
@Override
6778
public void onClick(View v) {
@@ -98,8 +109,14 @@ public void onResponse(String response) {
98109
int status = Integer.parseInt(jsonObject.getString("status"));
99110
if (status == 207) {
100111
Toast.makeText(getContext(), getString(R.string.change_password_succesfull), Toast.LENGTH_LONG).show();
101-
Utils.unsuscribeFromNotification(preferences.getString(Constant.ROLLNO, ""));
102-
Utils.logout(editor, getContext());
112+
if(preferences.getBoolean(Constant.LOGIN_STATUS,false)){
113+
Utils.unsuscribeFromNotification(preferences.getString(Constant.ROLLNO, ""));
114+
Utils.logout(editor, getContext());
115+
}else{
116+
Intent intent = new Intent(getContext(), LoginSignupActivity.class);
117+
startActivity(intent);
118+
}
119+
103120
} else {
104121

105122
JSONArray jsonArray = jsonObject.getJSONArray("messages");
@@ -121,10 +138,16 @@ public void onErrorResponse(VolleyError error) {
121138
}) {
122139
protected Map<String, String> getParams() throws AuthFailureError {
123140
Map<String, String> params = new HashMap<String, String>();
124-
params.put("rollno", preferences.getString(Constant.ROLLNO, ""));
141+
125142
params.put("old_password", old_password.getText().toString());
126143
params.put("new_password", new_password.getText().toString());
127144
params.put("confirm_password", confirm_new_password.getText().toString());
145+
if(forgot_password){
146+
params.put("forgot_password","forgot_password");
147+
params.put("rollno",rollno);
148+
}else{
149+
params.put("rollno", preferences.getString(Constant.ROLLNO, ""));
150+
}
128151
return params;
129152
}
130153
};
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,127 @@
11
package com.example.saar.ChangeCredentials;
22

3+
import android.app.ProgressDialog;
4+
import android.content.Intent;
35
import android.os.Bundle;
46
import android.support.annotation.NonNull;
57
import android.support.annotation.Nullable;
68
import android.support.v4.app.Fragment;
79
import android.view.LayoutInflater;
810
import android.view.View;
911
import android.view.ViewGroup;
12+
import android.widget.Button;
13+
import android.widget.EditText;
14+
import android.widget.Toast;
1015

16+
import com.android.volley.AuthFailureError;
17+
import com.android.volley.DefaultRetryPolicy;
18+
import com.android.volley.Request;
19+
import com.android.volley.RequestQueue;
20+
import com.android.volley.Response;
21+
import com.android.volley.VolleyError;
22+
import com.android.volley.toolbox.StringRequest;
23+
import com.android.volley.toolbox.Volley;
24+
import com.example.saar.Constant;
25+
import com.example.saar.OtpActivity;
1126
import com.example.saar.R;
27+
import com.example.saar.Utils.Utils;
28+
29+
import org.json.JSONArray;
30+
import org.json.JSONException;
31+
import org.json.JSONObject;
32+
33+
import java.util.HashMap;
34+
import java.util.Map;
35+
36+
import timber.log.Timber;
1237

1338
public class ForgotPasswordFragment extends Fragment {
1439

40+
EditText email, rollno;
41+
Button forgotPassword;
42+
ProgressDialog progressDialog;
43+
1544
@Override
1645
public View onCreateView(LayoutInflater inflater, ViewGroup container,
1746
Bundle savedInstanceState) {
1847
// Inflate the layout for this fragment
19-
return inflater.inflate(R.layout.fragment_forgot_password, container, false);
48+
View rootView = inflater.inflate(R.layout.fragment_forgot_password, container, false);
49+
email=rootView.findViewById(R.id.email_text_forgot_password);
50+
rollno=rootView.findViewById(R.id.roll_text_forgot_password);
51+
forgotPassword=rootView.findViewById(R.id.forgot_password_button);
52+
return rootView;
2053
}
2154

2255
@Override
2356
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
2457
super.onViewCreated(view, savedInstanceState);
2558
getActivity().setTitle(R.string.forgot_password);
59+
60+
forgotPassword.setOnClickListener(new View.OnClickListener() {
61+
@Override
62+
public void onClick(View v) {
63+
requestDatas();
64+
}
65+
});
66+
}
67+
68+
private void requestDatas() {
69+
Utils.closeKeyboard(getView(), getContext());
70+
progressDialog = new ProgressDialog(getContext());
71+
progressDialog.setMessage("Requesting....");
72+
progressDialog.show();
73+
StringRequest stringRequest = new StringRequest(Request.Method.POST, Constant.FORGOT_PASSWORD_URL, new Response.Listener<String>() {
74+
75+
@Override
76+
public void onResponse(String response) {
77+
Timber.d(response);
78+
progressDialog.dismiss();
79+
try {
80+
JSONObject jsonObject = new JSONObject(response);
81+
int status = Integer.parseInt(jsonObject.getString("status"));
82+
83+
if (status == 204) {
84+
Timber.d(getString(R.string.success_forgot_password_request));
85+
Toast.makeText(getContext(), getString(R.string.success_forgot_password_request), Toast.LENGTH_LONG).show();
86+
87+
Intent intent = new Intent(getContext(), OtpActivity.class);
88+
intent.putExtra("rollno",rollno.getText().toString());
89+
intent.putExtra("forgot_password","forgot_password");
90+
startActivity(intent);
91+
} else {
92+
Timber.d(getString(R.string.error_Forgot_password));
93+
JSONArray jsonArray = jsonObject.getJSONArray("messages");
94+
Toast.makeText(getContext(), jsonArray.toString(), Toast.LENGTH_LONG).show();
95+
Timber.d(jsonArray.toString());
96+
}
97+
98+
} catch (JSONException e) {
99+
e.printStackTrace();
100+
}
101+
}
102+
}, new Response.ErrorListener() {
103+
@Override
104+
public void onErrorResponse(VolleyError error) {
105+
//otp_progress.setVisibility(View.GONE);
106+
progressDialog.dismiss();
107+
Toast.makeText(getContext(), error.getLocalizedMessage(), Toast.LENGTH_LONG).show();
108+
Timber.d(error.toString());
109+
}
110+
111+
}) {
112+
protected Map<String, String> getParams() throws AuthFailureError {
113+
Map<String, String> params = new HashMap<String, String>();
114+
params.put("email", email.getText().toString());
115+
params.put("rollno", rollno.getText().toString());
116+
return params;
117+
}
118+
};
119+
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
120+
10000,
121+
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
122+
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
123+
124+
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
125+
Request<String> data = requestQueue.add(stringRequest);
26126
}
27127
}

app/src/main/java/com/example/saar/Constant.java

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class Constant {
1717
public static final String UPDATE_PROFILE_IMAGE_URL = "https://saar-server.000webhostapp.com/functions/profile_image.php";
1818
public static final String CHANGE_PASSWORD_URL = "https://saar-server.000webhostapp.com/functions/changePassword.php";
1919
public static final String RESEND_OTP_URL = "https://saar-server.000webhostapp.com/functions/resendOTP.php";
20+
public static final String FORGOT_PASSWORD_URL = "https://saar-server.000webhostapp.com/functions/forgotPassword.php";
2021

2122
//Store user profile data
2223
public static final String LOGIN_STATUS = "login_status";

app/src/main/java/com/example/saar/OtpActivity.java

+17
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.android.volley.VolleyError;
2121
import com.android.volley.toolbox.StringRequest;
2222
import com.android.volley.toolbox.Volley;
23+
import com.example.saar.ChangeCredentials.ChangeCredentialsActivity;
2324
import com.example.saar.Login_SignUp.LoginSignupActivity;
2425
import com.goodiebag.pinview.Pinview;
2526

@@ -44,6 +45,7 @@ public class OtpActivity extends AppCompatActivity {
4445
FloatingActionButton sendOTP;
4546
String otpValue = "", rollno;
4647
ProgressDialog progressDialog;
48+
Boolean forgot_password = false;
4749

4850
@Override
4951
protected void onCreate(Bundle savedInstanceState) {
@@ -80,6 +82,11 @@ public void onDataEntered(Pinview pinview, boolean fromUser) {
8082
resend.setVisibility(View.VISIBLE);
8183
}
8284

85+
if(getIntent().hasExtra("forgot_password")){
86+
forgot_password=true;
87+
}else{
88+
forgot_password=false;
89+
}
8390
//Action to be performed when the sending otp button is pressed
8491
sendOTP = (FloatingActionButton) findViewById(R.id.otp_next);
8592
sendOTP.setOnClickListener(new View.OnClickListener() {
@@ -210,6 +217,13 @@ public void onResponse(String response) {
210217
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
211218
startActivity(intent);
212219

220+
}else if(status == 208) {
221+
Timber.d(getString(R.string.forgot_password_success));
222+
Intent intent = new Intent(OtpActivity.this, ChangeCredentialsActivity.class);
223+
intent.putExtra("EXTRA", "openChangePassword");
224+
intent.putExtra("rollno", rollno);
225+
startActivity(intent);
226+
213227
} else {
214228

215229
JSONArray jsonArray = jsonObject.getJSONArray("messages");
@@ -233,6 +247,9 @@ protected Map<String, String> getParams() throws AuthFailureError {
233247
Map<String, String> params = new HashMap<String, String>();
234248
params.put("rollno", rollno);
235249
params.put("verification_code", otpValue);
250+
if(forgot_password){
251+
params.put("forgot_password","forgot_password");
252+
}
236253
return params;
237254
}
238255
};

app/src/main/res/values/strings.xml

+3
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,9 @@
270270
<string name="forgot_password">Forgot Password?</string>
271271
<string name="forgot_password_details">We just need your registered email id and college roll number to send you password reset instructions</string>
272272
<string name="reset_password">Reset Password</string>
273+
<string name="error_Forgot_password">Error requesting to reset your password.</string>
274+
<string name="success_forgot_password_request">Successfully requested to initiate forgot password. </string>
275+
<string name="forgot_password_success">We have succesfully verified you. You can now change your password.</string>
273276

274277
<!-- Change Password -->
275278
<string name="change_password_succesfull">Succesfully changed the password.</string>

0 commit comments

Comments
 (0)