Reputation: 381
So I have this little code snippet that is responsible for uploading a picture to the firebase storage. For some odd reason, my access keeps getting denied. There seems to be a problem with storageRef because it goes into the if statement that prints out the error.
// will handle the sign up of a user
@objc func handleSignUp(){
// first we cant to take sure that all of the fields are filled
var profilePic: String = ""
// will take the user selected image and load it to firebase
let imageName = NSUUID().uuidString
let storageRef = Storage.storage().reference().child("profile_images").child("\(imageName).PNG")
if let userImage = selectedImageFromPicker,let uploadData = UIImageJPEGRepresentation(userImage, 0.1){
storageRef.putData(uploadData, metadata: nil, completion: { (metadata, error) in
if error != nil{
print(error ?? "")
return
}
profilePic = (metadata?.downloadURL()!.absoluteString)!
guard let username = self.nameTextField.text,
let confirmPassword = self.confirmPasswordTextField.text,
let email = self.emailTextField.text,
let password = self.passwordTextField.text,
!username.isEmpty,
!email.isEmpty,
!password.isEmpty,
!confirmPassword.isEmpty
else {
print("Required fields are not all filled!")
return
}
if self.validateEmail(enteredEmail:email) != true{
let alertController = UIAlertController(title: "Error", message: "Please Enter A Valid Email", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "Ok", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
// will make sure user is validated before it even tries to create user
// will make sure the password and confirm password textfields have the same value if so it will print an error
if self.passwordTextField.text != self.confirmPasswordTextField.text {
let alertController = UIAlertController(title: "Error", message: "Passwords Don't Match", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "Ok", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}
// will authenticate a user into the authentication services with an email and passowrd
AuthService.createUser(controller: self, email: email, password: password) { (authUser) in
guard let firUser = authUser else {
return
}
//will add user to the database
print(profilePic)
print(username)
UserService.create(firUser, username: username , profilePic: profilePic,location: self.userLocation!) { (user) in
guard let user = user else {
print("User successfully loaded into firebase db")
return
}
// will set the current user for userdefaults to work
print(user.profilePic)
print(user.username)
User.setCurrent(user, writeToUserDefaults: true)
// self.delegate?.finishSigningUp()
self.finishSigningUp()
}
}
})
}
}
The error is
Error Domain=FIRStorageErrorDomain Code=-13021 "User does not have permission to access gs://eventful-3d558.appspot.com/profile_images/C0BC898A-6A6F-490F-954B-51D705CD2B23.PNG." UserInfo={object=profile_images/C0BC898A-6A6F-490F-954B-51D705CD2B23.PNG, bucket=eventful-3d558.appspot.com, ResponseBody={
"error": {
"code": 403,
"message": "Permission denied. Could not perform this operation"
}
If anyone knows what it is I would appreciate your help. It is preventing me from signing up users.
Upvotes: 0
Views: 2874
Reputation: 270
this is a perfect firebase presentation that will help with this problem: https://firebase.google.com/docs/database/security/
Upvotes: 0
Reputation: 278
You have to set the rules of Database to accept all read and write in Database->Rules
{
"rules": {
".read": true,
".write": true
}
}
By setting the database read and write rules to true, You set all the read write requests to be allowed.
If you find some permission errors in Firebase Storage then change the in Firebase Storage-> Rules
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if true;
}
}
}
Upvotes: 4
Reputation: 34
Make sure to check Firebase Authentication.
Firebase Storage buckets require Firebase Authentication to upload files.
Upvotes: 0