AWS Cognito deleteUser in flutter not working - flutter

I am trying to implement deleteUser function in cognito which allows the user to delete their own account information by using this code
void deleteUser() async {
try {
await Amplify.Auth.deleteUser();
final state = AuthState(authFlowStatus: AuthFlowStatus.none);
authStateController.add(state);
print('Delete user succeeded');
} on AmplifyException catch (e) {
print('Delete user failed with error: $e');
}
}
However, this code of course is not working for some reason and I don't know why. I also have the user's database in Amplify datastore graphql so I tried deleting the user's datastore data first before getting rid of the user's congnito data but it still does not work. This is the code that I am using to delete user's data from datastore. Please help me out by telling me how I can delete a user's cognito and datastore data.
void deleteUser (DeleteUserData data) async {
(await Amplify.DataStore.query(User.classType, where: User.EMAIL.eq(data.email)))
.forEach((element) async {
try {
await Amplify.DataStore.delete(element);
print('Deleted a user');
} on DataStoreException catch (e) {
print('Delete failed: $e');
}
});
}
Please help me out.

I was facing the same problem, I took a look into the insides of deleteUser method in the AWS api, something I strongly recommend, and I discovered that this API is only available in iOS.
screenshot
I was using amplify_auth_cognito: 0.4.5 and amplify_flutter: 0.4.5.
Upgraded the libraries to 0.5.1 and the warning has disappeared, maybe you can try using a different version of yours (I'm not using Amplify, just simple Cognito).
Cheers.

Related

Endpoint Not Responding

I'm trying to make CRUD app using mongodb atlas and express when i need to make an GET endpoint its not responding and keeps loading here the code
Get Endpoint:
app.get('/getTodo',(req, res) => {
const cursor = db.getDB().collection(coll).find({});
cursor.toArray(( todos) => {
res.send(todos);
});
}
});
I tried to catch the error using try catch there was no error then i tried to log something after it it logged. I also tried to user async and await samething nothing changed.
what should I do?? what I'm doing wrong??

Firebase Functions Exception "unauthenticated" but function does run

I am implementing Facebook Sign In with Firebase in my Flutter App, and when a user signs in with Facebook Sign In for the first time I am changing the "emailverified" property to true. I do this through functions with the following code:
if (facebookUser.user?.emailVerified == false) {
try {
HttpsCallable callable = FirebaseFunctions.instance.httpsCallable('verifyFacebook');
await callable.call();
} on FirebaseFunctionsException catch (e) {
print(e.code);
print(e.plugin);
print(e.message);
}
}
where verifyFacebook is:
exports.verifyFacebook = functions.auth.user().onCreate(async (snap) => {
return await admin.auth().updateUser(snap.uid, {
emailVerified: true
});
})
What I do not understand is that a Firebase Function Exception of "unauthenticated" is printed in the console, but when I sign in for the second time the "emailverified" property is changed to true (meaning that the Firebase Function was executed properly)! How could this happen if in the try-catch part I was thrown an error?
I searched for other answers such as adding a permission in Google Cloud Console but none helped me :(
Thanks in advance!
In Google Cloud Console I added "allUsers" in the Permission property, and I expected my app to work fine without the "unauthenticated" error. However, the issue persisted.
The verifyFacebook function triggers every time a user is created with Firebase Authentication and is not a callable function. You don't have to call the function yourself. When you try to call it, the callable function does not exists and hence you get an error but the function triggered by Firebase Auth does run.

FirebaseFunctionsException: code: -1007, too many HTTP redirects

I don't understand why I'm getting this error. It happens either on a virtual or real device. I try to uninstall the app and nothing change. Why a simple helloWord function throw this error. I have no console log displayed. In the log I have this message: "Request has invalid method. GET" then "Error: Invalid request, unable to process. at entryFromArgs..." Why redirect? Could it be related to the cloud region I change? How could I clean that. Thank you.
Below the cloud function.
exports.belleDiana = functions.region(CLOUD_REGION).https.onCall(async (data, context) : Promise<String> => {
console.log("belleDiana", data, context);
console.log("context.auth", context.auth?.uid, context);
return "belleDianaDone";
});
The call on flutter side.
try {
final result =
await FirebaseFunctions.instance.httpsCallable('belleDiana').call();
print("result: $result");
} on FirebaseFunctionsException catch (error) {
print(error);
print(error.code);
print(error.details);
print(error.message);
}
I switch to the original region 'us-central1'. When I change cloud region I have this message " Unhandled error cleaning up build images. This could result in a small monthly bill if not corrected. You can attempt to delete these images by redeploying or you can delete them manually at https://console.cloud.google.com/artifacts/docker/myproject/europe-west1/gcf-artifacts
"
After the deletion of that manually, every thing work as attended. I don't really understand why...

Why can't i access my s3 objects in amplify flutter?

Message displayed on chrome
Function to get the S3 URL
Future<String> getUrlS3(String UploadKey) async {
try {
GetUrlResult result =
await Amplify.Storage.getUrl(key: UploadKey);
return(result.url);
} on StorageException catch (e) {
return(e.message);
}
}
Function to get url
Future<void> cet() async {
String store = await S3Helper.getUrlS3('cover807c3f99-2bcb-44e3-be80-4899ea2355d8Attac');
print(store);
}
When I click on the link I get the access denied page.
The only way i can see the Objects are if i make the specific object public, also i am logged in via Auth Cognito so i should have the permissions to view them
I think you need to pass accesslevel option to your getUrlS3.
something like this
S3DownloadFileOptions options = S3UploadFileOptions(
accessLevel: StorageAccessLevel.protected,
metadata: metadata,
);

How can I catch errors in my firebase function when setting a document fails?

I have a firebase cloud function to create a user document with user data whenever a user registers. How would I return an error when the set() fails? Since this is not an http request (an I don't want to use an http request in this case) I have no response. So how would I catch errors?
export const onUserCreated = functions.region('europe-west1').auth.user().onCreate(async user => {
const privateUserData = {
phoneNumber: user.phoneNumber
}
const publicUserData = {
name: 'Nameless'
}
try
{
await firestore.doc('users').collection('private').doc('data').set(privateUserData);
}catch(error)
{
//What do I put here?
}
try
{
await firestore.doc('users').collection('public').doc('data').set(publicUserData);
}catch(error)
{
//What do I put here?
}
});
You can't "return" an error, since the client doesn't even "know" about this function running, there is nobody to respond to.
You can make a registration collection, and in your function make a document there for the current user (using the uid as the document id). In that document, you can put any information you'd like your user to know (status, errors, etc).
So your clients would have to add a listener to this document to learn about their registration.
In your particular code, I think the error is in doc('users'). I guess you meant doc('users/'+user.uid).
Your catch -block will receive errors that occur on your set -call:
try {
await firestore.doc('users').collection('public').doc('data').set(publicUserData);
} catch (error) {
// here you have the error info.
}