Basil Basil - 1 month ago 6
Swift Question

Sign In by UserName : Firebase,Swift

I want to let user sign in with Email and also with User Name
Is that possible using Firebase ?

Answer

Completely separating your users account from any kind of domain specific auth credential might be a real pain later(Most definitely):-

  • Updating your user password
  • Privacy

A better approach is to sign up your user with an email-ID and a unique userNameCheck if userName Already exists :Firebase, Swift, Then give your user option to Log In with a username or emailID.

Add another node to your JSON tree:-

{...

usernameEmailLink:{

    uniqueUserName1:emailID1,
    uniqueUserName2:emailID2, 
    uniqueUserName3:emailID3 

      }
 ...}

Update your security rules for this node:-

{".rules":{

 ".read" : "auth != null",
 ".write" : "auth != null",

  "usernameEmailLink" :{

    ".read" : "true",
    ".write" : "auth != null" 
     }
   }
 }

Code:-

func retrieveUserEmail(userName : String){

    FIRDatabase.database().reference().child("usernameEmailLink/\(userName)").observeSingleEvent(of: .value, with: {(snap) in


    print(snap.value!) //This will give you the email ID for the user.

    })

}

For saving the key-value pair:-

FIRDatabase.database().reference().child("usernameEmailLink").setValue["\(userName)" : "\(Email)"]