Today, we are going to learn Firebase Authentication(Phone Number authentication.)
Google Firebase provides many services, for example, notification(Firebase messaging), real-time database, analytics, etc.
Firebase authentication provides many authentication methods for instance,
- Sign-in with Apple
- Google sign-in
- Email and Password authentication
- Phone Number authentication, and many more.
Phone Number authentication is very easy and commonly used when users want to log in or sign in through a Phone Number. firebase sends the 6-digit (One Time Password) to the user’s phone via SMS messaging.
After that, the user has to send type the OTP in the input field in the app then the app has to send this OTP with verification ID to the firebase for verification.
Moreover, Firebase uses reCAPTCHA and silent push notifications to prevent bots from spamming the system
Implement Phone Number Authentication
Step -1
Configure a firebase project with an iOS app. A GoogleService-Info.plist file will be generated, add this file to your swift project.
After that, Add the REVERSED_CLIENT_ID value to the “URL Schemes” field at the Info tab
Step – 2
Now install ‘Firebase/Auth’ pod into your project
1 |
pod 'Firebase/Auth' |
Step – 3
Enable Phone number Authentication in the firebase project
Step – 4
Now we have to generate APN’s certificate.
Firebase uses silent push notifications to send tokens to the user’s device to verify the request is coming from a real user, after that import them to the keychain access.
And export the private key. Upload the exported .p12 file to the firebase project
Add the Push Notifications capability to your swift project
Step – 5
Request a OTP with a phone number
1 2 3 4 5 6 7 8 9 10 11 |
@IBAction func generateOTP(_ sender: Any) { Auth.auth().languageCode = "en" PhoneAuthProvider.provider().verifyPhoneNumber( NumberText.text ?? "", uiDelegate: nil) { (id, error) in if let error = error { print(error.localizedDescription) return } self.verificationId = id! } } |
This will verify the user’s phone number and the user receives a confirmation SMS with OTP.
Note:- You need to add the country code with the phone number(+91)
Step – 6
In the last step, we need to call PhoneAuthProvider credential method by sending verificationID and OTP in the arguments
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
@IBAction func Login(_ sender: Any) { let credential = PhoneAuthProvider.provider().credential(withVerificationID: verificationId, verificationCode: OTpPtext.text ?? "") Auth.auth().signIn(with: credential) { (authResult, error) in if let error = error { let authError = error as NSError print(authError.description) return } print("authResult: ", authResult) print("authResult: ", authResult?.additionalUserInfo) let currentUserInstance = Auth.auth().currentUser print("currentUserInstance: ", currentUserInstance) } } |
After that , we can use this credentails to sign in using Auth.auth().signIn(with: credential) method.
In Conclusion, Phone number authentication is very easy with Firebase
Lastly, If you have any queries or suggestions, you can leave your query/suggestion in the comment section.
For other blogs Please click here