Skip to content

Commit

Permalink
Merge pull request #78 from pet-sitter/77-auth-카카오-로그인-구현
Browse files Browse the repository at this point in the history
Feat: [Auth] 카카오 로그인 로직 구현 #77
  • Loading branch information
Yellowtoast authored Feb 14, 2024
2 parents b810835 + a5fad04 commit abfe6e0
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 53 deletions.
2 changes: 2 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<data android:host="kakaolink"
android:scheme="kakaoc731277fe79636328bb7e45a3ffa2b57" />
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
Expand Down
2 changes: 1 addition & 1 deletion lib/core/enums/sns_provider_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:freezed_annotation/freezed_annotation.dart';

enum SnsProviderType {
@JsonValue('kakao')
kakao(providerId: 'kakao.com'),
kakao(providerId: 'oidc.kakao'),
@JsonValue('google')
google(providerId: 'google.com'),
@JsonValue('apple')
Expand Down
37 changes: 15 additions & 22 deletions lib/features/auth/data/remote/auth_remote_data_source_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter/services.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:kakao_flutter_sdk_user/kakao_flutter_sdk_user.dart';
import 'package:pets_next_door_flutter/app/env/flavors.dart';
import 'package:pets_next_door_flutter/core/enums/sns_provider_type.dart';
import 'package:pets_next_door_flutter/core/network_handling/app_dio.dart';
import 'package:pets_next_door_flutter/core/network_handling/exceptions/custom_exception.dart';
import 'package:pets_next_door_flutter/features/auth/api/auth_api.dart';
Expand Down Expand Up @@ -41,14 +42,11 @@ final class AuthRemoteDataSourceImpl implements AuthRemoteDataSource {

@override
Future<UserCredential> signInWithKakao() async {
String customToken;
OAuthToken oAuthToken;

if (await isKakaoTalkInstalled()) {
try {
customToken = await UserApi.instance
.loginWithKakaoAccount()
.then((oAuthToken) => _getFirebaseAuthToken(oAuthToken))
.then((customToken) => customToken);
oAuthToken = await UserApi.instance.loginWithKakaoTalk();
} catch (error) {
// 사용자가 카카오톡 설치 후 디바이스 권한 요청 화면에서 로그인을 취소한 경우,
// 의도적인 로그인 취소로 보고 카카오계정으로 로그인 시도 없이 로그인 취소로 처리 (예: 뒤로 가기)
Expand All @@ -58,37 +56,32 @@ final class AuthRemoteDataSourceImpl implements AuthRemoteDataSource {

// 카카오톡에 연결된 카카오계정이 없는 경우, 카카오계정으로 로그인
try {
customToken = await UserApi.instance
.loginWithKakaoAccount()
.then((oAuthToken) => _getFirebaseAuthToken(oAuthToken))
.then((customToken) => customToken);
oAuthToken = await UserApi.instance.loginWithKakaoAccount();
} catch (error) {
// print('카카오계정으로 로그인 실패 $error');
// TODO: 에러 핸들링 좀 더 꼼꼼하게 필요
throw error;
}
}
} else {
try {
// print('카카오계정으로 로그인 성공');
customToken = await UserApi.instance
.loginWithKakaoAccount()
.then((oAuthToken) => _getFirebaseAuthToken(oAuthToken))
.then((customToken) => customToken);
return _firebaseAuth.signInWithCustomToken(customToken);
oAuthToken = await UserApi.instance.loginWithKakaoAccount();
} catch (error) {
throw error;
}
}
return _firebaseAuth.signInWithCustomToken(customToken);

final credential =
OAuthProvider(SnsProviderType.kakao.providerId).credential(
idToken: oAuthToken.idToken,
);

final userCredential = await _firebaseAuth.signInWithCredential(credential);

return userCredential;
}

@override
Future<void> signOut() {
return _firebaseAuth.signOut();
}

Future<String> _getFirebaseAuthToken(OAuthToken oAuthToken) async {
final customToken = await _authAPI.customToken(oAuthToken.accessToken);
return customToken;
}
}
2 changes: 0 additions & 2 deletions lib/features/auth/repositories/auth_repository.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:kakao_flutter_sdk_user/kakao_flutter_sdk_user.dart';
import 'package:pets_next_door_flutter/core/enums/sns_provider_type.dart';
import 'package:pets_next_door_flutter/core/utils/result.dart';

abstract interface class AuthRepository {
Future<Result<UserCredential>> signInOAuth(SnsProviderType provider);
Future<Result<void>> signOut();
Future<Result<String>> customToken(OAuthToken oAuthToken);
}
7 changes: 0 additions & 7 deletions lib/features/auth/repositories/auth_repository_impl.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:kakao_flutter_sdk_auth/src/model/oauth_token.dart';
import 'package:pets_next_door_flutter/core/enums/sns_provider_type.dart';
import 'package:pets_next_door_flutter/core/utils/result.dart';
import 'package:pets_next_door_flutter/features/auth/data/remote/auth_remote_data_source.dart';
Expand Down Expand Up @@ -39,10 +38,4 @@ final class AuthRepositoryImpl implements AuthRepository {
return Result.failure(e);
}
}

@override
Future<Result<String>> customToken(OAuthToken oAuthToken) {
// TODO: implement customToken
throw UnimplementedError();
}
}
17 changes: 0 additions & 17 deletions lib/features/auth/usecases/get_custom_token_use_case.dart

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ class StartWithKakaoButton extends ConsumerWidget with SignInEvent {
Widget build(BuildContext context, WidgetRef ref) {
return GestureDetector(
onTap: () async {
// TODO: 아직 CustomToken 발급 API가 나오지 않아서 불가능
ToastService.show(NormalToast(message: '현재 카카오 로그인이 불가합니다.'));
// await onTapSignInWithKakao(ref);
await onTapSignInWithKakao(ref);
},
child: SvgPicture.asset(PNDSvgs.kakao),
);
Expand Down
2 changes: 1 addition & 1 deletion lib/presentation/pages/splash/splash_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mixin class SplashEvent implements _SplashEvent {

final userData =
await ref.read(userDataProvider.future).onError((error, stackTrace) {
ref.context.goNamed(AppRoute.signIn.name);
return null;
});

if (userData != null) {
Expand Down

0 comments on commit abfe6e0

Please sign in to comment.