Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LoginActivity extends AppCompatActivity implements View.OnClickListener, GoogleApiClient.OnConnectionFailedListener{
- private EditText emailEditText;
- private EditText passwordEditText;
- private FirebaseAuth mFirebaseAuth;
- private FirebaseAuth.AuthStateListener mAuthStateListener;
- private RelativeLayout progressBar;
- private GoogleApiClient mGoogleApiClient;
- private volatile boolean googleSignOn;
- private long lastClickTime = 0;
- private static final int RC_SIGN_IN = 9001;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- if(FirebaseAuth.getInstance().getCurrentUser() != null){
- progressBar = (RelativeLayout) findViewById(R.id.loginProgressBar);
- progressBar.setVisibility(View.VISIBLE);
- progressBar.bringToFront();
- setupUsersManager();
- }
- Calligrapher calligrapher = new Calligrapher(this);
- calligrapher.setFont(this, "fonts/GothamRoundedMedium_21022.ttf", true);
- emailEditText = (EditText) findViewById(R.id.email);
- passwordEditText = (EditText) findViewById(R.id.password);
- mFirebaseAuth = FirebaseAuth.getInstance();
- googleSignOn = new Boolean(false);
- // Configure sign-in to request the user's ID, email address, and basic
- // profile. ID and basic profile are included in DEFAULT_SIGN_IN.
- GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
- .requestIdToken(getString(R.string.default_web_client))
- .requestEmail()
- .build();
- // Build a GoogleApiClient with access to the Google Sign-In API and the
- // options specified by gso.
- mGoogleApiClient = new GoogleApiClient.Builder(this)
- .enableAutoManage(this /* Activity */, this /* OnConnectionFailedListener */)
- .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
- .build();
- //set clicklistener for signup button
- findViewById(R.id.signUpButton).setOnClickListener(this);
- findViewById(R.id.signInButton).setOnClickListener(this);
- findViewById(R.id.googleSignIn).setOnClickListener(this);
- mAuthStateListener = new FirebaseAuth.AuthStateListener() {
- @Override
- public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
- FirebaseUser user = mFirebaseAuth.getCurrentUser();
- if(user != null) {
- Log.d("Sign in", user.getDisplayName() + " is signed in");
- }
- else {
- Log.d("Sign in", "user is signed out");
- }
- }
- };
- }
- private void signInWithEmail() {
- String email = emailEditText.getText().toString().trim();
- String password = passwordEditText.getText().toString().trim();
- if(TextUtils.isEmpty(email)){
- Toast.makeText(LoginActivity.this, "Please be sure to enter email", Toast.LENGTH_SHORT).show();
- return;
- }
- if(TextUtils.isEmpty(password)){
- Toast.makeText(LoginActivity.this, "Please be sure to enter password", Toast.LENGTH_SHORT).show();
- return;
- }
- mFirebaseAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- if(task.isSuccessful()) {
- progressBar = (RelativeLayout) findViewById(R.id.loginProgressBar);
- progressBar.setVisibility(View.VISIBLE);
- progressBar.bringToFront();
- setupUsersManager();
- }
- else {
- Toast.makeText(LoginActivity.this, "Something happened!", Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- @Override
- public void onClick(View v) {
- //get id of calling element who just clicked
- int i = v.getId();
- //if clicked signup button
- if (i == R.id.signUpButton) {
- startActivity(new Intent(LoginActivity.this, SignupActivity.class));
- }
- else if (i == R.id.signInButton) {
- signInWithEmail();
- }
- else if(i == R.id.googleSignIn) {
- findViewById(R.id.googleSignIn).setOnClickListener(null);
- signInWithGoogle();
- }
- }
- private void signInWithGoogle() {
- Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
- startActivityForResult(signInIntent, RC_SIGN_IN);
- }
- @Override
- public void onStart() {
- super.onStart();
- mFirebaseAuth.addAuthStateListener(mAuthStateListener);
- }
- @Override
- public void onStop() {
- super.onStop();
- if (mAuthStateListener != null) {
- mFirebaseAuth.removeAuthStateListener(mAuthStateListener);
- }
- }
- private void handleSignInResult(GoogleSignInResult result) {
- Log.d("Google Login", "handleSignInResult:" + result.isSuccess());
- if (result.isSuccess()) {
- progressBar = (RelativeLayout) findViewById(R.id.loginProgressBar);
- progressBar.setVisibility(View.VISIBLE);
- progressBar.bringToFront();
- // Signed in successfully, show authenticated UI.
- GoogleSignInAccount acct = result.getSignInAccount();
- Toast.makeText(LoginActivity.this, "Was successful", Toast.LENGTH_SHORT).show();
- firebaseAuthWithGoogle(acct);
- } else {
- // Signed out, show unauthenticated UI.
- findViewById(R.id.googleSignIn).setOnClickListener(this);
- Toast.makeText(LoginActivity.this, "Was not successful", Toast.LENGTH_SHORT).show();
- }
- }
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
- if (requestCode == RC_SIGN_IN) {
- GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
- handleSignInResult(result);
- } else {
- findViewById(R.id.googleSignIn).setOnClickListener(this);
- }
- }
- @Override
- public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
- }
- @Override
- public void onBackPressed() {}
- private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
- Log.d("Auth G w/ F", "firebaseAuthWithGoogle:" + acct.getId());
- AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
- mFirebaseAuth.signInWithCredential(credential)
- .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- Log.d("Auth G w/ F", "signInWithCredential:onComplete:" + task.isSuccessful());
- // If sign in fails, display a message to the user. If sign in succeeds
- // the auth state listener will be notified and logic to handle the
- // signed in user can be handled in the listener.
- if (!task.isSuccessful()) {
- Log.w("Auth G w/ F", "signInWithCredential", task.getException());
- Toast.makeText(LoginActivity.this, "Authentication failed.",
- Toast.LENGTH_SHORT).show();
- } else {
- setupUsersManager();
- }
- }
- });
- }
- private void setupUsersManager(){
- final DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
- final String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
- if (uid != null){
- DatabaseReference ref = mDatabase.child("users");
- ref.child(uid).addListenerForSingleValueEvent(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- AuthenticatedUser user;
- user = dataSnapshot.getValue(AuthenticatedUser.class);
- if (user == null){
- addUserToDatabase();
- } else {
- AuthenticatedUserManager.getInstance().setUser(user);
- getStripeInformation();
- startActivity(new Intent(LoginActivity.this, CheckInActivity.class));
- finish();
- }
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- Log.d("DatabaseCancelled", "Database returned canceled" + databaseError.toString());
- }
- });
- }
- }
- private void getStripeInformation(){
- final AuthenticatedUserManager userManager = AuthenticatedUserManager.getInstance();
- AuthenticatedUser user = userManager.getUser();
- if(!TextUtils.isEmpty(user.getStripeCustId()) && user.getStripeCustId()!= null){
- // Instantiate the RequestQueue.
- RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
- String url = "https://sleepy-forest-31423.herokuapp.com/charges/getCustomerPaymentInfo?"
- + "stripeCustId=" + user.getStripeCustId();
- Log.d("URL", url);
- // Request a string response from the provided URL.
- StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
- new Response.Listener<String>() {
- @Override
- public void onResponse(String responseJSON) {
- // your response
- Log.d("HTTP-RESPONSE", responseJSON);
- try {
- JSONObject obj = new JSONObject(responseJSON);
- JSONArray stripeCards = obj.getJSONObject("sources").getJSONArray("data");
- String card = stripeCards.getJSONObject(0).getString("brand")
- + " " + stripeCards.getJSONObject(0).getString("last4");
- AuthenticatedUser user = AuthenticatedUserManager.getInstance().getUser();
- user.setPayment(card);
- user.setStripeCustId(obj.get("id").toString());
- user.setStripeInfo(obj);
- AuthenticatedUserManager.getInstance().setUser(user);
- } catch (Throwable t) {
- Log.e("FROM OBJ", "Could not parse malformed JSON: "" + responseJSON + """);
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- // error
- }
- }
- );
- // Add the request to the RequestQueue.
- queue.add(stringRequest);
- }
- }
- private void addUserToDatabase(){
- DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
- AuthenticatedUser user = new AuthenticatedUser(
- mFirebaseAuth.getCurrentUser().getDisplayName(),
- "none",
- mFirebaseAuth.getCurrentUser().getEmail(),
- "none",
- "none",
- "none"
- );
- mDatabase.child("users").child(mFirebaseAuth.getCurrentUser().getUid()).setValue(user);
- mDatabase.child("users").child(mFirebaseAuth.getCurrentUser().getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- AuthenticatedUser user;
- user = dataSnapshot.getValue(AuthenticatedUser.class);
- AuthenticatedUserManager.getInstance().setUser(user);
- getStripeInformation();
- startActivity(new Intent(LoginActivity.this, CheckInActivity.class));
- finish();
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- Log.d("DatabaseCancelled", "Database returned canceled" + databaseError.toString());
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement