Skip to content

Commit

Permalink
feat: update validateVerificationCode to return user and idToken, enh…
Browse files Browse the repository at this point in the history
…ance onSuccess callback
  • Loading branch information
sharjeelyunus committed Jan 13, 2025
1 parent 84d402c commit 33abb10
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
15 changes: 10 additions & 5 deletions modules/auth/lib/signin/auth_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 10 additions & 3 deletions modules/auth/lib/signin/sign_in_with_verification_code.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class SignInWithVerificationCode {
}

/// Verifies the phone code with the provided [smsCode] and [verificationId].
Future<AuthenticatedUser?> validateVerificationCode({
Future<Map<String, dynamic>?> validateVerificationCode({
required String provider,
required String method,
required String smsCode,
Expand Down Expand Up @@ -107,7 +107,7 @@ class SignInWithVerificationCode {
}

/// Validate the phone code using Firebase's [signInWithCredential].
Future<AuthenticatedUser?> _validateFirebaseVerificationCode({
Future<Map<String, dynamic>?> _validateFirebaseVerificationCode({
required String smsCode,
required String verificationId,
}) async {
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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},
);
},
);
Expand Down
4 changes: 2 additions & 2 deletions modules/ensemble/lib/framework/stub/auth_context_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

Expand Down Expand Up @@ -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.');
Expand Down

0 comments on commit 33abb10

Please sign in to comment.