Reputation: 837
I implemented the log-in and registration views for the Authentication via Firebase. Then how can I authenticate to my database? I changed the rules and I put:
{
"rules": {
"users": {
"$uid": {
".write": "$uid === auth.uid"
}
}
}
}
I also wrote the code for obtain the User UID, but how can I say to my database that I'm authenticated?
PS: I need the example in Swift.
Upvotes: 1
Views: 705
Reputation: 6023
Swift 5 Just for Authentication and then use your own code for extension
let credential = PhoneAuthProvider.provider().credential(
withVerificationID: FireBaseverficationID,
verificationCode: "your device code")
let defaults = UserDefaults(suiteName: "group.Company.AppName")
let encodedData = NSKeyedArchiver.archivedData(withRootObject: credential)
defaults?.set(encodedData, forKey: "firebasecredential")
After this in your Extension File decode this Defaults Value and use Credential for SignIn
let decoded = UserDefaults(suiteName: "group.Company.AppName")?.object(forKey: "firebasecredential") as! Data
let credential = NSKeyedUnarchiver.unarchiveObject(with: decoded) as! PhoneAuthCredential
//MARK: - If user already login with credential
let userid = Auth.auth().currentUser
if userid == nil{
Auth.auth().signInAndRetrieveData(with: credential) { (authResult, error) in
if error != nil {
return
}
else
{
//MARK:- Your desire code After Login
}
}
}else{
//MARK:- Your desire code if User already Login
}
Upvotes: 1
Reputation: 49
try the following format:
cloud firestore
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth != null;
}
}
}
realtime database
// Checks auth uid equals database node uid
// In other words, the User can only access their own data
{
"rules": {
"users": {
"$uid": {
".read": "$uid === auth.uid",
".write": "$uid === auth.uid"
}
}
}
}
Upvotes: 0