build.gradle (Module 단위)
implementation 'com.google.firebase:firebase-analytics:17.2.2' // 파이어베이스 앱 분석
implementation 'com.google.firebase:firebase-core:17.2.1' // 파이어베이스 코어
implementation 'com.google.firebase:firebase-auth:19.1.0' // 파이어베이스 인증
implementation 'com.firebaseui:firebase-ui-auth:4.2.1' // 파이어베이스 인증2
implementation 'com.github.bumptech.glide:glide:4.9.0' // 글라이드 이미지 로딩
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0' // 글라이드 이미지 로딩2
activity_main.xml (구글 로그인 버튼)
<com.google.android.gms.common.SignInButton
android:id="@+id/btn_google"
android:layout_width="200dp"
android:layout_height="wrap_content" />
activity_result_login.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginResultActivity">
<TextView
android:id="@+id/tv_nickname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="닉네임"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/iv_profile" />
<ImageView
android:id="@+id/iv_profile"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="200dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
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.GoogleApiClient;
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.GoogleAuthProvider;
public class MainActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener {
private SignInButton btn_google; // 구글 로그인 버튼
private FirebaseAuth auth; // Firebase 인증 객체
private GoogleApiClient googleApiClient; // 구글 api 클라이언트 객체
private static final int REQ_SIGN_GOOGLE = 100; // 구글 로그인 결과 코드
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GoogleSignInOptions googleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
googleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this,this)
.addApi(Auth.GOOGLE_SIGN_IN_API, googleSignInOptions)
.build();
auth = FirebaseAuth.getInstance(); // Firebase 인증 객체 초기화
btn_google = findViewById(R.id.btn_google);
btn_google.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient);
startActivityForResult(intent, REQ_SIGN_GOOGLE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQ_SIGN_GOOGLE){
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if(result.isSuccess()){ // 인증 결과가 성공
GoogleSignInAccount account = result.getSignInAccount(); // 구글 로그인 정보
resultLoogin(account);
}
}
}
private void resultLoogin(final GoogleSignInAccount account) {
AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
auth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
Toast.makeText(MainActivity.this, "로그인 성공", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), LoginResultActivity.class);
intent.putExtra("nickName", account.getDisplayName());
intent.putExtra("photoUrl", String.valueOf(account.getPhotoUrl()));
startActivity(intent);
}else{
Toast.makeText(MainActivity.this, "로그인 실패", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}
LoginResultActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.bumptech.glide.Glide;
public class LoginResultActivity extends AppCompatActivity {
private TextView tv_nickname;
private ImageView iv_profile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_result);
Intent intent = getIntent();
tv_nickname = findViewById(R.id.tv_nickname);
tv_nickname.setText(intent.getStringExtra("nicmName"));
iv_profile = findViewById(R.id.iv_profile);
Glide.with(this).load(intent.getStringExtra("photoUrl")).into(iv_profile); // 프로필 url을 이미지에 셋팅
}
}
728x90
반응형
'Frontend > Android' 카테고리의 다른 글
안드로이드) 리싸이클러뷰(RecyclerView) (0) | 2022.01.05 |
---|---|
안드로이드) 파이어베이스 Realtime Database + 리싸이클러뷰 (0) | 2022.01.04 |
안드로이드) 뒤로가기 2번 누르면 종료되게 하기 (0) | 2021.11.01 |