Skip to content

Commit

Permalink
Merge pull request #259 from Eldar2021/ai/apple-signin
Browse files Browse the repository at this point in the history
Integrate sign in with apple
  • Loading branch information
Eldar2021 authored Nov 28, 2024
2 parents b973ddd + 52936cd commit 6e59280
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 43 deletions.
2 changes: 1 addition & 1 deletion app/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -606,4 +606,4 @@
/* End XCConfigurationList section */
};
rootObject = 97C146E61CF9000F007C117D /* Project object */;
}
}
84 changes: 53 additions & 31 deletions app/lib/app/data/source/remote/auth_remote_data_source_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:meta/meta.dart';
import 'package:mq_either/mq_either.dart';
import 'package:mq_remote_client/mq_remote_client.dart';
import 'package:mq_storage/mq_storage.dart';

import 'package:my_quran/app/app.dart';
import 'package:my_quran/config/config.dart';
import 'package:my_quran/constants/contants.dart';
Expand Down Expand Up @@ -36,9 +37,7 @@ final class AuthRemoteDataSourceImpl implements AuthRemoteDataSource {
);

return token.fold(
(error) {
return Left(error);
},
Left.new,
(r) async {
final user = UserModelResponse(
accessToken: r.key,
Expand All @@ -47,7 +46,10 @@ final class AuthRemoteDataSourceImpl implements AuthRemoteDataSource {
localeCode: languageCode,
);

await storage.writeString(key: StorageKeys.tokenKey, value: user.accessToken);
await storage.writeString(
key: StorageKeys.tokenKey,
value: user.accessToken,
);

return Right(user);
},
Expand Down Expand Up @@ -83,18 +85,24 @@ final class AuthRemoteDataSourceImpl implements AuthRemoteDataSource {
body: {'access_token': googleAuth.accessToken},
);

return token.fold(Left.new, (r) async {
final user = UserModelResponse(
accessToken: r.key,
username: googleAuth.name,
gender: gender,
localeCode: languageCode,
);

await storage.writeString(key: StorageKeys.tokenKey, value: user.accessToken);

return Right(user);
});
return token.fold(
Left.new,
(r) async {
final user = UserModelResponse(
accessToken: r.key,
username: googleAuth.name,
gender: gender,
localeCode: languageCode,
);

await storage.writeString(
key: StorageKeys.tokenKey,
value: user.accessToken,
);

return Right(user);
},
);
}

Future<_UserReqParam> _getGoogleAuth() async {
Expand Down Expand Up @@ -124,21 +132,30 @@ final class AuthRemoteDataSourceImpl implements AuthRemoteDataSource {
final token = await client.postType(
apiConst.loginWithApple,
fromJson: TokenResponse.fromJson,
body: {'access_token': appleAuth.accessToken},
body: {
'access_token': appleAuth.accessToken,
'id_token': appleAuth.identityToken,
},
);

return token.fold(Left.new, (r) async {
final user = UserModelResponse(
accessToken: r.key,
username: appleAuth.name,
gender: gender,
localeCode: languageCode,
);

await storage.writeString(key: StorageKeys.tokenKey, value: user.accessToken);

return Right(user);
});
return token.fold(
Left.new,
(r) async {
final user = UserModelResponse(
accessToken: r.key,
username: appleAuth.name,
gender: gender,
localeCode: languageCode,
);

await storage.writeString(
key: StorageKeys.tokenKey,
value: user.accessToken,
);

return Right(user);
},
);
}

Future<_UserReqParam> _getAppleAuth() async {
Expand All @@ -149,11 +166,14 @@ final class AuthRemoteDataSourceImpl implements AuthRemoteDataSource {
);
} else {
final appleAuth = await soccialAuth.signInWithApple();
final accessToken = appleAuth.credential?.accessToken ?? '';
final username = appleAuth.user?.displayName ?? '';
final identityToken = appleAuth.$2.identityToken ?? '';
final accessToken = appleAuth.$1.credential!.accessToken ?? '';
final username = appleAuth.$1.user?.displayName ?? '';

return _UserReqParam(
name: username,
accessToken: accessToken,
identityToken: identityToken,
);
}
}
Expand Down Expand Up @@ -212,8 +232,10 @@ final class _UserReqParam {
const _UserReqParam({
required this.name,
required this.accessToken,
this.identityToken,
});

final String name;
final String accessToken;
final String? identityToken;
}
12 changes: 7 additions & 5 deletions app/lib/core/auth/soccial_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,22 @@ class SoccialAuth {
}
}

Future<UserCredential> signInWithApple() async {
Future<(UserCredential, AuthorizationCredentialAppleID)> signInWithApple() async {
try {
final appleCredential = await SignInWithApple.getAppleIDCredential(
final credential = await SignInWithApple.getAppleIDCredential(
scopes: [
AppleIDAuthorizationScopes.email,
AppleIDAuthorizationScopes.fullName,
],
);

final oauthCredential = OAuthProvider('apple.com').credential(
idToken: appleCredential.identityToken,
accessToken: appleCredential.authorizationCode,
idToken: credential.identityToken,
accessToken: credential.authorizationCode,
);

final userCredential = await FirebaseAuth.instance.signInWithCredential(oauthCredential);
return userCredential;
return (userCredential, credential);
} catch (e, s) {
MqCrashlytics.report(e, s);
rethrow;
Expand Down
26 changes: 21 additions & 5 deletions app/lib/modules/login/presentation/view/sign_in_view.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import 'dart:async';
import 'dart:developer';
import 'dart:io';

import 'package:sign_in_with_apple/sign_in_with_apple.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import 'package:loader_overlay/loader_overlay.dart';
import 'package:mq_ci_keys/mq_ci_keys.dart';

import 'package:my_quran/app/app.dart';
import 'package:my_quran/components/components.dart';

import 'package:my_quran/config/config.dart';
import 'package:my_quran/constants/contants.dart';
import 'package:my_quran/core/core.dart';
Expand Down Expand Up @@ -98,9 +100,7 @@ class _SignInViewState extends State<SignInView> {
text: context.l10n.signIn,
onPressed: () {
if (formKey.currentState!.validate()) {
MqAnalytic.track(
AnalyticKey.goVerificationOtp,
);
MqAnalytic.track(AnalyticKey.goVerificationOtp);
try {
unawaited(AppAlert.showLoading(context));
context.read<AuthCubit>().loginWithEmail(emailController.text);
Expand Down Expand Up @@ -146,11 +146,27 @@ class _SignInViewState extends State<SignInView> {
children: [
Assets.icons.googleIcon.svg(height: 25),
const SizedBox(width: 10),
Text(context.l10n.google, style: context.bodyMedium),
Text(context.l10n.google, style: const TextStyle(fontSize: 18)),
],
),
),
),
const SizedBox(height: 16),
if (Platform.isIOS)
SignInWithAppleButton(
key: Key(MqKeys.loginTypeName('apple')),
text: 'Apple',
height: 50,
onPressed: () async {
MqAnalytic.track(
AnalyticKey.tapLogin,
params: {'soccial': 'apple'},
);
unawaited(AppAlert.showLoading(context));
await context.read<AuthCubit>().signInWithApple();
if (context.mounted) context.loaderOverlay.hide();
},
),
const SizedBox(height: 40),
TextButton(
onPressed: () {
Expand Down
2 changes: 1 addition & 1 deletion app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: A new Flutter project.

publish_to: "none"

version: 1.3.3+12
version: 1.3.3+13

environment:
sdk: ">=3.5.0 <4.0.0"
Expand Down

0 comments on commit 6e59280

Please sign in to comment.