Untitled
unknown
plain_text
9 months ago
2.1 kB
3
Indexable
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:sign_in_with_apple/sign_in_with_apple.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:logger/logger.dart';
import 'package:tamworth/core/util/get_storage.dart';
import 'package:tamworth/feature/login/data/model/social_login_post_model.dart';
import 'package:tamworth/feature/login/presentation/cubit/social_login_cubit.dart';
class AppleSignInService {
Future<bool> signInWithApple(BuildContext context) async {
try {
final credential = await SignInWithApple.getAppleIDCredential(
scopes: [
AppleIDAuthorizationScopes.email,
AppleIDAuthorizationScopes.fullName,
],
);
if (context.mounted) {
await loginWithAppleAccount(credential, context);
return true;
}
return false;
} catch (error) {
debugPrint("Error signing in with Apple: $error");
return false;
}
}
Future<void> loginWithAppleAccount(
AuthorizationCredentialAppleID credential, BuildContext context) async {
// Store user email and name
if (credential.email != null) {
GetSetStorage().setEmail(credential.email!);
}
final String fullName = [credential.givenName, credential.familyName]
.where((name) => name != null)
.join(' ');
if (fullName.isNotEmpty) {
GetSetStorage().setUsername(fullName);
}
final String? idToken = credential.identityToken;
if (idToken != null) {
final SocialLoginPostModel socialLoginPostModel = SocialLoginPostModel(
provider: "apple",
token: idToken,
deviceToken: GetSetStorage().getFcmToken(),
deviceType: Platform.isAndroid
? "Android"
: Platform.isIOS
? "IOS"
: "",
);
Logger().d(socialLoginPostModel.toMap());
if (context.mounted) {
context.read<SocialLoginCubit>().socialLogin(socialLoginPostModel);
}
} else {
debugPrint('Error: idToken is null');
}
}
}
Editor is loading...
Leave a Comment