From 81d37822c244c39dbc5f8e1ad3969bf77c6d3349 Mon Sep 17 00:00:00 2001 From: Daniel Mai Date: Tue, 27 Sep 2016 14:26:53 -0700 Subject: [PATCH] Open FirebaseUI Auth sign in screen if user is not logged in. --- app/build.gradle | 4 ++ .../udacity/friendlychat/MainActivity.java | 46 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/app/build.gradle b/app/build.gradle index 2b95de077..9c5acf304 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -43,6 +43,10 @@ dependencies { // Firebase compile 'com.google.firebase:firebase-database:9.6.0' + compile 'com.google.firebase:firebase-auth:9.6.0' + + // FirebaseUI + compile 'com.firebaseui:firebase-ui-auth:0.6.0' } apply plugin: 'com.google.gms.google-services' \ No newline at end of file diff --git a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java index 31fa5b98e..5c0ec2969 100644 --- a/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java +++ b/app/src/main/java/com/google/firebase/udacity/friendlychat/MainActivity.java @@ -16,6 +16,7 @@ package com.google.firebase.udacity.friendlychat; import android.os.Bundle; +import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; import android.text.Editable; import android.text.InputFilter; @@ -29,7 +30,11 @@ import android.widget.ImageButton; import android.widget.ListView; import android.widget.ProgressBar; +import android.widget.Toast; +import com.firebase.ui.auth.AuthUI; +import com.google.firebase.auth.FirebaseAuth; +import com.google.firebase.auth.FirebaseUser; import com.google.firebase.database.ChildEventListener; import com.google.firebase.database.DataSnapshot; import com.google.firebase.database.DatabaseError; @@ -46,6 +51,8 @@ public class MainActivity extends AppCompatActivity { public static final String ANONYMOUS = "anonymous"; public static final int DEFAULT_MSG_LENGTH_LIMIT = 1000; + public static final int RC_SIGN_IN = 1; + private ListView mMessageListView; private MessageAdapter mMessageAdapter; private ProgressBar mProgressBar; @@ -59,6 +66,8 @@ public class MainActivity extends AppCompatActivity { private FirebaseDatabase mFirebaseDatabase; private DatabaseReference mMessagesDatabaseReference; private ChildEventListener mChildEventListener; + private FirebaseAuth mFirebaseAuth; + private FirebaseAuth.AuthStateListener mAuthStateListener; @Override protected void onCreate(Bundle savedInstanceState) { @@ -69,6 +78,7 @@ protected void onCreate(Bundle savedInstanceState) { // Initialize Firebase components mFirebaseDatabase = FirebaseDatabase.getInstance(); + mFirebaseAuth = FirebaseAuth.getInstance(); mMessagesDatabaseReference = mFirebaseDatabase.getReference().child("messages"); @@ -141,6 +151,42 @@ public void onChildMoved(DataSnapshot dataSnapshot, String s) {} public void onCancelled(DatabaseError databaseError) {} }; mMessagesDatabaseReference.addChildEventListener(mChildEventListener); + + mAuthStateListener = new FirebaseAuth.AuthStateListener() { + @Override + public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { + FirebaseUser user = firebaseAuth.getCurrentUser(); + if (user != null) { + // User is signed in + Toast.makeText(MainActivity.this, "You're now signed in. Welcome to FriendlyChat.", Toast.LENGTH_SHORT).show(); + } else { + // User is signed out + startActivityForResult( + AuthUI.getInstance() + .createSignInIntentBuilder() + .setIsSmartLockEnabled(false) + .setProviders( + AuthUI.EMAIL_PROVIDER, + AuthUI.GOOGLE_PROVIDER) + .build(), + RC_SIGN_IN); + } + } + }; + } + + @Override + protected void onResume() { + super.onResume(); + mFirebaseAuth.addAuthStateListener(mAuthStateListener); + } + + @Override + protected void onPause() { + super.onPause(); + if (mAuthStateListener != null) { + mFirebaseAuth.removeAuthStateListener(mAuthStateListener); + } } @Override