Created
September 2, 2017 09:16
-
-
Save pinkeshdarji/49cdaadc968af82f7e9774afaf878ec5 to your computer and use it in GitHub Desktop.
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 com.example.pinkesh.firebasephoneauthentication; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.support.design.widget.FloatingActionButton; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.Toolbar; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.widget.Toast; | |
import com.firebase.ui.auth.AuthUI; | |
import com.firebase.ui.auth.ErrorCodes; | |
import com.firebase.ui.auth.IdpResponse; | |
import com.firebase.ui.auth.ResultCodes; | |
import com.google.firebase.auth.FirebaseAuth; | |
import java.util.Arrays; | |
public class MainActivity extends AppCompatActivity { | |
private static final int RC_SIGN_IN = 123; | |
private FirebaseAuth auth; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); | |
setSupportActionBar(toolbar); | |
auth = FirebaseAuth.getInstance(); | |
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); | |
fab.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
// if (auth.getCurrentUser() != null) { | |
// // already signed in | |
// Toast.makeText(MainActivity.this, "Users already logged in", Toast.LENGTH_SHORT).show(); | |
// }else { | |
// not signed in | |
startActivityForResult( | |
AuthUI.getInstance() | |
.createSignInIntentBuilder() | |
.setAvailableProviders( | |
Arrays.asList( | |
new AuthUI.IdpConfig.Builder(AuthUI.PHONE_VERIFICATION_PROVIDER).build() | |
)) | |
.build(), | |
RC_SIGN_IN); | |
// } | |
// Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) | |
// .setAction("Action", null).show(); | |
} | |
}); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.menu_main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
//noinspection SimplifiableIfStatement | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
// RC_SIGN_IN is the request code you passed into startActivityForResult(...) when starting the sign in flow. | |
if (requestCode == RC_SIGN_IN) { | |
IdpResponse response = IdpResponse.fromResultIntent(data); | |
// Successfully signed in | |
if (resultCode == ResultCodes.OK) { | |
Toast.makeText(this, "OTP verification success", Toast.LENGTH_SHORT).show(); | |
return; | |
} else { | |
// Sign in failed | |
if (response == null) { | |
// User pressed back button | |
Log.e("Login","Login canceled by User"); | |
return; | |
} | |
if (response.getErrorCode() == ErrorCodes.NO_NETWORK) { | |
Log.e("Login","No Internet Connection"); | |
return; | |
} | |
if (response.getErrorCode() == ErrorCodes.UNKNOWN_ERROR) { | |
Log.e("Login","Unknown Error"); | |
return; | |
} | |
} | |
Log.e("Login","Unknown sign in response"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment