Last active
March 5, 2024 18:32
-
-
Save alvareztech/8c38122832535b20f4afc42c5b0b9366 to your computer and use it in GitHub Desktop.
Firebase Auth with Facebook a Android application
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
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
tools:context="tech.alvarez.today.LoginActivity"> | |
<com.facebook.login.widget.LoginButton | |
android:id="@+id/loginButton" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerHorizontal="true" | |
android:layout_centerVertical="true" | |
android:paddingBottom="10dp" | |
android:paddingTop="10dp" /> | |
<ProgressBar | |
android:id="@+id/progressBar" | |
style="?android:attr/progressBarStyleLarge" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerHorizontal="true" | |
android:layout_centerVertical="true" | |
android:indeterminate="true" | |
android:visibility="gone" /> | |
</RelativeLayout> |
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
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
tools:context="tech.alvarez.today.MainActivity"> | |
<TextView | |
android:id="@+id/nameTextView" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerHorizontal="true" | |
android:layout_centerVertical="true" | |
android:text="Nombre" | |
android:textSize="20sp" /> | |
<TextView | |
android:id="@+id/emailTextView" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_above="@id/nameTextView" | |
android:layout_centerHorizontal="true" | |
android:text="Email" | |
android:textSize="16sp" /> | |
<TextView | |
android:id="@+id/uidTextView" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_below="@id/nameTextView" | |
android:layout_centerHorizontal="true" | |
android:text="UID" | |
android:textSize="14sp" /> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentBottom="true" | |
android:layout_centerHorizontal="true" | |
android:onClick="logout" | |
android:text="@string/logout" /> | |
</RelativeLayout> |
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
apply plugin: 'com.android.application' | |
android { | |
compileSdkVersion 24 | |
buildToolsVersion "24.0.0" | |
defaultConfig { | |
applicationId "tech.alvarez.today" | |
minSdkVersion 15 | |
targetSdkVersion 24 | |
versionCode 1 | |
versionName "1.0" | |
} | |
buildTypes { | |
release { | |
minifyEnabled false | |
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | |
} | |
} | |
} | |
dependencies { | |
compile fileTree(dir: 'libs', include: ['*.jar']) | |
testCompile 'junit:junit:4.12' | |
compile 'com.android.support:appcompat-v7:24.0.0' | |
compile 'com.facebook.android:facebook-android-sdk:[4,5)' | |
compile 'com.google.firebase:firebase-core:9.0.2' | |
compile 'com.google.firebase:firebase-auth:9.0.2' | |
} | |
apply plugin: 'com.google.gms.google-services' |
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
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<color name="colorPrimary">#4CAF50</color> | |
<color name="colorPrimaryDark">#388E3C</color> | |
<color name="colorAccent">#FFD740</color> | |
</resources> |
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 tech.alvarez.today; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.View; | |
import android.widget.ProgressBar; | |
import android.widget.Toast; | |
import com.facebook.AccessToken; | |
import com.facebook.CallbackManager; | |
import com.facebook.FacebookCallback; | |
import com.facebook.FacebookException; | |
import com.facebook.login.LoginResult; | |
import com.facebook.login.widget.LoginButton; | |
import com.google.android.gms.tasks.OnCompleteListener; | |
import com.google.android.gms.tasks.Task; | |
import com.google.firebase.auth.AuthCredential; | |
import com.google.firebase.auth.AuthResult; | |
import com.google.firebase.auth.FacebookAuthProvider; | |
import com.google.firebase.auth.FirebaseAuth; | |
import com.google.firebase.auth.FirebaseUser; | |
import java.util.Arrays; | |
public class LoginActivity extends AppCompatActivity { | |
private LoginButton loginButton; | |
private CallbackManager callbackManager; | |
private FirebaseAuth firebaseAuth; | |
private FirebaseAuth.AuthStateListener firebaseAuthListener; | |
private ProgressBar progressBar; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_login); | |
progressBar = (ProgressBar) findViewById(R.id.progressBar); | |
callbackManager = CallbackManager.Factory.create(); | |
loginButton = (LoginButton) findViewById(R.id.loginButton); | |
loginButton.setReadPermissions(Arrays.asList("email")); | |
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { | |
@Override | |
public void onSuccess(LoginResult loginResult) { | |
handleFacebookAccessToken(loginResult.getAccessToken()); | |
} | |
@Override | |
public void onCancel() { | |
Toast.makeText(getApplicationContext(), R.string.cancel_login, Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
public void onError(FacebookException error) { | |
Toast.makeText(getApplicationContext(), R.string.error_login, Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
firebaseAuth = FirebaseAuth.getInstance(); | |
firebaseAuthListener = new FirebaseAuth.AuthStateListener() { | |
@Override | |
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { | |
FirebaseUser user = firebaseAuth.getCurrentUser(); | |
if (user != null) { | |
goMainScreen(); | |
} | |
} | |
}; | |
} | |
private void handleFacebookAccessToken(AccessToken accessToken) { | |
progressBar.setVisibility(View.VISIBLE); | |
loginButton.setVisibility(View.GONE); | |
AuthCredential credential = FacebookAuthProvider.getCredential(accessToken.getToken()); | |
firebaseAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { | |
@Override | |
public void onComplete(@NonNull Task<AuthResult> task) { | |
if (!task.isSuccessful()) { | |
Toast.makeText(getApplicationContext(), R.string.firebase_error_login, Toast.LENGTH_LONG).show(); | |
} | |
progressBar.setVisibility(View.GONE); | |
loginButton.setVisibility(View.VISIBLE); | |
} | |
}); | |
} | |
private void goMainScreen() { | |
Intent intent = new Intent(this, MainActivity.class); | |
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); | |
startActivity(intent); | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
callbackManager.onActivityResult(requestCode, resultCode, data); | |
} | |
@Override | |
protected void onStart() { | |
super.onStart(); | |
firebaseAuth.addAuthStateListener(firebaseAuthListener); | |
} | |
@Override | |
protected void onStop() { | |
super.onStop(); | |
firebaseAuth.removeAuthStateListener(firebaseAuthListener); | |
} | |
} |
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 tech.alvarez.today; | |
import android.content.Intent; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.View; | |
import android.widget.TextView; | |
import com.facebook.login.LoginManager; | |
import com.google.firebase.auth.FirebaseAuth; | |
import com.google.firebase.auth.FirebaseUser; | |
public class MainActivity extends AppCompatActivity { | |
private TextView nameTextView; | |
private TextView emailTextView; | |
private TextView uidTextView; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
nameTextView = (TextView) findViewById(R.id.nameTextView); | |
emailTextView = (TextView) findViewById(R.id.emailTextView); | |
uidTextView = (TextView) findViewById(R.id.uidTextView); | |
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); | |
if (user != null) { | |
String name = user.getDisplayName(); | |
String email = user.getEmail(); | |
Uri photoUrl = user.getPhotoUrl(); | |
String uid = user.getUid(); | |
nameTextView.setText(name); | |
emailTextView.setText(email); | |
uidTextView.setText(uid); | |
} else { | |
goLoginScreen(); | |
} | |
} | |
private void goLoginScreen() { | |
Intent intent = new Intent(this, LoginActivity.class); | |
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); | |
startActivity(intent); | |
} | |
public void logout(View view) { | |
FirebaseAuth.getInstance().signOut(); | |
LoginManager.getInstance().logOut(); | |
goLoginScreen(); | |
} | |
} |
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
<resources> | |
<string name="app_name">Today</string> | |
<string name="facebook_app_id">TU_ID_APP</string> | |
<string name="welcome">Bienvenido</string> | |
<string name="cancel_login">Se canceló la operación</string> | |
<string name="error_login">Ocurrió un error al ingresar</string> | |
<string name="logout">Cerrar sesión</string> | |
<string name="firebase_error_login">Error al iniciar sesión en Firebase</string> | |
</resources> |
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 tech.alvarez.today; | |
import android.app.Application; | |
import com.facebook.FacebookSdk; | |
import com.facebook.appevents.AppEventsLogger; | |
/** | |
* Created by Daniel Alvarez on 29/6/16. | |
* Copyright © 2016 Alvarez.tech. All rights reserved. | |
*/ | |
public class TodayApp extends Application { | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
FacebookSdk.sdkInitialize(getApplicationContext()); | |
AppEventsLogger.activateApp(this); | |
} | |
} |
el todayapp.java es otra activity?
que funcion cumple?
thanks!
great! some deprecation happens
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank's a lot 👍