Firebase: Registering users based on their phone numbers added by admin -


newbie here. trying allow users register via phone number, when phone numbers have been added earlier admin. or link resources helpful. thanks!

basically via admin account, need add "good" numbers inside node let's call "white_list" , reference whitelistref.

then need 2 essential firebase methods register user via phone number, first 1 :

phoneauthprovider.provider().verifyphonenumber(phone_number) { } 

it send verification code user.

then need add method :

auth.auth().signin(with: credential) { } 

credential created using verificationid got first method, , verification code user received phone number if number correct :

 let credential = phoneauthprovider.provider().credential(         withverificationid: verificationid,         verificationcode: verificationcode) 

in case, need check phone number user typed in among white list (that admin added white_list node). depending on database structure, example, use method 1 :

func checknumbereligibility(_ phonenumber: string, completion: @escaping(bool) -> void) {         whitelistref.observesingleevent(of: .value, with: { (snapshot) in           guard let numbereligibility = snapshot.childsnapshot(forpath: "eligibility").value as? string else { return }          if numbereligibility == "good" {             completion(true)         } else {             completion(false)         }      }) 

finally, have :

 func registerphonenumber(_ phonenumber: string, completion: @escaping () -> void) {          phoneauthprovider.provider().verifyphonenumber(phonenumber) { [weak self] (verificationid, error) in              guard error == nil else {                 return             }           self?.checknumbereligibility(phonenumber) { (isnumberokay) in              guard isnumberokay else {                 return             }              userdefaults.standard.set(verificationid, forkey: "authverificationid")          }        }    }       func verifycode(_ verificationcode: string, completion: @escaping() -> void) {          guard let verificationid = userdefaults.standard.string(forkey: "authverificationid") else {             return         }          let credential = phoneauthprovider.provider().credential(             withverificationid: verificationid,             verificationcode: verificationcode)          auth.auth().signin(with: credential, completion: { (user, error) in              guard error == nil else {                 return             }              completion()          })      } 

this work, however, way, mean need give access via database rules white list node person (that mean, non users of app) might expose security issues. might consider before implementing system.

just let me know if have questions!


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -