Created
June 23, 2020 09:04
-
-
Save mozerian/93923e4871105c4802329b0894797954 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.twostepauthentication; | |
import androidx.annotation.NonNull; | |
import androidx.appcompat.app.AppCompatActivity; | |
import androidx.appcompat.widget.Toolbar; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import com.google.firebase.auth.FirebaseAuth; | |
import com.google.firebase.auth.FirebaseUser; | |
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; | |
public class MainActivity extends AppCompatActivity { | |
//firebase auth to check if user is authenticated. | |
private FirebaseAuth mAuth; | |
private DatabaseReference userRef; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Toolbar toolbar = findViewById(R.id.main_toolbar); | |
setSupportActionBar(toolbar); | |
getSupportActionBar().setTitle("Home"); | |
mAuth = FirebaseAuth.getInstance(); | |
userRef = FirebaseDatabase.getInstance().getReference().child("Users"); | |
} | |
//onstart method used to check if the user is registered or not | |
@Override | |
protected void onStart() | |
{ | |
super.onStart(); | |
FirebaseUser currentUser = mAuth.getCurrentUser(); | |
if (currentUser ==null){ | |
SendUserToLoginActivity(); | |
} | |
else{ | |
//checking if the user exists in the firebase database | |
CheckUserExistence(); | |
} | |
} | |
private void CheckUserExistence() | |
{ | |
//get the user id | |
final String currentUserId = mAuth.getCurrentUser().getUid(); | |
userRef.addValueEventListener(new ValueEventListener() { | |
@Override | |
public void onDataChange(@NonNull DataSnapshot dataSnapshot) | |
{ | |
if (!dataSnapshot.hasChild(currentUserId)){ | |
//user is authenticated but but his record is not present in real time firebase database | |
SendUserToStepTwoAuthentication(); | |
} | |
} | |
@Override | |
public void onCancelled(@NonNull DatabaseError databaseError) { | |
} | |
}); | |
} | |
private void SendUserToStepTwoAuthentication() | |
{ | |
Intent steptwoIntent = new Intent(MainActivity.this, StepTwoAuthentication.class); | |
steptwoIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); | |
startActivity(steptwoIntent); | |
finish(); | |
} | |
private void SendUserToLoginActivity() | |
{ | |
Intent loginIntent = new Intent(MainActivity.this, Login.class); | |
loginIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); | |
startActivity(loginIntent); | |
finish(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment