-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refact: Google Login, Social Login 로직을 util로 분리
- Loading branch information
Showing
4 changed files
with
114 additions
and
68 deletions.
There are no files selected for viewing
28 changes: 27 additions & 1 deletion
28
frontend/ARchive/app/src/main/java/com/droidblossom/archive/ARchiveApplication.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,46 @@ | ||
package com.droidblossom.archive | ||
|
||
import android.app.Application | ||
import android.content.pm.PackageInfo | ||
import android.content.pm.PackageManager | ||
import android.util.Base64 | ||
import android.util.Log | ||
import com.droidblossom.archive.util.AppSignatureHelper | ||
import com.kakao.sdk.common.KakaoSdk | ||
import dagger.hilt.android.HiltAndroidApp | ||
import java.security.MessageDigest | ||
import java.security.NoSuchAlgorithmException | ||
|
||
|
||
@HiltAndroidApp | ||
class ARchiveApplication : Application() { | ||
override fun onCreate() { | ||
super.onCreate() | ||
// 키 값 알아내기 | ||
// AppSignatureHelper(this@ARchiveApplication).apply { | ||
// Log.d("hash", "hash : ${appSignature}") | ||
// Log.d("key1", "hash : ${appSignature}") | ||
// } | ||
// getHashKey() | ||
// Kakao Sdk 초기화 | ||
KakaoSdk.init(this, BuildConfig.KAKAO_NATIVE_APP_KEY) | ||
} | ||
private fun getHashKey() { | ||
// | ||
var packageInfo: PackageInfo? = null | ||
try { | ||
packageInfo = packageManager.getPackageInfo(packageName, PackageManager.GET_SIGNATURES) | ||
} catch (e: PackageManager.NameNotFoundException) { | ||
e.printStackTrace() | ||
} | ||
if (packageInfo == null) Log.e("KeyHash", "KeyHash:null") | ||
for (signature in packageInfo!!.signatures) { | ||
try { | ||
val md = MessageDigest.getInstance("SHA") | ||
md.update(signature.toByteArray()) | ||
Log.d("key2", Base64.encodeToString(md.digest(), Base64.DEFAULT)) | ||
} catch (e: NoSuchAlgorithmException) { | ||
Log.e("key3", "Unable to get MessageDigest. signature=$signature", e) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
frontend/ARchive/app/src/main/java/com/droidblossom/di/SocialLoginUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.droidblossom.di | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.util.Log | ||
import com.droidblossom.archive.presentation.ui.auth.AuthViewModel | ||
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.Task | ||
import com.kakao.sdk.user.UserApiClient | ||
|
||
class SocialLoginUtil(private val context: Context, private val callback: LoginCallback) { | ||
|
||
interface LoginCallback { | ||
fun onLoginSuccess(social: AuthViewModel.Social) | ||
fun onLoginFailure(error: Throwable) | ||
} | ||
|
||
private val googleSignInClient: GoogleSignInClient by lazy { | ||
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) | ||
.requestEmail() | ||
.build() | ||
GoogleSignIn.getClient(context, gso) | ||
} | ||
|
||
fun kakaoSignIn() { | ||
if (UserApiClient.instance.isKakaoTalkLoginAvailable(context)) { | ||
UserApiClient.instance.loginWithKakaoTalk(context) { token, error -> | ||
if (error != null) { | ||
Log.e("카카오", "로그인 실패 $error") | ||
callback.onLoginFailure(error) | ||
} else if (token != null) { | ||
//Log.e("카카오", "로그인 성공 ${token.accessToken}") | ||
callback.onLoginSuccess(AuthViewModel.Social.KAKAO) | ||
} | ||
} | ||
} else { | ||
UserApiClient.instance.loginWithKakaoAccount(context) { token, error -> | ||
if (error != null) { | ||
Log.e("카카오", "로그인 실패 $error") | ||
callback.onLoginFailure(error) | ||
} else if (token != null) { | ||
//Log.e("카카오", "로그인 성공 ${token.accessToken}") | ||
callback.onLoginSuccess(AuthViewModel.Social.KAKAO) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun googleSignIn(): Intent { | ||
return googleSignInClient.signInIntent | ||
} | ||
|
||
fun handleGoogleSignInResult(completedTask: Task<GoogleSignInAccount>) { | ||
try { | ||
val account = completedTask.getResult(ApiException::class.java) | ||
//Log.d("구글", "로그인 성공: ${account.idToken}") | ||
callback.onLoginSuccess(AuthViewModel.Social.GOOGLE) | ||
} catch (e: ApiException) { | ||
//Log.w("구글", "signInResult:failed code=" + e.statusCode) | ||
callback.onLoginFailure(e) | ||
} | ||
} | ||
} |