Exchange

Firebase Google Auth - Android Studio - Tamil

 Firebase Google Auth - Android Studio - Tamil




Main Activity.Java

package com.vtappsandgames.pelistasiddhaclinic;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

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.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;

public class MainActivity extends AppCompatActivity {


private static final String TAG = "GoogleActivity";
private static final int RC_SIGN_IN = 9001;

// [START declare_auth]
private FirebaseAuth mAuth;
// [END declare_auth]

private GoogleSignInClient mGoogleSignInClient;

private Button gSignin;


// [START on_start_check_user]
@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
if(currentUser != null){
Intent intent = new Intent(getApplicationContext(), ProfileActivity.class);
startActivity(intent);
}
}
// [END on_start_check_user]

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

findViewById(R.id.btn_siginin).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
signIn();
}
});

mAuth = FirebaseAuth.getInstance();
createRequest();
}

private void createRequest() {

// [START config_signin]
// Configure Google Sign In
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();

mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
// [END config_signin]

}

// [START signin]
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
// [END signin]


// [START onactivityresult]
@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.getIdToken());
} catch (ApiException e) {
// Google Sign In failed, update UI appropriately
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
// [END onactivityresult]

// [START auth_with_google]
private void firebaseAuthWithGoogle(String idToken) {
AuthCredential credential = GoogleAuthProvider.getCredential(idToken, null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
FirebaseUser user = mAuth.getCurrentUser();

Intent intent = new Intent(getApplicationContext(), ProfileActivity.class);
startActivity(intent);

} else {
// If sign in fails, display a message to the user.
Toast.makeText(MainActivity.this, "Sorry Auth Failed", Toast.LENGTH_SHORT).show();
}
}
});
}
// [END auth_with_google]

}


Profile Activity.Java

package com.vtappsandgames.pelistasiddhaclinic;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.firebase.auth.FirebaseAuth;

public class ProfileActivity extends AppCompatActivity {

private TextView name, mail;
private Button signout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);

signout = findViewById(R.id.btn_signout);
name = findViewById(R.id.txt_name);
mail = findViewById(R.id.txt_mai);


GoogleSignInAccount googleSignInAccount = GoogleSignIn.getLastSignedInAccount(this);
if(googleSignInAccount != null){
name.setText(googleSignInAccount.getDisplayName());
mail.setText(googleSignInAccount.getEmail());
}

signout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

FirebaseAuth.getInstance().signOut();
Intent intent = new Intent(getApplicationContext(), MainActivity .class);
startActivity(intent);

}
});
}
}


Thank you

Post a Comment

0 Comments