-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoginActivity.java
200 lines (180 loc) · 8.43 KB
/
LoginActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package project.airved;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.mongodb.stitch.android.core.Stitch;
import com.mongodb.stitch.android.core.auth.StitchUser;
import com.mongodb.stitch.android.core.auth.providers.userpassword.UserPasswordAuthProviderClient;
import com.mongodb.stitch.core.auth.providers.userpassword.UserPasswordCredential;
public class LoginActivity extends AppCompatActivity {
private EditText emailView;
private EditText passwordView;
private Button signInBtn;
private TextView registerLink;
private TextView resetPassword;
private StitchUser currentUser;
@Override
protected void onStart() {
super.onStart();
if (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION}, 101);
}
Splash.client = Stitch.getAppClient("YOUR APP-ID");
currentUser = Splash.client.getAuth().getUser();
if(currentUser != null){
startActivity(new Intent(LoginActivity.this, Dashboard.class));
finish();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
emailView = (EditText) findViewById(R.id.username);
passwordView = (EditText) findViewById(R.id.password);
signInBtn = (Button) findViewById(R.id.signinbtn);
registerLink = (TextView) findViewById(R.id.registerlink);
resetPassword = (TextView) findViewById(R.id.resetpassword);
emailView.setError(null);
passwordView.setError(null);
passwordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
if (id == EditorInfo.IME_ACTION_DONE || id == EditorInfo.IME_NULL) {
attemptLogin();
return true;
}
return false;
}
});
signInBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
attemptLogin();
}
});
resetPassword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
recoverPassword();
}
});
registerLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(LoginActivity.this, RegisterUser.class));
}
});
}
private void attemptLogin() {
final String email = emailView.getText().toString();
String password = passwordView.getText().toString();
boolean cancel = false;
View focusView = null;
if (TextUtils.isEmpty(email)) {
emailView.setError(getString(R.string.error_field_required));
focusView = emailView;
cancel = true;
} else if (!MiscFunc.isEmailValid(email)) {
emailView.setError(getString(R.string.error_invalid_email));
focusView = emailView;
cancel = true;
}
if (!TextUtils.isEmpty(password) && !MiscFunc.isPasswordValid(password)) {
passwordView.setError(getString(R.string.error_invalid_password));
focusView = passwordView;
cancel = true;
}
if (cancel) {
focusView.requestFocus();
} else {
//UserPasswordCredential credential = new UserPasswordCredential(params[0], params[1]);
new getLoginTask().execute(email,password);
}
}
private class getLoginTask extends AsyncTask<String, Void, Void> {
private ProgressDialog pd;
private byte statusCode;
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = ProgressDialog.show(LoginActivity.this, "", "Airis is recognizing you...", true, false); // Create and show Progress dialog
}
@Override
protected Void doInBackground(String... params) {
try {
UserPasswordCredential credential = new UserPasswordCredential(params[0], params[1]);
Splash.client.getAuth().loginWithCredential(credential).addOnCompleteListener(new OnCompleteListener<StitchUser>() {
@Override
public void onComplete(@NonNull final Task<StitchUser> task) {
if (task.isSuccessful()){
statusCode = 1;
onPostExecute();
}
else{
statusCode = 0;
onPostExecute();
}
}
});
} catch (Exception e) {
Log.e("stitch-auth", "Authentication Failed!");
}
return null;
}
protected void onPostExecute() {
if (statusCode == 1) {
Log.d("stitch-auth", "Authentication Successful.");
Toast.makeText(LoginActivity.this, "Airis welcomes you!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(LoginActivity.this, Dashboard.class));
pd.dismiss();
finish();
} else {
Log.e("stitch-auth", "Authentication Failed.");
pd.dismiss();
Toast.makeText(LoginActivity.this, "Ugh...! Error in recognizing.", Toast.LENGTH_SHORT).show();
passwordView.setText("");
}
}
}
private void recoverPassword() {
String email = emailView.getText().toString();
UserPasswordAuthProviderClient emailPassClient = Stitch.getDefaultAppClient().getAuth().getProviderClient(UserPasswordAuthProviderClient.factory);
if (TextUtils.isEmpty(email)) {
emailView.setError(getString(R.string.error_field_required));
} else {
emailPassClient.sendResetPasswordEmail(email)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull final Task<Void> task) {
if (task.isSuccessful()) {
Log.d("stitch-auth", "Successfully sent password reset email");
Toast.makeText(LoginActivity.this, "Password reset mail sent.", Toast.LENGTH_SHORT).show();
} else {
Log.e("stitch-auth", "Error sending password reset email:", task.getException());
Toast.makeText(LoginActivity.this, "Account not found!", Toast.LENGTH_SHORT).show();
}
}
}
);
}
}
}