Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity {
- private FirebaseAuth mAuth;
- String email = "gg@gmail.com";
- String password = "123456";
- String TAG = "LogTestLogin";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mAuth = FirebaseAuth.getInstance();
- createAccount();
- signIn();
- }
- @Override
- public void onStart() {
- super.onStart();
- // Check if user is signed in (non-null) and update UI accordingly.
- FirebaseUser currentUser = mAuth.getCurrentUser();
- updateUI(currentUser);
- }
- private void createAccount() {
- mAuth.createUserWithEmailAndPassword(email, password)
- .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- if (task.isSuccessful()) {
- // Sign in success, update UI with the signed-in user's information
- Log.d(TAG, "createUserWithEmail:success");
- FirebaseUser user = mAuth.getCurrentUser();
- updateUI(user);
- } else {
- // If sign in fails, display a message to the user.
- Log.w(TAG, "createUserWithEmail:failure", task.getException());
- Toast.makeText(MainActivity.this, "Authentication failed.",
- Toast.LENGTH_SHORT).show();
- updateUI(null);
- }
- // ...
- }
- });
- }
- private void signIn() {
- mAuth.signInWithEmailAndPassword(email, password)
- .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- if (task.isSuccessful()) {
- // Sign in success, update UI with the signed-in user's information
- Log.d(TAG, "signInWithEmail:success");
- FirebaseUser user = mAuth.getCurrentUser();
- updateUI(user);
- } else {
- // If sign in fails, display a message to the user.
- Log.w(TAG, "signInWithEmail:failure", task.getException());
- Toast.makeText(MainActivity.this, "Authentication failed.",
- Toast.LENGTH_SHORT).show();
- updateUI(null);
- }
- // ...
- }
- });
- }
- private void updateUI(FirebaseUser user) {
- // FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
- if (user != null) {
- // Name, email address, and profile photo Url
- String name = user.getDisplayName();
- String email = user.getEmail();
- Uri photoUrl = user.getPhotoUrl();
- // Check if user's email is verified
- boolean emailVerified = user.isEmailVerified();
- // The user's ID, unique to the Firebase project. Do NOT use this value to
- // authenticate with your backend server, if you have one. Use
- // FirebaseUser.getIdToken() instead.
- String uid = user.getUid();
- String str = "name: " + name +
- "\nemail: " + email +
- "\nuid: " + uid +
- "\nphotoUrl: " + photoUrl;
- Toast.makeText(MainActivity.this, str,
- Toast.LENGTH_SHORT).show();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement