Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.melonfeed.melonfeed;
- import androidx.annotation.NonNull;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.text.TextUtils;
- import android.util.Log;
- import android.util.Patterns;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.google.android.material.textfield.TextInputEditText;
- import com.google.firebase.FirebaseException;
- import com.google.firebase.auth.FirebaseAuth;
- import com.google.firebase.auth.FirebaseUser;
- import com.google.firebase.auth.PhoneAuthCredential;
- import com.google.firebase.auth.PhoneAuthProvider;
- import com.google.firebase.database.DataSnapshot;
- import com.google.firebase.database.DatabaseError;
- import com.google.firebase.database.DatabaseReference;
- import com.google.firebase.database.FirebaseDatabase;
- import com.google.firebase.database.ValueEventListener;
- import com.hbb20.CountryCodePicker;
- import java.util.concurrent.TimeUnit;
- import java.util.regex.Pattern;
- public class phoneLogin extends AppCompatActivity {
- TextInputEditText editTextPhone, username, password;
- private static final Pattern PASSWORD_PATTERN =
- Pattern.compile("^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).{8,16}$");
- CountryCodePicker phoneCode;
- TextView errorText;
- Button signup;
- FirebaseAuth mAuth;
- String verificationCode;
- PhoneAuthProvider.ForceResendingToken resendingToken;
- PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_phone_login);
- editTextPhone = findViewById(R.id.editTextPhone);
- signup = findViewById(R.id.signupBTNPhone);
- phoneCode = findViewById(R.id.login_countrycode);
- password = findViewById(R.id.password);
- errorText = findViewById(R.id.errorTextPhone);
- mAuth = FirebaseAuth.getInstance();
- username = findViewById(R.id.editTextUsername);
- phoneCode.registerCarrierNumberEditText(editTextPhone);
- mAuth.getFirebaseAuthSettings().setAppVerificationDisabledForTesting(true);
- signup.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (TextUtils.isEmpty(username.getText().toString()) ||
- TextUtils.isEmpty(editTextPhone.getText().toString()) ||
- TextUtils.isEmpty(password.getText().toString())) {
- errorText.setText("Please enter valid information");
- return;
- }
- if (!PASSWORD_PATTERN.matcher(password.getText().toString()).matches()) {
- errorText.setText("Password must contain at least one digit, one uppercase letter, one lowercase letter, one special character, and be 8-16 characters long.");
- return;
- }
- if (username.getText().toString().contains(" ")) {
- errorText.setText("No spaces should be in the username");
- return;
- }
- checkUsernameAvailability(username.getText().toString(), new UsernameAvailabilityListener() {
- @Override
- public void onUsernameAvailable() {
- if (!phoneCode.isValidFullNumber()) {
- Toast.makeText(phoneLogin.this, "Phone number not valid", Toast.LENGTH_SHORT).show();
- return;
- }
- Intent intent = new Intent(phoneLogin.this, otpVerification.class);
- intent.putExtra("phone", phoneCode.getFullNumberWithPlus());
- startActivity(intent);
- }
- @Override
- public void onUsernameUnavailable() {
- Toast.makeText(phoneLogin.this, "Username not available", Toast.LENGTH_SHORT).show();
- }
- @Override
- public void onError() {
- Toast.makeText(phoneLogin.this, "Error checking username availability, please try again later.", Toast.LENGTH_SHORT).show();
- }
- });
- }
- });
- }
- private interface UsernameAvailabilityListener {
- void onUsernameAvailable();
- void onUsernameUnavailable();
- void onError();
- }
- private void checkUsernameAvailability(final String username, final phoneLogin.UsernameAvailabilityListener listener) {
- try {
- DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference().child("users");
- usersRef.orderByChild("username").equalTo(username).addListenerForSingleValueEvent(new ValueEventListener() {
- @Override
- public void onDataChange(@NonNull DataSnapshot snapshot) {
- if (snapshot.exists()) {
- // Username is already taken, notify the listener
- listener.onUsernameUnavailable();
- } else {
- // Username is available, notify the listener
- listener.onUsernameAvailable();
- }
- }
- @Override
- public void onCancelled(@NonNull DatabaseError error) {
- // Error occurred while checking username availability, notify the listener
- listener.onError();
- }
- });
- } catch (Exception e){
- e.printStackTrace();
- listener.onError();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment