Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.sanchez.worldgramproject;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.preference.PreferenceManager;
- import android.util.Log;
- import android.view.View;
- import android.widget.Toast;
- import com.example.sanchez.worldgramproject.Fragments.MainActivity;
- import com.google.android.gms.auth.api.Auth;
- import com.google.android.gms.auth.api.signin.GoogleSignIn;
- import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
- import com.google.android.gms.auth.api.signin.GoogleSignInClient;
- import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
- import com.google.android.gms.auth.api.signin.GoogleSignInResult;
- import com.google.android.gms.common.ConnectionResult;
- import com.google.android.gms.common.SignInButton;
- import com.google.android.gms.common.api.ApiException;
- import com.google.android.gms.tasks.OnCompleteListener;
- import com.google.android.gms.tasks.Task;
- import com.google.firebase.auth.AuthCredential;
- import com.google.firebase.auth.AuthResult;
- import com.google.firebase.auth.FirebaseAuth;
- import com.google.firebase.auth.FirebaseUser;
- import com.google.firebase.auth.GoogleAuthProvider;
- import com.google.firebase.database.DataSnapshot;
- import com.google.firebase.database.DatabaseError;
- import com.google.firebase.database.DatabaseReference;
- import com.google.firebase.database.FirebaseDatabase;
- import com.google.firebase.database.ValueEventListener;
- import androidx.annotation.NonNull;
- import androidx.annotation.Nullable;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.fragment.app.FragmentActivity;
- /**
- * Created by Sanchez on 10.09.2016.
- */
- public class Login extends Activity {
- private static final String TAG = "LoginProcess";
- Intent UsernameIntent;
- Intent maps;
- SignInButton gsignInButton;
- private static final int RC_SIGN_IN = 1;
- DatabaseReference mRef;
- FirebaseAuth mAuth;
- FirebaseAuth.AuthStateListener mAuthListener;
- GoogleSignInOptions gso;
- GoogleSignInClient mGoogleSignInClient;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.welcomescreenlogin);
- UsernameIntent = new Intent(this, Username.class);
- maps = new Intent(Login.this, MainActivity.class);
- mRef = FirebaseDatabase.getInstance().getReference().child("Users");
- mAuth = FirebaseAuth.getInstance();
- PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().clear().apply();
- mAuthListener = new FirebaseAuth.AuthStateListener() {
- @Override
- public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
- FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
- if (user == null) {
- // User is signed out
- return;
- } else {
- mRef.child(user.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot snapshot) {
- if (snapshot.exists()) {
- // TODO: handle the case where the data already exists
- startActivity(maps);
- finish();
- }
- else {
- // User is signed in but no Username
- UsernameIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- startActivity(UsernameIntent);
- finish();
- // TODO: handle the case where the data does not yet exist
- }
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- Toast.makeText(Login.this, "Error", Toast.LENGTH_LONG).show();
- }
- });
- }
- }
- };
- mAuth.addAuthStateListener(mAuthListener);
- gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
- .requestIdToken(getString(R.string.default_web_client_id))
- .requestEmail()
- .build();
- mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
- gsignInButton = findViewById(R.id.sib);
- gsignInButton.setColorScheme(SignInButton.COLOR_DARK); // wide button style
- gsignInButton.setOnClickListener(myhandler);
- }
- View.OnClickListener myhandler = new View.OnClickListener() {
- public void onClick(View v) {
- signIn();
- }
- };
- public void signIn() {
- Intent signInIntent = mGoogleSignInClient.getSignInIntent();
- startActivityForResult(signInIntent, RC_SIGN_IN);
- }
- @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) {
- Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
- try {
- // Google Sign In was successful, authenticate with Firebase
- GoogleSignInAccount account = task.getResult(ApiException.class);
- firebaseAuthWithGoogle(account);
- } catch (ApiException e) {
- // Google Sign In failed, update UI appropriately
- Log.w(TAG, "Google sign in failed", e);
- Toast.makeText(this, "dont give up", Toast.LENGTH_SHORT).show();
- // ...
- }
- }
- }
- private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
- Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
- AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
- mAuth.signInWithCredential(credential)
- .addOnCompleteListener(this,new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- Log.d(TAG, "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(TAG, "signInWithCredential", task.getException());
- Toast.makeText(Login.this, "Authentication failed.",
- Toast.LENGTH_SHORT).show();
- }
- // ...
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement