11import 'dart:async' ;
22
33import 'package:dio/dio.dart' ;
4- import 'package:flutter/foundation.dart' ;
5- import 'package:google_sign_in/google_sign_in.dart' ;
64import 'package:injectable/injectable.dart' ;
75import 'package:on_time_front/core/logging/app_logger.dart' ;
6+ import 'package:on_time_front/core/services/google_authentication_service.dart' ;
87import 'package:on_time_front/core/validation/backend_constraints.dart' ;
98import 'package:on_time_front/data/data_sources/authentication_remote_data_source.dart' ;
109import 'package:on_time_front/data/data_sources/token_local_data_source.dart' ;
1110import 'package:on_time_front/data/models/sign_in_with_google_request_model.dart' ;
1211import 'package:on_time_front/data/models/sign_in_with_apple_request_model.dart' ;
12+ import 'package:on_time_front/domain/entities/google_auth_credential.dart' ;
1313import 'package:on_time_front/domain/entities/user_entity.dart' ;
1414import 'package:on_time_front/domain/repositories/user_repository.dart' ;
1515import 'package:rxdart/subjects.dart' ;
1616
1717@Singleton (as : UserRepository )
1818class UserRepositoryImpl implements UserRepository {
19- static const _googleIosClientId =
20- '456571312261-r35ah9qi0qaq7al007e2db0e0jmjcmb4.apps.googleusercontent.com' ;
21- static const _googleServerClientId =
22- '456571312261-5kuf2r6i5i7lqjr7qealv06sdgkn3hcp.apps.googleusercontent.com' ;
23- static const _googleScopes = ['email' , 'profile' ];
24-
2519 final AuthenticationRemoteDataSource _authenticationRemoteDataSource;
2620 final TokenLocalDataSource _tokenLocalDataSource;
27- final GoogleSignIn _googleSignIn = GoogleSignIn .instance;
28- Future <void >? _googleSignInInitialization;
21+ final GoogleAuthenticationService _googleAuthenticationService;
2922 late final _userStreamController = BehaviorSubject <UserEntity >.seeded (
3023 const UserEntity .empty (),
3124 );
3225
33- @override
34- Stream <GoogleSignInAuthenticationEvent > get googleAuthenticationEvents =>
35- _googleSignIn.authenticationEvents;
36-
37- @override
38- bool get supportsGoogleAuthenticate => _googleSignIn.supportsAuthenticate ();
39-
4026 UserRepositoryImpl (
4127 this ._authenticationRemoteDataSource,
4228 this ._tokenLocalDataSource,
29+ this ._googleAuthenticationService,
4330 );
4431
45- @override
46- Future <void > initializeGoogleSignIn () {
47- return _googleSignInInitialization ?? = _initializeGoogleSignIn ();
48- }
49-
50- Future <void > _initializeGoogleSignIn () async {
51- await _googleSignIn.initialize (
52- clientId: _googleClientId,
53- serverClientId: _googleServerClientId,
54- );
55- }
56-
57- @override
58- Future <GoogleSignInAccount > authenticateWithGoogle () async {
59- await initializeGoogleSignIn ();
60- return _googleSignIn.authenticate (scopeHint: _googleScopes);
61- }
62-
6332 @override
6433 Future <UserEntity > getUser () async {
6534 try {
@@ -122,16 +91,14 @@ class UserRepositoryImpl implements UserRepository {
12291 }
12392
12493 @override
125- Future <void > signInWithGoogle (GoogleSignInAccount googleUser ) async {
94+ Future <void > signInWithGoogle (GoogleAuthCredential credential ) async {
12695 try {
127- final GoogleSignInAuthentication googleAuth = googleUser.authentication;
128- final String ? idToken = googleAuth.idToken;
129- if (idToken == null ) {
96+ if (credential.idToken.isEmpty) {
13097 throw Exception ('Google ID Token is null' );
13198 }
13299 final signInWithGoogleRequestModel = SignInWithGoogleRequestModel (
133- idToken: idToken,
134- refreshToken: '' ,
100+ idToken: credential. idToken,
101+ refreshToken: credential.refreshToken ,
135102 );
136103 await _tokenLocalDataSource.deleteToken ();
137104 final result = await _authenticationRemoteDataSource.signInWithGoogle (
@@ -225,8 +192,7 @@ class UserRepositoryImpl implements UserRepository {
225192 @override
226193 Future <void > disconnectGoogleSignIn () async {
227194 try {
228- await _googleSignIn.disconnect ();
229- AppLogger .debug ('Google Sign-In disconnected' );
195+ await _googleAuthenticationService.disconnect ();
230196 } catch (error) {
231197 AppLogger .debug (
232198 'Google Sign-In disconnect failed errorType=${error .runtimeType }' ,
@@ -237,11 +203,4 @@ class UserRepositoryImpl implements UserRepository {
237203 @override
238204 Stream <UserEntity > get userStream =>
239205 _userStreamController.asBroadcastStream ();
240-
241- String ? get _googleClientId {
242- if (kIsWeb) return null ;
243- return defaultTargetPlatform == TargetPlatform .iOS
244- ? _googleIosClientId
245- : null ;
246- }
247206}
0 commit comments