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 bffa054 commit 6f90b09
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 97 deletions.
2 changes: 1 addition & 1 deletion lib/data/services/account_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class AccountService {
user = Account(
uid: authData.uid,
email: authData.email!,
name:name?? authData.displayName);
name: name ?? authData.displayName);
await _accountsDb.doc(authData.uid).set(user);
}
await _setUserSession(authData.uid);
Expand Down
16 changes: 0 additions & 16 deletions lib/data/services/auth_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ import 'dart:convert';
import 'dart:math';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:crypto/crypto.dart';
import 'package:firebase_auth/firebase_auth.dart' as firebase_auth;
import 'package:flutter/foundation.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:injectable/injectable.dart';
import 'package:oauth2/oauth2.dart';
import 'package:projectunity/data/services/account_service.dart';
import 'package:sign_in_with_apple/sign_in_with_apple.dart';
import '../model/account/account.dart';
import '../state_manager/auth/desktop/desktop_auth_manager.dart';

Expand Down Expand Up @@ -121,20 +119,6 @@ class AuthService {
return null;
}

String generateNounce([int length = 32]) {
const charset =
'0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._';
final random = Random.secure();
return List.generate(length, (_) => charset[random.nextInt(charset.length)])
.join();
}

String sha256ofString(String input) {
final bytes = utf8.encode(input);
final digest = sha256.convert(bytes);
return digest.toString();
}

Future<bool> signOut() async {
try {
await firebaseAuth.signOut();
Expand Down
3 changes: 1 addition & 2 deletions lib/style/other/smart_scroll_view.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

// A scrollview that supports Spacer to fill the remaining space
import 'package:flutter/cupertino.dart';

Expand Down Expand Up @@ -33,4 +32,4 @@ class SmartScrollView extends StatelessWidget {
},
);
}
}
}
2 changes: 1 addition & 1 deletion lib/ui/admin/members/list/member_list_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class HeaderDelegate extends SliverPersistentHeaderDelegate {
}

@override
double get maxExtent => 60;
double get maxExtent => 80;

@override
double get minExtent => 60;
Expand Down
15 changes: 2 additions & 13 deletions lib/ui/sign_in/bloc/sign_in_view_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
import 'dart:async';
import 'dart:convert';
import 'dart:math';

import 'package:crypto/crypto.dart';
import 'package:firebase_auth/firebase_auth.dart' as firebase_auth;
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:injectable/injectable.dart';
import 'package:projectunity/data/services/account_service.dart';
import 'package:projectunity/ui/sign_in/widget/apple_signin_button.dart';
import '../../../data/core/exception/error_const.dart';
import '../../../data/model/account/account.dart';
import '../../../data/provider/user_state.dart';
import '../../../data/services/auth_service.dart';
import 'sign_in_view_event.dart';
import 'sign_in_view_state.dart';
import 'package:sign_in_with_apple/sign_in_with_apple.dart';

@Injectable()
class SignInBloc extends Bloc<SignInEvent, SignInState> {
Expand All @@ -31,11 +25,6 @@ class SignInBloc extends Bloc<SignInEvent, SignInState> {
on<AppleSignInEvent>(_appleSignIn);
}

Future<void> check(Emitter<SignInState> emit) async {
final isAvailable = await SignInWithApple.isAvailable();
emit(state.copyWith(appleSignInAvailable: isAvailable));
}

Future<void> _googleSignIn(
SignInEvent event, Emitter<SignInState> emit) async {
try {
Expand Down Expand Up @@ -68,8 +57,8 @@ class SignInBloc extends Bloc<SignInEvent, SignInState> {
emit(state.copyWith(appleSignInLoading: false));
}
} on Exception catch (e) {
emit(
state.copyWith(appleSignInLoading: false, error: somethingWentWrongError));
emit(state.copyWith(
appleSignInLoading: false, error: somethingWentWrongError));
throw Exception(e.toString());
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/ui/sign_in/bloc/sign_in_view_event.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:equatable/equatable.dart';

abstract class SignInEvent{}
abstract class SignInEvent {}

class GoogleSignInEvent extends SignInEvent {}
class AppleSignInEvent extends SignInEvent {}

class AppleSignInEvent extends SignInEvent {}
18 changes: 7 additions & 11 deletions lib/ui/sign_in/bloc/sign_in_view_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ import 'package:freezed_annotation/freezed_annotation.dart';

part "sign_in_view_state.freezed.dart";


@freezed
class SignInState with _$SignInState{
const factory SignInState({
@Default(false) appleSignInAvailable,
@Default(false) googleSignInLoading,
@Default(false) appleSignInLoading,
@Default(false) signInSuccess,
String? error
}) = _SignInState;
class SignInState with _$SignInState {
const factory SignInState(
{@Default(false) appleSignInAvailable,
@Default(false) googleSignInLoading,
@Default(false) appleSignInLoading,
@Default(false) signInSuccess,
String? error}) = _SignInState;
}


22 changes: 11 additions & 11 deletions lib/ui/sign_in/sign_in_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class SignInScreenState extends State<SignInScreen> {
return AppPage(
backGroundColor: context.colorScheme.surface,
body: BlocListener<SignInBloc, SignInState>(
listenWhen: (previous,current)=>current.error != null,
listenWhen: (previous, current) => current.error != null,
listener: (context, state) {
if (state.error != null) {
showSnackBar(context: context, error: state.error);
Expand All @@ -58,10 +58,13 @@ class SignInScreenState extends State<SignInScreen> {
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset(ImageConst.loginPageVectorImage,
fit: BoxFit.cover,
),
const SizedBox(height: 50,),
Image.asset(
ImageConst.loginPageVectorImage,
fit: BoxFit.cover,
),
const SizedBox(
height: 50,
),
Text(
context.l10n.sign_in_title_text,
textAlign: TextAlign.center,
Expand All @@ -76,23 +79,20 @@ class SignInScreenState extends State<SignInScreen> {
left: 20.0, right: 20, top: 20, bottom: 40),
child: Text(
context.l10n.sign_in_description_text,
style: AppTextStyle.style16
.copyWith(color: context.colorScheme.textSecondary),
style: AppTextStyle.style16.copyWith(
color: context.colorScheme.textSecondary),
overflow: TextOverflow.fade,
textAlign: TextAlign.center,
),
),

],

),
),
const GoogleSignInButton(),
const SizedBox(
height: 20,
),
if(kIsWeb || Platform.isIOS)
const AppleSignInButton()
if (kIsWeb || Platform.isIOS) const AppleSignInButton()
],
),
),
Expand Down
7 changes: 4 additions & 3 deletions lib/ui/sign_in/widget/apple_signin_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class AppleSignInButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocBuilder<SignInBloc, SignInState>(
buildWhen: (previous, current) => previous.appleSignInLoading!=current.appleSignInLoading,
buildWhen: (previous, current) =>
previous.appleSignInLoading != current.appleSignInLoading,
builder: (context, state) {
return AppButton(
onTap: () => context.read<SignInBloc>().add(AppleSignInEvent()),
Expand All @@ -26,7 +27,7 @@ class AppleSignInButton extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
padding: const EdgeInsets.all(5),
padding: const EdgeInsets.all(5),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: context.colorScheme.surface),
Expand All @@ -48,4 +49,4 @@ class AppleSignInButton extends StatelessWidget {
);
});
}
}
}
7 changes: 4 additions & 3 deletions lib/ui/sign_in/widget/google_signin_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class GoogleSignInButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocBuilder<SignInBloc, SignInState>(
buildWhen: (previous, current) => previous.googleSignInLoading != current.googleSignInLoading,
buildWhen: (previous, current) =>
previous.googleSignInLoading != current.googleSignInLoading,
builder: (context, state) {
return AppButton(
onTap: () => context.read<SignInBloc>().add(GoogleSignInEvent()),
Expand All @@ -27,11 +28,11 @@ class GoogleSignInButton extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
padding: const EdgeInsets.all(5),
padding: const EdgeInsets.all(5),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: context.colorScheme.surface),
child: SvgPicture.asset(
child: SvgPicture.asset(
Assets.images.googleLogo,
)),
const SizedBox(
Expand Down
28 changes: 2 additions & 26 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ packages:
source: hosted
version: "0.3.3+8"
crypto:
dependency: "direct main"
dependency: transitive
description:
name: crypto
sha256: ff625774173754681d66daaf4a448684fb04b78f902da9cb3d308c19cc5e8bab
Expand Down Expand Up @@ -1189,30 +1189,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.4"
sign_in_with_apple:
dependency: "direct main"
description:
name: sign_in_with_apple
sha256: "3dcc472f6d3ef6915828b4aa536da14df20a6d1fee65c02eaa7e75b5c4f3f599"
url: "https://pub.dev"
source: hosted
version: "6.0.0"
sign_in_with_apple_platform_interface:
dependency: transitive
description:
name: sign_in_with_apple_platform_interface
sha256: a5883edee09ed6be19de19e7d9f618a617fe41a6fa03f76d082dfb787e9ea18d
url: "https://pub.dev"
source: hosted
version: "1.0.0"
sign_in_with_apple_web:
dependency: transitive
description:
name: sign_in_with_apple_web
sha256: ffe74793bc31e7e7cc7e149fe14711a59e50a9cbbb3198e1f67fda92492f7b54
url: "https://pub.dev"
source: hosted
version: "2.0.0"
simple_gesture_detector:
dependency: transitive
description:
Expand Down Expand Up @@ -1556,4 +1532,4 @@ packages:
version: "3.1.2"
sdks:
dart: ">=3.3.0 <4.0.0"
flutter: ">=3.19.1"
flutter: ">=3.19.0"
2 changes: 0 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ dependencies:
cloud_firestore: ^4.5.2
collection: ^1.17.0
connectivity_plus: ^5.0.2
crypto:
device_info_plus: ^9.1.2
equatable: ^2.0.5
freezed: ^2.4.7
Expand All @@ -45,7 +44,6 @@ dependencies:
package_info_plus: ^5.0.1
rxdart: ^0.27.7
shared_preferences: ^2.1.1
sign_in_with_apple: ^6.0.0
sticky_headers: ^0.3.0+2
table_calendar: ^3.0.7
url_launcher: ^6.1.11
Expand Down
3 changes: 1 addition & 2 deletions test/unit_test/authentication/login/login_bloc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ void main() {
bloc = SignInBloc(userStateNotifier, authService, accountService);
});


group("Log in with google test", () {
test("Set initial state on cancel login test", () async {
when(authService.signInWithGoogle()).thenAnswer((_) async => null);
Expand All @@ -59,7 +58,7 @@ void main() {
bloc.stream,
emitsInOrder([
const SignInState(googleSignInLoading: true),
const SignInState(googleSignInLoading: false,signInSuccess: true),
const SignInState(googleSignInLoading: false, signInSuccess: true),
]));
await untilCalled(userStateNotifier.setUser(user));
verify(userStateNotifier.setUser(user)).called(1);
Expand Down
6 changes: 2 additions & 4 deletions test/unit_test/space/join_space/join_space_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,7 @@ void main() {
});
group("sign out test ", () {
test("sign out successful test with navigation test", () async {
when(authService.signOut())
.thenAnswer((_) => Future(() => true));
when(authService.signOut()).thenAnswer((_) => Future(() => true));
bloc.add(SignOutEvent());
expect(
bloc.stream,
Expand All @@ -244,8 +243,7 @@ void main() {
});

test("sign out failure test", () {
when(authService.signOut())
.thenAnswer((_) => Future(() => false));
when(authService.signOut()).thenAnswer((_) => Future(() => false));
bloc.add(SignOutEvent());
expect(
bloc.stream,
Expand Down

0 comments on commit 6f90b09

Please sign in to comment.