Skip to content

Commit

Permalink
Implement apple login for iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-sneha-s committed Mar 11, 2024
1 parent d57518b commit bffa054
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 90 deletions.
24 changes: 13 additions & 11 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
plugins {
id "com.android.application"
id "kotlin-android"
id "dev.flutter.flutter-gradle-plugin"
id "com.google.gms.google-services"
id "com.google.firebase.crashlytics"
}


def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
Expand All @@ -6,10 +15,6 @@ if (localPropertiesFile.exists()) {
}
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
Expand All @@ -21,14 +26,11 @@ if (flutterVersionName == null) {
flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'



android {
compileSdkVersion 33//flutter.compileSdkVersion
compileSdkVersion 34//flutter.compileSdkVersion

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
Expand Down Expand Up @@ -100,5 +102,5 @@ flutter {
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.9.0"
}
14 changes: 0 additions & 14 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
buildscript {
ext.kotlin_version = '1.8.21'
repositories {
google()
mavenCentral()
}

dependencies {
classpath 'com.android.tools.build:gradle:7.0.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.3.10'
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.8.1'
}
}

allprojects {
repositories {
Expand Down
33 changes: 25 additions & 8 deletions android/settings.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
include ':app'
pluginManagement {
def flutterSdkPath = {
def properties = new Properties()
file("local.properties").withInputStream { properties.load(it) }
def flutterSdkPath = properties.getProperty("flutter.sdk")
assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
return flutterSdkPath
}()

def localPropertiesFile = new File(rootProject.projectDir, "local.properties")
def properties = new Properties()
includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")

assert localPropertiesFile.exists()
localPropertiesFile.withReader("UTF-8") { reader -> properties.load(reader) }
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
}

def flutterSdkPath = properties.getProperty("flutter.sdk")
assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
apply from: "$flutterSdkPath/packages/flutter_tools/gradle/app_plugin_loader.gradle"
plugins {
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
id "com.android.application" version "7.0.4" apply false
id "org.jetbrains.kotlin.android" version "1.8.21" apply false
id "com.google.gms.google-services" version "4.3.10" apply false
id "com.google.firebase.crashlytics" version "2.9.9" apply false

}

include ":app"
30 changes: 13 additions & 17 deletions lib/data/services/auth_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,20 @@ class AuthService {
return user;
}

Future<Account?> signInWithApple() async {
final rawNounce = generateNonce();
final nounce = sha256ofString(rawNounce);

final appleCredential = await SignInWithApple.getAppleIDCredential(scopes: [
AppleIDAuthorizationScopes.email,
AppleIDAuthorizationScopes.fullName
], nonce: nounce);

final oAuthCredential = firebase_auth.OAuthProvider("apple.com").credential(
idToken: appleCredential.identityToken, rawNonce: rawNounce);

final name = appleCredential.givenName;
final authUser = await signInWithCredentials(oAuthCredential);
if (name != null) {
return await setUser(authUser, name: name);
Future<firebase_auth.User?> signInWithApple() async {
final firebase_auth.UserCredential? credential;

firebase_auth.AppleAuthProvider appleProvider =
firebase_auth.AppleAuthProvider();
if (kIsWeb) {
credential = await firebase_auth.FirebaseAuth.instance
.signInWithPopup(appleProvider);
} else {
credential = await firebase_auth.FirebaseAuth.instance
.signInWithProvider(appleProvider);
}
return await setUser(authUser);

return credential.user;
}

Future<Account?> setUser(firebase_auth.User? authUser, {String? name}) async {
Expand Down
8 changes: 5 additions & 3 deletions lib/ui/sign_in/bloc/sign_in_view_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,18 @@ class SignInBloc extends Bloc<SignInEvent, SignInState> {
AppleSignInEvent event, Emitter<SignInState> emit) async {
try {
emit(state.copyWith(appleSignInLoading: true));
Account? user = await _authService.signInWithApple();
if (user != null) {
firebase_auth.User? authUser = await _authService.signInWithApple();
print(authUser);
if (authUser != null) {
final Account user = await _accountService.getUser(authUser);
await _userStateNotifier.setUser(user);
emit(state.copyWith(appleSignInLoading: false, signInSuccess: true));
} else {
emit(state.copyWith(appleSignInLoading: false));
}
} on Exception catch (e) {
emit(
state.copyWith(appleSignInLoading: false, error: firesbaseAuthError));
state.copyWith(appleSignInLoading: false, error: somethingWentWrongError));
throw Exception(e.toString());
}
}
Expand Down
7 changes: 4 additions & 3 deletions lib/ui/sign_in/sign_in_screen.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
Expand Down Expand Up @@ -84,13 +87,11 @@ class SignInScreenState extends State<SignInScreen> {

),
),



const GoogleSignInButton(),
const SizedBox(
height: 20,
),
if(kIsWeb || Platform.isIOS)
const AppleSignInButton()
],
),
Expand Down
56 changes: 28 additions & 28 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ packages:
dependency: transitive
description:
name: _flutterfire_internals
sha256: "2489c399d8b23a12704bd9e056e3df957c69ceec621f4fc8d69fa7cd5211d1f4"
sha256: "4eec93681221723a686ad580c2e7d960e1017cf1a4e0a263c2573c2c6b0bf5cd"
url: "https://pub.dev"
source: hosted
version: "1.3.24"
version: "1.3.25"
analyzer:
dependency: transitive
description:
Expand Down Expand Up @@ -173,26 +173,26 @@ packages:
dependency: "direct main"
description:
name: cloud_firestore
sha256: f7488f2092c1b33d855ac4bdd46de9a3bb8029c4ea5caebb52dfaf812c90f0a4
sha256: "31cfa4d65d6e9ea837234fffe121304034c30c9214c06207b4a35867e3757900"
url: "https://pub.dev"
source: hosted
version: "4.15.7"
version: "4.15.8"
cloud_firestore_platform_interface:
dependency: transitive
description:
name: cloud_firestore_platform_interface
sha256: adff62e6d3736d8c529a417487d81c5bec1be227e0c04ba95ef64364485aee09
sha256: a0097a26569b015faf8142e159e855241609ea9a1738b5fd1c40bfe8411b41a0
url: "https://pub.dev"
source: hosted
version: "6.1.8"
version: "6.1.9"
cloud_firestore_web:
dependency: transitive
description:
name: cloud_firestore_web
sha256: "73efac045b40263001b9265a375b1bd4032945dc6b888698d8add927dc74b818"
sha256: ed680ece29a5750985119c09cdc276b460c3a2fa80e8c12f9b7241f6b4a7ca16
url: "https://pub.dev"
source: hosted
version: "3.10.7"
version: "3.10.8"
code_builder:
dependency: transitive
description:
Expand Down Expand Up @@ -373,34 +373,34 @@ packages:
dependency: "direct main"
description:
name: firebase_auth
sha256: "8ca05d45c47451586b753e7e9040378e87cdd72220a73d3a343600dd7b138181"
sha256: "17b841e1b000c3441b8ffceca88f468e078d0443db9643e77541bdfb7a3fd16b"
url: "https://pub.dev"
source: hosted
version: "4.17.7"
version: "4.17.8"
firebase_auth_platform_interface:
dependency: transitive
description:
name: firebase_auth_platform_interface
sha256: "9214148dde687d07f8e593d3b95f17fc2282167a98c04765115a1c3119837a5f"
sha256: f294ceef40409a36c819a14280ca864fe487b44033e5276443377c66cb448310
url: "https://pub.dev"
source: hosted
version: "7.1.7"
version: "7.1.8"
firebase_auth_web:
dependency: transitive
description:
name: firebase_auth_web
sha256: "287e858f1fe8a62a4a7b99a47800713f98d41bef24548095e7165f65b4e0e78d"
sha256: "1f231da900fe7ff9f2974f8adcbdb3363c410c24725978afa5dc33e1e7e62e06"
url: "https://pub.dev"
source: hosted
version: "5.9.7"
version: "5.9.8"
firebase_core:
dependency: "direct main"
description:
name: firebase_core
sha256: a746f861618eef57a11d8b658a2db1fa5a336fc82ba65b7763d7e2292687de2b
sha256: "53316975310c8af75a96e365f9fccb67d1c544ef0acdbf0d88bbe30eedd1c4f9"
url: "https://pub.dev"
source: hosted
version: "2.26.0"
version: "2.27.0"
firebase_core_platform_interface:
dependency: transitive
description:
Expand All @@ -421,42 +421,42 @@ packages:
dependency: "direct main"
description:
name: firebase_crashlytics
sha256: "1b871e88921cd3bcf8fadfed32af7adbfecaa7e6a8bf5f44a2e0c1f6310c6842"
sha256: c4f1b723d417bc9c4774810e774ff91df8fb0032d33fb2888b2c887e865581b8
url: "https://pub.dev"
source: hosted
version: "3.4.17"
version: "3.4.18"
firebase_crashlytics_platform_interface:
dependency: transitive
description:
name: firebase_crashlytics_platform_interface
sha256: f8af3f770257fc1ddd6cf1741edcf6cb440124f02e197480b05f26417d48a7b2
sha256: c5a11fca3df76a98e3fa68fde8b10a08aacb9a7639f619fbfd4dad6c67a08643
url: "https://pub.dev"
source: hosted
version: "3.6.24"
version: "3.6.25"
firebase_storage:
dependency: "direct main"
description:
name: firebase_storage
sha256: "8e93e9462f353946c471d5ab6f2eeedcb163fdecb5a8c44b152d962ee9093d40"
sha256: ce1b0efe8dc111058c5f079b2f2ce84906d030d0fd2eef70c42ca1253c67039a
url: "https://pub.dev"
source: hosted
version: "11.6.8"
version: "11.6.9"
firebase_storage_platform_interface:
dependency: transitive
description:
name: firebase_storage_platform_interface
sha256: ca9dbfc61c91c2d918004f8d1eb214171f941c789c1216e94eedbe45c8d29a21
sha256: "4a2b64d4dac096390a0b7f2e7b6d086d0546c4a1bf7ee3fc4b5ae4cc41005c46"
url: "https://pub.dev"
source: hosted
version: "5.1.11"
version: "5.1.12"
firebase_storage_web:
dependency: transitive
description:
name: firebase_storage_web
sha256: "076c336dee318fe32be5bec4eac9682aaf39bd224040113768c74b601650546c"
sha256: "4153814db8d59138e816d9f016736e4095c45675a2c18f2868d11ffd8cc6a4ca"
url: "https://pub.dev"
source: hosted
version: "3.7.2"
version: "3.7.3"
fixnum:
dependency: transitive
description:
Expand Down Expand Up @@ -1510,10 +1510,10 @@ packages:
dependency: transitive
description:
name: win32
sha256: "464f5674532865248444b4c3daca12bd9bf2d7c47f759ce2617986e7229494a8"
sha256: "8cb58b45c47dcb42ab3651533626161d6b67a2921917d8d429791f76972b3480"
url: "https://pub.dev"
source: hosted
version: "5.2.0"
version: "5.3.0"
win32_registry:
dependency: transitive
description:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,13 @@ class MockAuthService extends _i1.Mock implements _i7.AuthService {
) as _i8.Future<_i3.User?>);

@override
_i8.Future<_i5.Account?> signInWithApple() => (super.noSuchMethod(
_i8.Future<_i3.User?> signInWithApple() => (super.noSuchMethod(
Invocation.method(
#signInWithApple,
[],
),
returnValue: _i8.Future<_i5.Account?>.value(),
) as _i8.Future<_i5.Account?>);
returnValue: _i8.Future<_i3.User?>.value(),
) as _i8.Future<_i3.User?>);

@override
_i8.Future<_i5.Account?> setUser(
Expand Down
6 changes: 3 additions & 3 deletions test/unit_test/space/join_space/join_space_test.mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -742,13 +742,13 @@ class MockAuthService extends _i1.Mock implements _i17.AuthService {
) as _i8.Future<_i6.User?>);

@override
_i8.Future<_i5.Account?> signInWithApple() => (super.noSuchMethod(
_i8.Future<_i6.User?> signInWithApple() => (super.noSuchMethod(
Invocation.method(
#signInWithApple,
[],
),
returnValue: _i8.Future<_i5.Account?>.value(),
) as _i8.Future<_i5.Account?>);
returnValue: _i8.Future<_i6.User?>.value(),
) as _i8.Future<_i6.User?>);

@override
_i8.Future<_i5.Account?> setUser(
Expand Down

0 comments on commit bffa054

Please sign in to comment.