Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity {
- private final static String TAG = "MainActivity";
- private FirebaseAuth mAuth;
- private TextView text;
- private EditText login;
- private EditText pass;
- private Button save;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- text = findViewById(R.id.text);
- login = findViewById(R.id.login);
- pass = findViewById(R.id.pass);
- save = findViewById(R.id.save);
- save.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- performLoginOrAccountCreation(login.getText().toString(), pass.getText().toString());
- }
- });
- FirebaseApp.initializeApp(this);
- mAuth = FirebaseAuth.getInstance();
- }
- @Override
- protected void onStart() {
- super.onStart();
- FirebaseUser user = mAuth.getCurrentUser();
- updateUI(user);
- }
- private void performLoginOrAccountCreation(final String email, final String password){
- mAuth.fetchSignInMethodsForEmail(email).addOnCompleteListener(
- this, new OnCompleteListener<SignInMethodQueryResult>() {
- @Override
- public void onComplete(@NonNull Task<SignInMethodQueryResult> task) {
- if (task.isSuccessful()) {
- SignInMethodQueryResult result = task.getResult();
- if(result != null && result.getSignInMethods() != null && result.getSignInMethods().size() > 0){
- signIn(email, password);
- }else{ createAccount(email, password); }
- } else {
- Log.w(TAG, "User check failed", task.getException());
- Toast.makeText(MainActivity.this, "There is a problem, please try again later.", Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- private void createAccount(String email, String password) {
- mAuth.createUserWithEmailAndPassword(email, password)
- .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- FirebaseUser user = null;
- if (task.isSuccessful()) { user = mAuth.getCurrentUser(); }
- else {
- Log.w(TAG, "createUserWithEmail:failure", task.getException());
- Toast.makeText(MainActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show();
- }
- updateUI(user);
- }
- });
- }
- private void signIn(String email, String password) {
- mAuth.signInWithEmailAndPassword(email, password)
- .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- FirebaseUser user = null;
- if (task.isSuccessful()) { user = mAuth.getCurrentUser(); }
- else {
- Log.w(TAG, "signInWithEmailAndPassword:failure", task.getException());
- Toast.makeText(MainActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show();
- }
- updateUI(user);
- }
- });
- }
- private void updateUI(FirebaseUser user) {
- if(user == null) {
- text.setText("NO USER");
- }else{
- text.setText(user.getEmail() + " authed");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement