1

Im trying to organize my Database and Storage. And i want to use the same autoID it generated for the user in database to be used in my Storage. Is it possible?

this is the code that i use to save the profile photo.

 let email = Auth.auth().currentUser?.email else { return }

 let storageRef = Storage.storage().reference().child(email+"/ProfilePhoto/\("Profile Photo")")

Instead of using the email. i want the user autoID in database to be used.

enter image description here

Nick
  • 104
  • 11
  • Are you sure you don't want to use the user's unique ID in Firebase Authentication? That usually makes more sense to store per-user data. – Doug Stevenson Aug 24 '19 at 13:26
  • how do i get the ID in Firebase Authentication to be used for the userID in database? – Nick Aug 24 '19 at 13:28
  • this is what i use to save the user in database Database.database().reference().child("User").childByAutoId().setValue(UserDictionary) and how to use the same ID in Firebase Storage – Nick Aug 24 '19 at 13:29
  • Please don't use emails as node keys or paths. It will contain characters that cannot be used as a key and then you have to parse it and have a bunch of extra code. Just use the users uid as the key and also as the path the where their data is stored in storage. So it woud look like */storage/uid/ProfilePhoto* where the uid is `currentUser.uid` – Jay Aug 24 '19 at 13:35

2 Answers2

2

The most appropriate way to store per user information in both Cloud Storage and Realtime Database (and Cloud Storage for that matter) is the signed-in user's unique ID (UID).

let uid = Auth.auth().currentUser?.uid else { return }

See https://firebase.google.com/docs/reference/swift/firebaseauth/api/reference/Protocols/UserInfo.html#uid

The auto id generated by Realtime Database won't uniquely identify the user. It will return a random value every time.

Jay
  • 34,438
  • 18
  • 52
  • 81
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • it worked. but how come theres a word "Optional" in the user in Firebase database? – Nick Aug 24 '19 at 13:41
  • Thats because Doug uses a `?` to find the user. See https://stackoverflow.com/questions/43460412/getting-an-optional-firebase-database-value-when-it-shouldnt-be-optional – Frank van Puffelen Aug 24 '19 at 13:57
-1

To get the UID of the current user:

let uid = Auth.auth().current?.uid
print(uid!)
  • it worked. but how come theres a word "Optional" in the user in Firebase database? See image above – Nick Aug 24 '19 at 13:57
  • Because you have saved an optional string in the database. You must make it not optional with **!**. – Filippo Zanfini Aug 24 '19 at 14:07
  • For example: var str: String? str = "Hello" print (str) // Optional ("Hello") var str: String! str = "Hello" print (str) // "Hello" – Filippo Zanfini Aug 24 '19 at 14:09
  • @Nick It is **not** because *Because you have saved an optional string in the database*. Also, Auth.auth() does not have a `current` var, it's `.currentUser`. It's optional because the `currentUser` var could be `nil` if there is no current user, so it, and it's properties could be nil. You should never force unwrap it with a ! in a production app as it can cause your app to crash. When a var can be nil, Swift uses an Optional property - which has nothing to do with Firebase or storing data. – Jay Aug 25 '19 at 13:04
  • Here's a link with a great explanation and best practices. [optionals](https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu/32170457#32170457) – Jay Aug 25 '19 at 13:06