Skip to content

Commit aecb312

Browse files
committed
Using Firebase Auth to Authenticate Users
1 parent 62b1d09 commit aecb312

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

.idea/caches/build_file_checksums.ser

0 Bytes
Binary file not shown.

app/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ dependencies {
2525
testImplementation 'junit:junit:4.12'
2626
androidTestImplementation 'com.android.support.test:runner:1.0.2'
2727
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
28-
compile 'com.google.firebase:firebase-core:15.0.0'
28+
implementation 'com.google.firebase:firebase-core:15.0.0'
29+
implementation 'com.firebaseui:firebase-ui-auth:3.3.1'
2930
}
3031

3132
apply plugin: 'com.google.gms.google-services'
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,50 @@
11
package com.dragosholban.myinstagramapp;
22

3+
import android.content.Intent;
34
import android.support.v7.app.AppCompatActivity;
45
import android.os.Bundle;
6+
import android.view.View;
7+
import android.widget.Toast;
8+
9+
import com.firebase.ui.auth.AuthUI;
10+
import com.firebase.ui.auth.IdpResponse;
11+
import com.google.firebase.auth.FirebaseAuth;
12+
import com.google.firebase.auth.FirebaseUser;
513

614
public class MainActivity extends AppCompatActivity {
715

16+
private static final int RC_SIGN_IN = 123;
17+
818
@Override
919
protected void onCreate(Bundle savedInstanceState) {
1020
super.onCreate(savedInstanceState);
1121
setContentView(R.layout.activity_main);
1222
}
23+
24+
public void signIn(View view) {
25+
startActivityForResult(
26+
// Get an instance of AuthUI based on the default app
27+
AuthUI.getInstance().createSignInIntentBuilder().build(),
28+
RC_SIGN_IN);
29+
}
30+
31+
@Override
32+
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
33+
super.onActivityResult(requestCode, resultCode, data);
34+
35+
if (requestCode == RC_SIGN_IN) {
36+
IdpResponse response = IdpResponse.fromResultIntent(data);
37+
38+
if (resultCode == RESULT_OK) {
39+
// Successfully signed in
40+
FirebaseUser fbUser = FirebaseAuth.getInstance().getCurrentUser();
41+
Toast.makeText(this, "Authenticated as " + fbUser.getDisplayName(), Toast.LENGTH_SHORT).show();
42+
} else {
43+
// Sign in failed, check response for error code
44+
if (response != null) {
45+
Toast.makeText(this, response.getError().getMessage(), Toast.LENGTH_SHORT).show();
46+
}
47+
}
48+
}
49+
}
1350
}

app/src/main/res/layout/activity_main.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
android:layout_height="match_parent"
77
tools:context=".MainActivity">
88

9-
<TextView
9+
<Button
1010
android:layout_width="wrap_content"
1111
android:layout_height="wrap_content"
12-
android:text="Hello World!"
12+
android:text="Sign In"
1313
app:layout_constraintBottom_toBottomOf="parent"
1414
app:layout_constraintLeft_toLeftOf="parent"
1515
app:layout_constraintRight_toRightOf="parent"
16-
app:layout_constraintTop_toTopOf="parent" />
16+
app:layout_constraintTop_toTopOf="parent"
17+
android:onClick="signIn"/>
1718

1819
</android.support.constraint.ConstraintLayout>

0 commit comments

Comments
 (0)