Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Using Firebase Auth to Authenticate Users
  • Loading branch information
dragosholban committed May 13, 2018
1 parent 62b1d09 commit aecb312
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
3 changes: 2 additions & 1 deletion app/build.gradle
Expand Up @@ -25,7 +25,8 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
compile 'com.google.firebase:firebase-core:15.0.0'
implementation 'com.google.firebase:firebase-core:15.0.0'
implementation 'com.firebaseui:firebase-ui-auth:3.3.1'
}

apply plugin: 'com.google.gms.google-services'
@@ -1,13 +1,50 @@
package com.dragosholban.myinstagramapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.auth.IdpResponse;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class MainActivity extends AppCompatActivity {

private static final int RC_SIGN_IN = 123;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void signIn(View view) {
startActivityForResult(
// Get an instance of AuthUI based on the default app
AuthUI.getInstance().createSignInIntentBuilder().build(),
RC_SIGN_IN);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == RC_SIGN_IN) {
IdpResponse response = IdpResponse.fromResultIntent(data);

if (resultCode == RESULT_OK) {
// Successfully signed in
FirebaseUser fbUser = FirebaseAuth.getInstance().getCurrentUser();
Toast.makeText(this, "Authenticated as " + fbUser.getDisplayName(), Toast.LENGTH_SHORT).show();
} else {
// Sign in failed, check response for error code
if (response != null) {
Toast.makeText(this, response.getError().getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
}
}
7 changes: 4 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Expand Up @@ -6,13 +6,14 @@
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:text="Sign In"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toTopOf="parent"
android:onClick="signIn"/>

</android.support.constraint.ConstraintLayout>

0 comments on commit aecb312

Please sign in to comment.