I have looked through every tutorial and the steps in retrieving the directories are more or less the same however I hit a snag and there doesn't seem to be anyone encountering this. So in my application, I have a button that calls a function
onPressed: () async {
isListing
? _showMyDialog()
: setState(() {
isListing = !isListing;
});
try {
directories=await firebaseAPI.list(
'${widget.level}/${widget.value}');
setState(() {
isListing = !isListing;
prefs.setBool('isInitialized', true);
});
_showNotification();
prefs.setBool('isInitialized', true);
Navigator.pushNamedAndRemoveUntil(
context, Dashboard.id, (route) => false);
} catch (error) {}
}
And here's the code for the list function
Future<firebase_storage.ListResult> downloadFiles(url) async {
final ref =
firebase_storage.FirebaseStorage.instance.ref(url);
final result = await ref.listAll();
print(result);
return result;
}
The function gets called but doesn't print or return anything it just gives me this:
W/NetworkRequest(13843): no auth token for request
W/StorageUtil(13843): no auth token for request
W/NetworkRequest(13843): No App Check token for request.
Which I don't get since I changed my rules to this:
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, allow write: if true;
}
}
}
Do I have to change the rules further? or is there a different way of accessing google cloud storage?
I figured it out. I just did a cold boot of my emulator and now it's able to access it
Firestore storage needs authentication in order to upload data, so you need to use firebase_auth or you need to change the rules to: allow read, write;
Related
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.
I am trying to upload an image to Firebase Storage using Flutter Web. I have followed many tutorials online on how to go about this, but I end up with the same error.
This is my code for upload
pickImageFromGallery() async {
imageFile = await ImagePickerWeb.getImageInfo;
setState(() {
_image = imageFile!.fileName!;
});
}
Future<void> uploadFile(
MediaInfo mediaInfo, String ref, String fileName) async {
try {
String mimeType = mime(path.basename(mediaInfo.fileName!))!;
final String extension = extensionFromMime(mimeType)!;
var metadata = fb.UploadMetadata(
contentType: mimeType,
);
fb.StorageReference storageReference =
fb.storage().ref('images').child('$fileName.$extension');
fb.UploadTaskSnapshot uploadTaskSnapshot =
await storageReference.put(mediaInfo.data, metadata).future;
Uri imageUri = await uploadTaskSnapshot.ref.getDownloadURL();
setState(() {
_imageURL = imageUri.toString();
});
} catch (e) {
setState(() {
_imageURL = e.toString();
});
}
}
This is my Storage rules for public access
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write;
}
}
}
This is the error I get
FirebaseError: Firebase Storage: An unknown error occurred, please check the error payload for server response.
{
"error": {
"code": 404,
"message": "Not Found."
}
} (storage/unknown)
I have tried everything online that seems to be the solution, but there is no exact solution for this problem. Please help me figure out what may be the problem.
I am using Flutter Web, and the following packages:
firebase,
path,
image_picker_web,
mime_type
EDIT: Working Code
The problem seemed to be with the StorageReference. It worked once edited to the following:
fb.StorageReference storageReference = fb.storage().refFromURL('gs://storageBucket/').child('$fileName.$extension');
replace 'storageBucket' with your storageBucket name shown in Firebase Storage
I also experienced this, because i did not update the firebase storage rules to allow me to write to the database
I got the same error.
It happens when the path can't reach the bucket, which I found out means "404 not found". Check the bucket name in storageBucket in firebaseConfig. In my case, I was using Dotenv and left a space after the double quotes.
Dotenv was converting it to "" double quotation marks.
You can check the bucket name in the Firebase storage console. You don't need the "gs://" at the beginning or the "/" at the end. Just check the name. If you are using Dotenv, watch out for spaces.
I got this error because when I copied the REACT_APP_FIREBASE_STORAGE_BUCKET variables from the .env file used in my local environment to Netlify which is my hosting service, I included the quotation marks (that is, I entered 'my.secret.url' instead of my.secret.url.
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,
);
Thanks to the https://www.youtube.com/watch?v=VDulvfBpzZE great resource, I installed firestore emulator and my testing environment, everything went fine except when I came to test the following rule:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
function invitationIsForMe(destEmail) {
return request.auth.email == destEmail;
}
match /invitations/{inviteId} {
allow read, delete: if invitationIsForMe(resource.destEmail);
}
}
Which generate an error in the test:
FirebaseError:
Property email is undefined on object.
The test is as follow:
const me = {uid: 'myuserid', email: 'me#foo.bar'};
const getDb = (auth) => {
return firebase.initializeTestApp({projectId: YORGA_PROJECT_ID, auth}).firestore();
}
describe('firestore structure and rules', () => {
it("Can read an invitation sent to me", async () => {
const db = getDb(me);
const testDoc = db.collection('invitations').where('destEmail', '==', me.email);
await firebase.assertSucceeds(testDoc.get());
})
}
If I test it with request.auth.uid, it's working (but that's not the logic I'm looking for).
So my question: is it a firestore emulator limitation ? I hope it's not and it's just me, but if it's the case I think the example given with a uid and an email for auth in initializing the testing base is a bit confusing so it's worth a warning.
I have a class that wraps http calls (get, post, delete).
What I need to achieve is a way to check every api response to be able to force a logout if, for example, a token is expired.
Let's say I'm logged in my app and my token expires; I then try to list my posts with a call like this:
var results = api.get('http://api.site.com/user/123/posts')
get(String endpoint) async {
return http.get(endpoint,
headers: headers(),
).then((response) {
var result = json.decode(response.body);
if (result['force_logout'] == true) {
UserServices.logout(); // delete secure storage data and other stuff
Navigator.of(context).pushNamedAndRemoveUntil('/login', (Route<dynamic> route) => false);
}
return result;
});
}
Inside my api get method I would like to be able to perform the user logout and navigate to the login screen, but how can I do it without having a context?