Firebase Demo Code with Swift5
Firebase Demo Project
The Firebase Realtime Database lets you build rich, collaborative applications by allowing secure access to the database directly from client-side code. Data is persisted locally, and even while offline, realtime events continue to fire, giving the end user a responsive experience.
Firebase Authentication aims to make building secure authentication systems easy, while improving the sign-in and onboarding experience for end users. It provides an end-to-end identity solution, supporting email and password accounts, phone auth, and Google, Twitter, Facebook, and GitHub login, and more.
- Create a project with an iOS app using the Firebase Console, and connect your iOS app to the newly created Firebase project with the use of
GoogleService-Info.plist, which is a file generated during your app registration and containing important information, e.g.PROJECT_ID,BUNDLE_IDandAPI_KEY. - Add the Firebase Authentication pod to your
Podfileand runpod installafterward. - Initialise the Firebase configuration in
AppDelegate.swiftat the start of the app: - Enable Email/Password authentication in the Authentication section in the Firebase Console:
Code:-
| let email = "example@gmail.com" | |
| let password = "testtest" | |
| Auth.auth().createUser(withEmail: email, password: password) { authResult, error in | |
| if let error = error as? NSError { | |
| switch AuthErrorCode(rawValue: error.code) { | |
| case .operationNotAllowed: | |
| // Error: The given sign-in provider is disabled for this Firebase project. Enable it in the Firebase console, under the sign-in method tab of the Auth section. | |
| case .emailAlreadyInUse: | |
| // Error: The email address is already in use by another account. | |
| case .invalidEmail: | |
| // Error: The email address is badly formatted. | |
| case .weakPassword: | |
| // Error: The password must be 6 characters long or more. | |
| default: | |
| print("Error: \(error.localizedDescription)") | |
| } | |
| } else { | |
| print("User signs up successfully") | |
| let newUserInfo = Auth.auth().currentUser | |
| let email = newUserInfo?.email | |
| } | |
| } |
Comments
Post a Comment