Guest User

Untitled

a guest
Sep 13th, 2023
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. package com.melonfeed.melonfeed;
  2.  
  3. import androidx.annotation.NonNull;
  4. import androidx.appcompat.app.AppCompatActivity;
  5.  
  6. import android.content.Intent;
  7. import android.os.Bundle;
  8. import android.text.TextUtils;
  9. import android.util.Log;
  10. import android.util.Patterns;
  11. import android.view.View;
  12. import android.widget.Button;
  13. import android.widget.TextView;
  14. import android.widget.Toast;
  15.  
  16. import com.google.android.material.textfield.TextInputEditText;
  17. import com.google.firebase.FirebaseException;
  18. import com.google.firebase.auth.FirebaseAuth;
  19. import com.google.firebase.auth.FirebaseUser;
  20. import com.google.firebase.auth.PhoneAuthCredential;
  21. import com.google.firebase.auth.PhoneAuthProvider;
  22. import com.google.firebase.database.DataSnapshot;
  23. import com.google.firebase.database.DatabaseError;
  24. import com.google.firebase.database.DatabaseReference;
  25. import com.google.firebase.database.FirebaseDatabase;
  26. import com.google.firebase.database.ValueEventListener;
  27. import com.hbb20.CountryCodePicker;
  28.  
  29. import java.util.concurrent.TimeUnit;
  30. import java.util.regex.Pattern;
  31.  
  32. public class phoneLogin extends AppCompatActivity {
  33.  
  34. TextInputEditText editTextPhone, username, password;
  35. private static final Pattern PASSWORD_PATTERN =
  36. Pattern.compile("^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).{8,16}$");
  37. CountryCodePicker phoneCode;
  38. TextView errorText;
  39. Button signup;
  40. FirebaseAuth mAuth;
  41. String verificationCode;
  42. PhoneAuthProvider.ForceResendingToken resendingToken;
  43. PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
  44.  
  45. @Override
  46. protected void onCreate(Bundle savedInstanceState) {
  47. super.onCreate(savedInstanceState);
  48. setContentView(R.layout.activity_phone_login);
  49.  
  50. editTextPhone = findViewById(R.id.editTextPhone);
  51. signup = findViewById(R.id.signupBTNPhone);
  52. phoneCode = findViewById(R.id.login_countrycode);
  53. password = findViewById(R.id.password);
  54. errorText = findViewById(R.id.errorTextPhone);
  55.  
  56. mAuth = FirebaseAuth.getInstance();
  57. username = findViewById(R.id.editTextUsername);
  58. phoneCode.registerCarrierNumberEditText(editTextPhone);
  59. mAuth.getFirebaseAuthSettings().setAppVerificationDisabledForTesting(true);
  60.  
  61. signup.setOnClickListener(new View.OnClickListener() {
  62. @Override
  63. public void onClick(View v) {
  64. if (TextUtils.isEmpty(username.getText().toString()) ||
  65. TextUtils.isEmpty(editTextPhone.getText().toString()) ||
  66. TextUtils.isEmpty(password.getText().toString())) {
  67. errorText.setText("Please enter valid information");
  68. return;
  69. }
  70.  
  71. if (!PASSWORD_PATTERN.matcher(password.getText().toString()).matches()) {
  72. errorText.setText("Password must contain at least one digit, one uppercase letter, one lowercase letter, one special character, and be 8-16 characters long.");
  73. return;
  74. }
  75.  
  76. if (username.getText().toString().contains(" ")) {
  77. errorText.setText("No spaces should be in the username");
  78. return;
  79. }
  80.  
  81. checkUsernameAvailability(username.getText().toString(), new UsernameAvailabilityListener() {
  82. @Override
  83. public void onUsernameAvailable() {
  84. if (!phoneCode.isValidFullNumber()) {
  85. Toast.makeText(phoneLogin.this, "Phone number not valid", Toast.LENGTH_SHORT).show();
  86. return;
  87. }
  88. Intent intent = new Intent(phoneLogin.this, otpVerification.class);
  89. intent.putExtra("phone", phoneCode.getFullNumberWithPlus());
  90. startActivity(intent);
  91. }
  92.  
  93. @Override
  94. public void onUsernameUnavailable() {
  95. Toast.makeText(phoneLogin.this, "Username not available", Toast.LENGTH_SHORT).show();
  96. }
  97.  
  98. @Override
  99. public void onError() {
  100. Toast.makeText(phoneLogin.this, "Error checking username availability, please try again later.", Toast.LENGTH_SHORT).show();
  101. }
  102. });
  103. }
  104. });
  105.  
  106.  
  107. }
  108.  
  109. private interface UsernameAvailabilityListener {
  110. void onUsernameAvailable();
  111. void onUsernameUnavailable();
  112. void onError();
  113. }
  114.  
  115. private void checkUsernameAvailability(final String username, final phoneLogin.UsernameAvailabilityListener listener) {
  116. try {
  117. DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference().child("users");
  118. usersRef.orderByChild("username").equalTo(username).addListenerForSingleValueEvent(new ValueEventListener() {
  119. @Override
  120. public void onDataChange(@NonNull DataSnapshot snapshot) {
  121. if (snapshot.exists()) {
  122. // Username is already taken, notify the listener
  123. listener.onUsernameUnavailable();
  124. } else {
  125. // Username is available, notify the listener
  126. listener.onUsernameAvailable();
  127. }
  128. }
  129.  
  130. @Override
  131. public void onCancelled(@NonNull DatabaseError error) {
  132. // Error occurred while checking username availability, notify the listener
  133. listener.onError();
  134. }
  135. });
  136. } catch (Exception e){
  137. e.printStackTrace();
  138. listener.onError();
  139. }
  140. }
  141. }
  142.  
Advertisement
Add Comment
Please, Sign In to add comment