please i am stuck with my auth flow in flutter after my confirm registration with aws amplify . The idea is to create a user exactly after the user has completed his registration by retrieving the userid and creating a user with the id but it keeps returning null even after the registration.
In summary what i am trying to archieve is to not return null after the user as confirmed registration. please if anyone have a solution or have series of better ideas i am really open to anything that would work.
this is my repository below.
@override
Future<String?> get user async {
try {
final awsUser = await Amplify.Auth.getCurrentUser();
return awsUser.userId;
} catch (e) {
return null;
}
}
@override
Future<String?> confirmSignUp({
required String email,
required String confirmationCode,
}) async {
try {
final res = await Amplify.Auth.confirmSignUp(
username: email,
confirmationCode: confirmationCode,
);
return res.isSignUpComplete ? user : null;
} on AuthException catch (e) {
throw AuthFailure(message: e.message);
}
}
@override
Future<void> createNewUser({
required String username,
required String emailAddress,
required String userId,
}) async {
try {
final newUser = User(
id: userId,
username: username,
email: emailAddress,
dateCreated: TemporalDateTime.now(),
);
await Amplify.DataStore.save(newUser);
} on DataStoreException catch (e) {
throw OtherFailure(message: e.message);
}
}
This is my bloc below.
Future<void> confirmRegistration(String email, String username) async {
try {
emit(
state.copyWith(
status: ConfirmRegistrationStatus.submitting,
),
);
final userId = await _iAuthFacade.confirmSignUp(
email: email,
confirmationCode: state.confirmCode,
);
await _iAuthFacade.createNewUser(
username: username,
emailAddress: email,
userId: userId!,
);
emit(state.copyWith(status: ConfirmRegistrationStatus.submitted));
} on AuthFailure catch (e) {
emit(
state.copyWith(
status: ConfirmRegistrationStatus.error,
failure: AuthFailure(message: e.message),
),
);
}
but when i created a diffrent page or dummy page with just setstate it gave me the right result without returning null. This is it below.
Future<String?> get user async {
try {
final user = await Amplify.Auth.getCurrentUser();
log('user was checked');
return user.userId;
} catch (e) {
log('no user was found');
return null;
}
}
Future<String?> confirmUser() async {
try {
final result = await Amplify.Auth.confirmSignUp(
username: '[email protected]', confirmationCode: '615841');
log('confirm user was triggerd');
setState(() {
isCompleteSignUpComplete = result.isSignUpComplete;
});
return result.isSignUpComplete ? user : null;
} on AuthException catch (e) {
log(e.message);
}
}
Future<void> createNewUser({
required String username,
required String emailAddress,
required String userId,
}) async {
try {
final newUser = User(
id: userId,
username: username,
email: emailAddress,
dateCreated: TemporalDateTime.now(),
);
log('new user wa created');
await Amplify.DataStore.save(newUser);
} on DataStoreException catch (e) {
log(e.message);
}
}
Future<void> confirmSignUpAndRegUser() async {
log('confirmed signup and created new user');
final userId = await confirmUser();
log(userId.toString());
await createNewUser(
username: 'johnblll',
emailAddress: '[email protected]',
userId: userId!,
);
}