Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Using Firebase Auth to Authenticate Users
- Loading branch information
1 parent
62b1d09
commit aecb312
Showing
4 changed files
with
43 additions
and
4 deletions.
There are no files selected for viewing
Binary file not shown.
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
37 changes: 37 additions & 0 deletions
37
app/src/main/java/com/dragosholban/myinstagramapp/MainActivity.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} | ||
} | ||
} |
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