Advertisement
Guest User

Untitled

a guest
Dec 30th, 2018
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.20 KB | None | 0 0
  1. package com.example.sanchez.worldgramproject;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.preference.PreferenceManager;
  7.  
  8.  
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.Toast;
  12.  
  13. import com.example.sanchez.worldgramproject.Fragments.MainActivity;
  14. import com.google.android.gms.auth.api.Auth;
  15. import com.google.android.gms.auth.api.signin.GoogleSignIn;
  16. import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
  17. import com.google.android.gms.auth.api.signin.GoogleSignInClient;
  18. import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
  19. import com.google.android.gms.auth.api.signin.GoogleSignInResult;
  20. import com.google.android.gms.common.ConnectionResult;
  21. import com.google.android.gms.common.SignInButton;
  22. import com.google.android.gms.common.api.ApiException;
  23.  
  24. import com.google.android.gms.tasks.OnCompleteListener;
  25. import com.google.android.gms.tasks.Task;
  26. import com.google.firebase.auth.AuthCredential;
  27. import com.google.firebase.auth.AuthResult;
  28. import com.google.firebase.auth.FirebaseAuth;
  29. import com.google.firebase.auth.FirebaseUser;
  30. import com.google.firebase.auth.GoogleAuthProvider;
  31. import com.google.firebase.database.DataSnapshot;
  32. import com.google.firebase.database.DatabaseError;
  33. import com.google.firebase.database.DatabaseReference;
  34. import com.google.firebase.database.FirebaseDatabase;
  35. import com.google.firebase.database.ValueEventListener;
  36.  
  37. import androidx.annotation.NonNull;
  38. import androidx.annotation.Nullable;
  39. import androidx.appcompat.app.AppCompatActivity;
  40. import androidx.fragment.app.FragmentActivity;
  41.  
  42. /**
  43. * Created by Sanchez on 10.09.2016.
  44. */
  45.  
  46.  
  47. public class Login extends Activity {
  48.  
  49. private static final String TAG = "LoginProcess";
  50. Intent UsernameIntent;
  51. Intent maps;
  52. SignInButton gsignInButton;
  53. private static final int RC_SIGN_IN = 1;
  54. DatabaseReference mRef;
  55. FirebaseAuth mAuth;
  56. FirebaseAuth.AuthStateListener mAuthListener;
  57. GoogleSignInOptions gso;
  58. GoogleSignInClient mGoogleSignInClient;
  59.  
  60.  
  61. @Override
  62. protected void onCreate(Bundle savedInstanceState) {
  63. super.onCreate(savedInstanceState);
  64. setContentView(R.layout.welcomescreenlogin);
  65.  
  66. UsernameIntent = new Intent(this, Username.class);
  67. maps = new Intent(Login.this, MainActivity.class);
  68. mRef = FirebaseDatabase.getInstance().getReference().child("Users");
  69. mAuth = FirebaseAuth.getInstance();
  70. PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().clear().apply();
  71.  
  72. mAuthListener = new FirebaseAuth.AuthStateListener() {
  73. @Override
  74. public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
  75. FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
  76.  
  77. if (user == null) {
  78. // User is signed out
  79.  
  80. return;
  81. } else {
  82.  
  83. mRef.child(user.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
  84. @Override
  85. public void onDataChange(DataSnapshot snapshot) {
  86. if (snapshot.exists()) {
  87.  
  88. // TODO: handle the case where the data already exists
  89. startActivity(maps);
  90. finish();
  91. }
  92. else {
  93.  
  94. // User is signed in but no Username
  95. UsernameIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  96. startActivity(UsernameIntent);
  97. finish();
  98. // TODO: handle the case where the data does not yet exist
  99.  
  100.  
  101. }
  102. }
  103.  
  104. @Override
  105. public void onCancelled(DatabaseError databaseError) {
  106. Toast.makeText(Login.this, "Error", Toast.LENGTH_LONG).show();
  107. }
  108.  
  109. });
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117. }
  118. }
  119. };
  120.  
  121. mAuth.addAuthStateListener(mAuthListener);
  122.  
  123.  
  124. gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
  125. .requestIdToken(getString(R.string.default_web_client_id))
  126. .requestEmail()
  127. .build();
  128.  
  129.  
  130.  
  131. mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
  132.  
  133. gsignInButton = findViewById(R.id.sib);
  134.  
  135. gsignInButton.setColorScheme(SignInButton.COLOR_DARK); // wide button style
  136. gsignInButton.setOnClickListener(myhandler);
  137.  
  138.  
  139. }
  140.  
  141. View.OnClickListener myhandler = new View.OnClickListener() {
  142. public void onClick(View v) {
  143. signIn();
  144. }
  145.  
  146. };
  147.  
  148.  
  149.  
  150. public void signIn() {
  151.  
  152. Intent signInIntent = mGoogleSignInClient.getSignInIntent();
  153. startActivityForResult(signInIntent, RC_SIGN_IN);
  154. }
  155.  
  156. @Override
  157. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  158. super.onActivityResult(requestCode, resultCode, data);
  159.  
  160. // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
  161. if (requestCode == RC_SIGN_IN) {
  162. Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
  163. try {
  164. // Google Sign In was successful, authenticate with Firebase
  165. GoogleSignInAccount account = task.getResult(ApiException.class);
  166. firebaseAuthWithGoogle(account);
  167. } catch (ApiException e) {
  168. // Google Sign In failed, update UI appropriately
  169. Log.w(TAG, "Google sign in failed", e);
  170. Toast.makeText(this, "dont give up", Toast.LENGTH_SHORT).show();
  171. // ...
  172. }
  173. }
  174.  
  175. }
  176.  
  177. private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
  178. Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
  179.  
  180. AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
  181. mAuth.signInWithCredential(credential)
  182. .addOnCompleteListener(this,new OnCompleteListener<AuthResult>() {
  183. @Override
  184. public void onComplete(@NonNull Task<AuthResult> task) {
  185. Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());
  186.  
  187. // If sign in fails, display a message to the user. If sign in succeeds
  188. // the auth state listener will be notified and logic to handle the
  189. // signed in user can be handled in the listener.
  190. if (!task.isSuccessful()) {
  191. Log.w(TAG, "signInWithCredential", task.getException());
  192. Toast.makeText(Login.this, "Authentication failed.",
  193. Toast.LENGTH_SHORT).show();
  194. }
  195.  
  196.  
  197. // ...
  198. }
  199. });
  200.  
  201. }
  202.  
  203.  
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement