Guest User

Untitled

a guest
Nov 13th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.62 KB | None | 0 0
  1. if (mAuth.getCurrentUser() != null) {
  2. startActivity(new Intent(LoginActivity.this, MainActivity.class));
  3. finish();
  4. }
  5.  
  6.  
  7. setContentView(R.layout.activity_login);
  8. inputEmail = (EditText) findViewById(R.id.email);
  9. inputPassword = (EditText) findViewById(R.id.password);
  10. progressBar = (ProgressBar) findViewById(R.id.progressBar);
  11. btnSignup = (Button) findViewById(R.id.btn_signup);
  12. btnLogin = (Button) findViewById(R.id.btn_login);
  13. btnReset = (Button) findViewById(R.id.btn_reset_password);
  14.  
  15. //Get Firebase auth instance
  16. mAuth = FirebaseAuth.getInstance();
  17.  
  18. btnSignup.setOnClickListener(new View.OnClickListener() {
  19. @Override
  20. public void onClick(View v) {
  21. startActivity(new Intent(LoginActivity.this, SignupActivity.class));
  22. }
  23. });
  24. btnReset.setOnClickListener(new View.OnClickListener() {
  25. @Override
  26. public void onClick(View v) {
  27. startActivity(new Intent(LoginActivity.this, ResetPasswordActivity.class));
  28. }
  29. });
  30.  
  31. btnLogin.setOnClickListener(new View.OnClickListener() {
  32.  
  33. @Override
  34. public void onClick(View v) {
  35. String email = inputEmail.getText().toString();
  36. final String password = inputPassword.getText().toString();
  37.  
  38. if (TextUtils.isEmpty(email)) {
  39. Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
  40. return;
  41. }
  42.  
  43. if (TextUtils.isEmpty(password)) {
  44. Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
  45.  
  46. return;
  47. }
  48.  
  49. progressBar.setVisibility(View.VISIBLE);
  50.  
  51. //authenticate user
  52. mAuth.signInWithEmailAndPassword(email, password)
  53. .addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
  54. @Override
  55. public void onComplete(@NonNull Task<AuthResult> task) {
  56.  
  57. // If sign in fails, display a message to the user. If sign in succeeds
  58. // the auth state listener will be notified and logic to handle the
  59. // signed in user can be handled in the listener.
  60. progressBar.setVisibility(View.GONE);
  61. if (!task.isSuccessful()) {
  62. // there was an error
  63. if (password.length() < 6) {
  64. inputPassword.setError(getString(R.string.minimum_password));
  65. } else {
  66. Toast.makeText(LoginActivity.this, getString(R.string.auth_failed), Toast.LENGTH_LONG).show();
  67. }
  68. } else {
  69. Intent homeIntent = new Intent(LoginActivity.this, Home1.class);
  70. startActivity(homeIntent);
  71.  
  72.  
  73. }
  74. }
  75. });
  76. }
  77. });
  78. }
  79.  
  80.  
  81.  
  82. }
  83.  
  84. private EditText inputEmail;
  85. private Button btnReset, btnback;
  86. private FirebaseAuth mAuth;
  87. private ProgressBar progressBar;
  88. private FirebaseAuth.AuthStateListener mAuthListener;
  89.  
  90. @Override
  91. protected void onCreate(Bundle savedInstanceState) {
  92. super.onCreate(savedInstanceState);
  93. setContentView(R.layout.activity_reset_password);
  94. inputEmail = (EditText) findViewById(R.id.email);
  95. btnReset = (Button) findViewById(R.id.btn_reset_password);
  96. btnback = (Button) findViewById(R.id.btn_back);
  97. progressBar = (ProgressBar) findViewById(R.id.progressBar);
  98.  
  99. mAuth = FirebaseAuth.getInstance();
  100. btnback.setOnClickListener(new View.OnClickListener() {
  101. @Override
  102. public void onClick(View v) {
  103. finish();
  104. }
  105. });
  106.  
  107. btnReset.setOnClickListener(new View.OnClickListener() {
  108. @Override
  109. public void onClick(View v) {
  110. String email = inputEmail.getText().toString().trim();
  111.  
  112. if (TextUtils.isEmpty(email)) {
  113. Toast.makeText(getApplication(), "Enter your registered email id", Toast.LENGTH_SHORT).show();
  114. return;
  115. }
  116.  
  117. progressBar.setVisibility(View.VISIBLE);
  118. mAuth.sendPasswordResetEmail(email)
  119. .addOnCompleteListener(new OnCompleteListener<Void>() {
  120. @Override
  121. public void onComplete(@NonNull Task<Void> task) {
  122. if (task.isSuccessful()) {
  123. Toast.makeText(ResetPasswordActivity.this, "Nós te enviamos instrunções para recuperar sua senha!", Toast.LENGTH_SHORT).show();
  124. } else {
  125. Toast.makeText(ResetPasswordActivity.this, "Falha ao enviar email de recuperação!", Toast.LENGTH_SHORT).show();
  126.  
  127. }
  128.  
  129. progressBar.setVisibility(View.GONE);
  130. }
  131. });
  132. }
  133. });
  134. }
  135.  
  136. private EditText inputEmail, inputPassword; //hit option + enter if you on mac , for windows hit ctrl + enter
  137. private Button btnSignIn, btnSignUp, btnResetPassword;
  138. private ProgressBar progressBar;
  139. private FirebaseAuth mAuth;
  140. private FirebaseAuth.AuthStateListener mAuthListener;
  141.  
  142. @Override
  143. protected void onCreate(Bundle savedInstanceState) {
  144. super.onCreate(savedInstanceState);
  145. setContentView(R.layout.activity_signup);
  146. //Get Firebase auth instance
  147. mAuth = FirebaseAuth.getInstance();
  148. btnSignIn = (Button) findViewById(R.id.sign_in_button);
  149. btnSignUp = (Button) findViewById(R.id.sign_up_button);
  150. inputEmail = (EditText) findViewById(R.id.email);
  151. inputPassword = (EditText) findViewById(R.id.password);
  152. progressBar = (ProgressBar) findViewById(R.id.progressBar);
  153. btnResetPassword = (Button) findViewById(R.id.btn_reset_password);
  154.  
  155. btnResetPassword.setOnClickListener(new View.OnClickListener() {
  156. @Override
  157. public void onClick(View v) {
  158. startActivity(new Intent(SignupActivity.this,MainActivity.class));
  159. }
  160. });
  161. btnSignIn.setOnClickListener(new View.OnClickListener() {
  162. @Override
  163. public void onClick(View v) {
  164. finish();
  165. }
  166. });
  167. btnSignUp.setOnClickListener(new View.OnClickListener() {
  168. @Override
  169. public void onClick(View v) {
  170.  
  171. String email = inputEmail.getText().toString().trim();
  172. String password = inputPassword.getText().toString().trim();
  173.  
  174. if (TextUtils.isEmpty(email)) {
  175. Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
  176. return;
  177. }
  178.  
  179. if (TextUtils.isEmpty(password)) {
  180. Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
  181. return;
  182. }
  183.  
  184. if(password.length() <6){
  185.  
  186. Toast.makeText(getApplicationContext(),"Digite uma senha com mais de 6 digitos",Toast.LENGTH_SHORT).show();
  187. return;
  188. }
  189.  
  190.  
  191. progressBar.setVisibility(View.VISIBLE);
  192. //create user
  193. mAuth.createUserWithEmailAndPassword(email, password)
  194. .addOnCompleteListener(SignupActivity.this, new OnCompleteListener<AuthResult>() {
  195. @Override
  196. public void onComplete(@NonNull Task<AuthResult> task) {
  197. Toast.makeText(SignupActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
  198. progressBar.setVisibility(View.GONE);
  199. // If sign in fails, display a message to the user. If sign in succeeds
  200. // the auth state listener will be notified and logic to handle the
  201. // signed in user can be handled in the listener.
  202. if (!task.isSuccessful()) {
  203. Toast.makeText(SignupActivity.this, "Authentication failed." + task.getException(),
  204. Toast.LENGTH_SHORT).show();
  205. } else {
  206. startActivity(new Intent(SignupActivity.this, MainActivity[].class));
  207. }
  208. }
  209. });
  210.  
  211. }
  212. });
  213. }
  214.  
  215. @Override
  216. protected void onResume() {
  217. super.onResume();
  218. progressBar.setVisibility(View.GONE);
  219. }
  220.  
  221. //get current user
  222. final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
  223. setDataToView(user);
  224.  
  225. mAuthListener = new FirebaseAuth.AuthStateListener() {
  226. @Override
  227. public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
  228. FirebaseUser user = firebaseAuth.getCurrentUser();
  229. if (user == null) {
  230. // user auth state is changed - user is null
  231. // launch login activity
  232. startActivity(new Intent(MainActivity.this, LoginActivity.class));
  233. finish();
  234. }
  235. }
  236. };
  237. btnChangePassword = (Button) findViewById(R.id.change_password_button);
  238.  
  239. btnRemoveUser = (Button) findViewById(R.id.remove_user_button);
  240.  
  241. changePassword = (Button) findViewById(R.id.changePass);
  242. remove = (Button) findViewById(R.id.remove);
  243. signOut = (Button) findViewById(R.id.sign_out);
  244.  
  245. oldEmail = (EditText) findViewById(R.id.old_email);
  246.  
  247. password = (EditText) findViewById(R.id.password);
  248. newPassword = (EditText) findViewById(R.id.newPassword);
  249.  
  250. oldEmail.setVisibility(View.GONE);
  251.  
  252. password.setVisibility(View.GONE);
  253. newPassword.setVisibility(View.GONE);
  254.  
  255. changePassword.setVisibility(View.GONE);
  256.  
  257. remove.setVisibility(View.GONE);
  258.  
  259. progressBar = (ProgressBar) findViewById(R.id.progressBar);
  260.  
  261. if (progressBar != null) {
  262. progressBar.setVisibility(View.GONE);
  263. }
  264.  
  265. btnChangePassword.setOnClickListener(new View.OnClickListener() {
  266. @Override
  267. public void onClick(View v) {
  268. oldEmail.setVisibility(View.GONE);
  269.  
  270. password.setVisibility(View.GONE);
  271. newPassword.setVisibility(View.VISIBLE);
  272.  
  273. changePassword.setVisibility(View.VISIBLE);
  274.  
  275. remove.setVisibility(View.GONE);
  276. }
  277. });
  278.  
  279. changePassword.setOnClickListener(new View.OnClickListener() {
  280. @Override
  281. public void onClick(View v) {
  282. progressBar.setVisibility(View.VISIBLE);
  283. if (user != null && !newPassword.getText().toString().trim().equals("")) {
  284. if (newPassword.getText().toString().trim().length() < 6) {
  285. newPassword.setError("Senha muito curta, entre com pelo menos 6 digitos");
  286. progressBar.setVisibility(View.GONE);
  287. } else {
  288. user.updatePassword(newPassword.getText().toString().trim())
  289. .addOnCompleteListener(new OnCompleteListener<Void>() {
  290. @Override
  291. public void onComplete(@NonNull Task<Void> task) {
  292. if (task.isSuccessful()) {
  293. Toast.makeText(MainActivity.this, "Senha atualizada,entre com sua nova senha!", Toast.LENGTH_SHORT).show();
  294. signOut();
  295. progressBar.setVisibility(View.GONE);
  296. } else {
  297. Toast.makeText(MainActivity.this, "Falha ao atualizar senha!", Toast.LENGTH_SHORT).show();
  298. progressBar.setVisibility(View.GONE);
  299. }
  300. }
  301. });
  302. }
  303. } else if (newPassword.getText().toString().trim().equals("")) {
  304. newPassword.setError("Enter password");
  305. progressBar.setVisibility(View.GONE);
  306. }
  307. }
  308. });
  309.  
  310. btnRemoveUser.setOnClickListener(new View.OnClickListener() {
  311. @Override
  312. public void onClick(View v) {
  313. progressBar.setVisibility(View.VISIBLE);
  314. if (user != null) {
  315. user.delete()
  316. .addOnCompleteListener(new OnCompleteListener<Void>() {
  317. @Override
  318. public void onComplete(@NonNull Task<Void> task) {
  319. if (task.isSuccessful()) {
  320. Toast.makeText(MainActivity.this, "Seu perfil foi deletado :( Crie uma conta agora!", Toast.LENGTH_SHORT).show();
  321.  
  322. startActivity(new Intent(MainActivity.this, SignupActivity.class));
  323. finish();
  324. progressBar.setVisibility(View.GONE);
  325. } else {
  326. Toast.makeText(MainActivity.this, "Failed to delete your account!", Toast.LENGTH_SHORT).show();
  327. progressBar.setVisibility(View.GONE);
  328. }
  329. }
  330. });
  331. }
  332. }
  333. });
  334. signOut.setOnClickListener(new View.OnClickListener() {
  335. @Override
  336. public void onClick(View v) {
  337. signOut();
  338. }
  339. });
  340. }
  341.  
  342. @SuppressLint("SetTextI18n")
  343. private void setDataToView(FirebaseUser user) {
  344.  
  345. email.setText("User Email: " + user.getEmail());
  346.  
  347.  
  348. }
  349.  
  350. // this listener will be called when there is change in firebase user session
  351. FirebaseAuth.AuthStateListener authListener = new FirebaseAuth.AuthStateListener() {
  352. @SuppressLint("SetTextI18n")
  353. @Override
  354. public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
  355. FirebaseUser user = firebaseAuth.getCurrentUser();
  356. if (user == null) {
  357. // user auth state is changed - user is null
  358. // launch login activity
  359. startActivity(new Intent(MainActivity.this, LoginActivity.class));
  360. finish();
  361. } else {
  362. setDataToView(user);
  363.  
  364. }
  365. }
  366.  
  367.  
  368. };
  369.  
  370. //sign out method
  371. public void signOut() {
  372. mAuth.signOut();
  373.  
  374. @Override
  375. protected void onResume() {
  376. super.onResume();
  377. progressBar.setVisibility(View.GONE);
  378. }
  379.  
  380. @Override
  381. public void onStart() {
  382. super.onStart();
  383. mAuth.addAuthStateListener(authListener);
  384. }
  385.  
  386. @Override
  387. public void onStop() {
  388. super.onStop();
  389. if (authListener != null) {
  390. mAuth.removeAuthStateListener(authListener);
  391. }
  392. }
Add Comment
Please, Sign In to add comment