public void RegisterNewAccount() { context.Fetch(); var email = newAccountData.Email; var password = newAccountData.Password; var passwordVerify = newAccountData.PasswordVerify; if (string.IsNullOrWhiteSpace(email) || string.IsNullOrWhiteSpace(password) || string.IsNullOrWhiteSpace(passwordVerify)) { AppDelegate.ShowMessage("Hey!", "Seems that some information is missing...", NavigationController); return; } if (password != passwordVerify) { AppDelegate.ShowMessage("Hey!", "Passwords don't match. Please, verify.", NavigationController); return; } indicatorView.StartAnimating(); View.EndEditing(true); Auth.DefaultInstance.CreateUser(email, password, CreateUserOnCompletion); }
void ShowInformationMessage() { AppDelegate.ShowMessage("Warning!", "If you use phone sign-in, you might receive an SMS message for verification and standard rates apply.", NavigationController, CheckForPushNotificationsStatus); }
void SignInOnCompletion(AuthDataResult authResult, NSError error) { if (error != null) { AuthErrorCode errorCode; if (IntPtr.Size == 8) // 64 bits devices { errorCode = (AuthErrorCode)((long)error.Code); } else // 32 bits devices { errorCode = (AuthErrorCode)((int)error.Code); } // Posible error codes that SignIn method with credentials could throw // Visit https://firebase.google.com/docs/auth/ios/errors for more information switch (errorCode) { case AuthErrorCode.InvalidCredential: case AuthErrorCode.InvalidEmail: case AuthErrorCode.OperationNotAllowed: case AuthErrorCode.EmailAlreadyInUse: case AuthErrorCode.UserDisabled: case AuthErrorCode.WrongPassword: default: AppDelegate.ShowMessage("Could not login!", error.LocalizedDescription, NavigationController); break; } return; } NavigationController.PushViewController(new UserViewController("Google"), true); }
void DownloadPhoto(string userUid, string url) { if (string.IsNullOrWhiteSpace(userUid) || string.IsNullOrWhiteSpace(url)) { return; } var webClient = new WebClient(); webClient.DownloadDataCompleted += (sender, e) => { if (e.Error != null) { InvokeOnMainThread(() => { AppDelegate.ShowMessage("Cannot update the photo...", e.Error.Message, NavigationController); }); return; } var bytes = e.Result; var tempPath = Path.GetTempPath(); var filename = $"{userUid}.png"; var filePath = Path.Combine(tempPath, filename); File.WriteAllBytes(filePath, bytes); InvokeOnMainThread(() => { photo.Image = GetPhoto(userUid); Root.Reload(imgPhotoContainer, UITableViewRowAnimation.None); }); }; webClient.DownloadDataAsync(new Uri(url)); }
void CreateUserOnCompletion(User user, NSError error) { indicatorView.StopAnimating(); if (error != null) { AuthErrorCode errorCode; if (IntPtr.Size == 8) // 64 bits devices { errorCode = (AuthErrorCode)((long)error.Code); } else // 32 bits devices { errorCode = (AuthErrorCode)((int)error.Code); } // Posible error codes that CreateUser method could throw // Visit https://firebase.google.com/docs/auth/ios/errors for more information switch (errorCode) { case AuthErrorCode.InvalidEmail: case AuthErrorCode.EmailAlreadyInUse: case AuthErrorCode.OperationNotAllowed: case AuthErrorCode.WeakPassword: default: AppDelegate.ShowMessage("Could not login!", error.LocalizedDescription, NavigationController); break; } return; } NavigationController.PushViewController(new UserViewController("Firebase"), true); }
void ShowDeniedMessage() { AppDelegate.ShowMessage("Push Notifications was denied.", "Please, go to app settings and grant permissions for Push Notifications.", NavigationController, "Got it!", null, "Go to settings", OpenSettings); }
void VerifyPhoneNumberOnCompletion(string verificationId, NSError error) { indicatorView.StopAnimating(); if (error != null) { AppDelegate.ShowMessage("An error has ocurred...", error.LocalizedDescription, NavigationController); return; } NSUserDefaults.StandardUserDefaults.SetString(verificationId, "AuthVerificationID"); OpenViewController(new VerificationCodeViewController()); }
void SendPasswordResetOnCompletion(NSError error) { indicatorView.StopAnimating(); if (error != null) { AppDelegate.ShowMessage("Email sent failed!", error.LocalizedDescription, NavigationController); } else { AppDelegate.ShowMessage("Email sent!", $"An email has been sent to {passwordData.Email}", NavigationController); } }
public MenuViewController() : base(UITableViewStyle.Grouped, null, true) { Root = new RootElement("Firebase Auth Sample") { new Section("Select your authentication method") { new StringElement("Password Authentication", () => OpenViewController(new PasswordLoginViewController())) { Alignment = UITextAlignment.Center }, new StringElement("Phone Number Authentication", () => { if (Runtime.Arch == Arch.SIMULATOR) { AppDelegate.ShowMessage("Hey!", "To see this auth in action, you must run the sample on a device.", NavigationController); return; } if (NSUserDefaults.StandardUserDefaults.StringForKey("") == null) { OpenViewController(new PhoneNumberViewController()); } else { UIViewController [] viewControllers = { NavigationController.TopViewController, new PhoneNumberViewController(), new VerificationCodeViewController() }; NavigationController.SetViewControllers(viewControllers, true); } }) { Alignment = UITextAlignment.Center }, new StringElement("Google Sign-In", () => { var storyboard = UIStoryboard.FromName("Main", NSBundle.MainBundle); var viewController = storyboard.InstantiateViewController("SignInLoginViewControllerID") as SignInLoginViewController; OpenViewController(viewController); }) { Alignment = UITextAlignment.Center }, new StringElement("Facebook Login", () => { var storyboard = UIStoryboard.FromName("Main", NSBundle.MainBundle); var viewController = storyboard.InstantiateViewController("FacebookLoginViewControllerID") as FacebookLoginViewController; OpenViewController(viewController); }) { Alignment = UITextAlignment.Center } } }; }
public void DidSignIn(SignIn signIn, GoogleUser user, NSError error) { if (error == null && user != null) { // Get Google ID token and Google access token and exchange them for a Firebase credential var authentication = user.Authentication; var credential = GoogleAuthProvider.GetCredential(authentication.IdToken, authentication.AccessToken); // Authenticate with Firebase using the credential Auth.DefaultInstance.SignInWithCredential(credential, SignInOnCompletion); } else { BtnSignIn.Enabled = true; AppDelegate.ShowMessage("Could not login!", error.LocalizedDescription, NavigationController); } }
public void SendPasswordReset() { context.Fetch(); var email = passwordData.Email; if (string.IsNullOrWhiteSpace(email)) { AppDelegate.ShowMessage("Hey!", "Please, enter an email!", NavigationController); return; } indicatorView.StartAnimating(); View.EndEditing(true); auth.SendPasswordReset(email, SendPasswordResetOnCompletion); }
void UpdateEmail() { var email = txtEmail.Value; if (string.IsNullOrWhiteSpace(email)) { AppDelegate.ShowMessage("Hey!", "Youn need to type an email!", NavigationController); return; } indicatorView.StartAnimating(); View.EndEditing(true); user.UpdateEmail(email, (error) => { indicatorView.StopAnimating(); if (error != null) { AuthErrorCode errorCode; if (IntPtr.Size == 8) // 64 bits devices { errorCode = (AuthErrorCode)((long)error.Code); } else // 32 bits devices { errorCode = (AuthErrorCode)((int)error.Code); } // Posible error codes that UpdateEmail method could throw // Visit https://firebase.google.com/docs/auth/ios/errors for more information switch (errorCode) { case AuthErrorCode.EmailAlreadyInUse: case AuthErrorCode.InvalidEmail: case AuthErrorCode.RequiresRecentLogin: default: AppDelegate.ShowMessage("Could not update the email!", error.LocalizedDescription, NavigationController); break; } } else { AppDelegate.ShowMessage("Success!", "Email updated!", NavigationController); } }); }
void SendVerifyEmail() { indicatorView.StartAnimating(); View.EndEditing(true); user.SendEmailVerification((error) => { indicatorView.StopAnimating(); if (error != null) { AppDelegate.ShowMessage("Could not send the email!", error.LocalizedDescription, NavigationController); } else { AppDelegate.ShowMessage("Success!", $"Verification email sent to {user.Email}!", NavigationController); } }); }
void LogIn() { var verificationCode = txtVerificationCode.Value; if (string.IsNullOrWhiteSpace(verificationCode)) { AppDelegate.ShowMessage("Invalid Code", "Please, enter a valid code.", NavigationController); return; } View.EndEditing(true); indicatorView.StartAnimating(); var verificationId = NSUserDefaults.StandardUserDefaults.StringForKey("AuthVerificationID"); var credential = PhoneAuthProvider.DefaultInstance.GetCredential(verificationId, verificationCode); Auth.DefaultInstance.SignInAndRetrieveDataWithCredential(credential, SignInOnCompletion); }
public void Login() { context.Fetch(); var email = passwordData.Email; var password = passwordData.Password; if (string.IsNullOrWhiteSpace(email) || string.IsNullOrWhiteSpace(password)) { AppDelegate.ShowMessage("Hey!", "Seems that some information is missing...", NavigationController); return; } indicatorView.StartAnimating(); View.EndEditing(true); auth.SignIn(email, password, SignInOnCompletion); }
void SendVerificationCode() { var phoneCode = txtPhoneNumber.Caption; var phoneNumber = txtPhoneNumber.Value; if (string.IsNullOrWhiteSpace(phoneNumber)) { AppDelegate.ShowMessage("Invalid Phone Number", "Please, enter a number.", NavigationController); return; } View.EndEditing(true); indicatorView.StartAnimating(); PhoneAuthProvider.DefaultInstance.VerifyPhoneNumber($"{phoneCode}{phoneNumber}", VerifyPhoneNumberOnCompletion); }
void UpdateNameAndPhoto() { var name = txtName.Value; var photoUrl = txtPhotoUrl.Value; if (string.IsNullOrWhiteSpace(name)) { AppDelegate.ShowMessage("Hey!", "Please, enter a name...", NavigationController); return; } indicatorView.StartAnimating(); View.EndEditing(true); var changeRequest = user.ProfileChangeRequest(); changeRequest.DisplayName = name; changeRequest.PhotoUrl = new NSUrl(photoUrl); changeRequest.CommitChanges((error) => { indicatorView.StopAnimating(); if (error != null) { AppDelegate.ShowMessage("Could not update name and photo!", error.LocalizedDescription, NavigationController); } else { AppDelegate.ShowMessage("Success!", "Name and photo updated!", NavigationController); if (PhotoExists(user.Uid) && lastPhotoUrl == user.PhotoUrl?.AbsoluteString) { photo.Image = GetPhoto(user.Uid); } else if (user.PhotoUrl != null) { DownloadPhoto(user.Uid, user.PhotoUrl.AbsoluteString); lastPhotoUrl = user.PhotoUrl.AbsoluteString; } } }); }
void Logout() { NSError error; var signedOut = Auth.DefaultInstance.SignOut(out error); if (signedOut) { if (authMethod == "Google") { SignIn.SharedInstance.SignOutUser(); } else if (authMethod == "Facebook") { new LoginManager().LogOut(); } NavigationController.PopToRootViewController(true); } else { AuthErrorCode errorCode; if (IntPtr.Size == 8) // 64 bits devices { errorCode = (AuthErrorCode)((long)error.Code); } else // 32 bits devices { errorCode = (AuthErrorCode)((int)error.Code); } // Posible error codes that SignOut method with credentials could throw // Visit https://firebase.google.com/docs/auth/ios/errors for more information switch (errorCode) { case AuthErrorCode.KeychainError: default: AppDelegate.ShowMessage("Could not login!", error.LocalizedDescription, NavigationController); break; } } }
void DeleteUser() { user.Delete((error) => { if (error != null) { AuthErrorCode errorCode; if (IntPtr.Size == 8) // 64 bits devices { errorCode = (AuthErrorCode)((long)error.Code); } else // 32 bits devices { errorCode = (AuthErrorCode)((int)error.Code); } // Posible error codes that Delete method could throw // Visit https://firebase.google.com/docs/auth/ios/errors for more information switch (errorCode) { case AuthErrorCode.RequiresRecentLogin: default: AppDelegate.ShowMessage("Could delete the current user!", error.LocalizedDescription, NavigationController); break; } return; } if (authMethod == "Google") { SignIn.SharedInstance.SignOutUser(); } else if (authMethod == "Facebook") { new LoginManager().LogOut(); } NavigationController.PopToRootViewController(true); }); }
void BtnLogin_Completed(object sender, LoginButtonCompletedEventArgs e) { if (e.Error != null) { // Handle if there was an error AppDelegate.ShowMessage("Could not login!", e.Error.Description, NavigationController); return; } if (e.Result.IsCancelled) { // Handle if the user cancelled the login request AppDelegate.ShowMessage("Could not login!", "The user cancelled the login", NavigationController); return; } // Get access token for the signed-in user and exchange it for a Firebase credential var credential = FacebookAuthProvider.GetCredential(AccessToken.CurrentAccessToken.TokenString); // Authenticate with Firebase using the credential Auth.DefaultInstance.SignIn(credential, SignInOnCompletion); }
public void DidComplete(LoginButton loginButton, LoginManagerLoginResult result, NSError error) { if (error != null) { // Handle if there was an error AppDelegate.ShowMessage("Could not login!", error.Description, NavigationController); return; } if (result.IsCancelled) { // Handle if the user cancelled the login request AppDelegate.ShowMessage("Could not login!", "The user cancelled the login", NavigationController); return; } // Get access token for the signed-in user and exchange it for a Firebase credential var credential = FacebookAuthProvider.GetCredential(AccessToken.CurrentAccessToken.TokenString); // Authenticate with Firebase using the credential Auth.DefaultInstance.SignInWithCredential(credential, SignInOnCompletion); }
void UpdatePassword() { var password = txtPassword.Value; var passwordVerify = txtPasswordVerify.Value; if (string.IsNullOrWhiteSpace(password) || string.IsNullOrWhiteSpace(passwordVerify)) { AppDelegate.ShowMessage("Hey!", "Seems that some information is missing...", NavigationController); return; } if (password != passwordVerify) { AppDelegate.ShowMessage("Hey!", "Passwords don't match. Please, verify.", NavigationController); return; } indicatorView.StartAnimating(); View.EndEditing(true); user.UpdatePassword(password, (error) => { indicatorView.StopAnimating(); if (error != null) { AppDelegate.ShowMessage("Could not update the password!", error.LocalizedDescription, NavigationController); } else { AppDelegate.ShowMessage("Success!", "Password updated!", NavigationController); txtPassword.Value = string.Empty; txtPasswordVerify.Value = string.Empty; Root.Reload(txtPassword, UITableViewRowAnimation.None); Root.Reload(txtPasswordVerify, UITableViewRowAnimation.None); } }); }
void SignInOnCompletion(AuthDataResult authResult, NSError error) { indicatorView.StopAnimating(); if (error != null) { AuthErrorCode errorCode; if (IntPtr.Size == 8) // 64 bits devices { errorCode = (AuthErrorCode)((long)error.Code); } else // 32 bits devices { errorCode = (AuthErrorCode)((int)error.Code); } // Posible error codes that SignIn method with email and password could throw // Visit https://firebase.google.com/docs/auth/ios/errors for more information switch (errorCode) { case AuthErrorCode.OperationNotAllowed: case AuthErrorCode.InvalidEmail: case AuthErrorCode.UserDisabled: case AuthErrorCode.WrongPassword: default: AppDelegate.ShowMessage("Could not login!", error.LocalizedDescription, NavigationController); break; } return; } NSUserDefaults.StandardUserDefaults.RemoveObject("AuthVerificationID"); NavigationController.PushViewController(new UserViewController("Firebase2"), true); }