diff --git a/modules/auth/lib/signin/auth_manager.dart b/modules/auth/lib/signin/auth_manager.dart index c7debbf62..558219863 100644 --- a/modules/auth/lib/signin/auth_manager.dart +++ b/modules/auth/lib/signin/auth_manager.dart @@ -464,31 +464,36 @@ class AuthContextManagerImpl with Invokable implements AuthContextManager { required String method, required String smsCode, required String verificationId, - required Function(AuthenticatedUser) onSuccess, + required Function(AuthenticatedUser, String idToken) onSuccess, required Function(String) onError, required Function(String) onVerificationFailure, }) async { try { final SignInWithVerificationCode _signInWithVerificationCode = SignInWithVerificationCode(); - final user = await _signInWithVerificationCode.validateVerificationCode( + final response = + await _signInWithVerificationCode.validateVerificationCode( provider: provider, method: method, smsCode: smsCode, verificationId: verificationId, ); - if (user != null) { + if (response != null) { + final user = response['user']; + final idToken = response['idToken']; + await AuthManager()._updateCurrentUser( Utils.globalAppKey.currentContext!, user, ); - onSuccess(user); + + onSuccess(user, idToken); } else { onError('Something went wrong, User not found'); } - return user; + return response?['user']; } catch (e) { onVerificationFailure('Error verifying phone code: ${e.toString()}'); return null; diff --git a/modules/auth/lib/signin/sign_in_with_verification_code.dart b/modules/auth/lib/signin/sign_in_with_verification_code.dart index 33de7de87..3402cd2a8 100644 --- a/modules/auth/lib/signin/sign_in_with_verification_code.dart +++ b/modules/auth/lib/signin/sign_in_with_verification_code.dart @@ -34,7 +34,7 @@ class SignInWithVerificationCode { } /// Verifies the phone code with the provided [smsCode] and [verificationId]. - Future validateVerificationCode({ + Future?> validateVerificationCode({ required String provider, required String method, required String smsCode, @@ -107,7 +107,7 @@ class SignInWithVerificationCode { } /// Validate the phone code using Firebase's [signInWithCredential]. - Future _validateFirebaseVerificationCode({ + Future?> _validateFirebaseVerificationCode({ required String smsCode, required String verificationId, }) async { @@ -126,11 +126,18 @@ class SignInWithVerificationCode { ); } - return AuthenticatedUser( + final String? idToken = await userCredential.user!.getIdToken(); + + final authenticatedUser = AuthenticatedUser( id: userCredential.user!.uid, phoneNumber: userCredential.user!.phoneNumber ?? '', provider: SignInProvider.firebase, ); + + return { + 'user': authenticatedUser, + 'idToken': idToken, + }; } catch (e) { rethrow; } diff --git a/modules/ensemble/lib/action/sign_in_with_verification_code_actions.dart b/modules/ensemble/lib/action/sign_in_with_verification_code_actions.dart index b550098f0..31a780c34 100644 --- a/modules/ensemble/lib/action/sign_in_with_verification_code_actions.dart +++ b/modules/ensemble/lib/action/sign_in_with_verification_code_actions.dart @@ -198,13 +198,13 @@ class ValidateVerificationCodeAction extends EnsembleAction { {'error': error}, ); }, - onSuccess: (AuthenticatedUser user) { + onSuccess: (AuthenticatedUser user, String idToken) { _triggerEventAction( context, onSuccess, initiator, 'onSuccess', - {'user': user}, + {'user': user, 'idToken': idToken}, ); }, ); diff --git a/modules/ensemble/lib/framework/stub/auth_context_manager.dart b/modules/ensemble/lib/framework/stub/auth_context_manager.dart index 6083269be..8fce6f4d9 100644 --- a/modules/ensemble/lib/framework/stub/auth_context_manager.dart +++ b/modules/ensemble/lib/framework/stub/auth_context_manager.dart @@ -19,7 +19,7 @@ abstract class AuthContextManager { required String verificationId, required String smsCode, required Function(String error) onError, - required Function(AuthenticatedUser user) onSuccess, + required Function(AuthenticatedUser, String idToken) onSuccess, required Function(String) onVerificationFailure, }); @@ -66,7 +66,7 @@ class AuthContextManagerStub implements AuthContextManager { required String verificationId, required String smsCode, required Function(String error) onError, - required Function(AuthenticatedUser user) onSuccess, + required Function(AuthenticatedUser, String idToken) onSuccess, required Function(String) onVerificationFailure, }) { throw RuntimeError('Auth is not enabled.');