Ejemplo n.º 1
0
        public UserPageViewModel(INavigationService navigationService, IPageDialogService pageDialogService, IGoogleService googleService, IFacebookService facebookService, IAppleService appleService) : base(navigationService)
        {
            _pageDialogService = pageDialogService;
            _googleService     = googleService;
            _facebookService   = facebookService;
            _appleService      = appleService;

            _multiFactorService = new MultiFactorService(this);

            Title = "User";

            var user = _auth.CurrentUser;

            Update(user);

            Name        = new ReactivePropertySlim <string>(user?.DisplayName);
            Email       = new ReactivePropertySlim <string>(user?.Email);
            PhoneNumber = new ReactivePropertySlim <string>(user?.PhoneNumber);

            _isEnrolledMultiFactor.Value = user.MultiFactor.EnrolledFactors.Any();

            UpdateNameCommand.Subscribe(UpdateName);
            UpdateEmailCommand.Subscribe(UpdateEmail);
            UpdatePhoneNumberCommand.Subscribe(UpdatePhoneNumber);
            SignOutCommand.Subscribe(SignOut);
            LinkOrUnlinkWithGoogleCommand.Subscribe(() => IsLinkedWithGoogle.Value ? UnlinkWithProvider("google.com") : LinkWithGoogle());
            LinkOrUnlinkWithTwitterCommand.Subscribe(() => IsLinkedWithTwitter.Value ? UnlinkWithProvider("twitter.com") : LinkWithProvider("twitter.com"));
            LinkOrUnlinkWithFacebookCommand.Subscribe(() => IsLinkedWithFacebook.Value ? UnlinkWithProvider("facebook.com") : LinkWithFacebook());
            LinkOrUnlinkWithGitHubCommand.Subscribe(() => IsLinkedWithGitHub.Value ? UnlinkWithProvider("github.com") : LinkWithProvider("github.com"));
            LinkOrUnlinkWithYahooCommand.Subscribe(() => IsLinkedWithYahoo.Value ? UnlinkWithProvider("yahoo.com") : LinkWithProvider("yahoo.com"));
            LinkOrUnlinkWithMicrosoftCommand.Subscribe(() => IsLinkedWithMicrosoft.Value ? UnlinkWithProvider("microsoft.com") : LinkWithProvider("microsoft.com"));
            LinkOrUnlinkWithAppleCommand.Subscribe(() => IsLinkedWithApple.Value ? UnlinkWithProvider("apple.com") : LinkWithApple());
            EnrollOrUnenrollMultiFactorCommand.Subscribe(() => IsEnrolledMultiFactor.Value ? UnenrollMultiFactor() : EnrollMultiFactor());
            DeleteCommand.Subscribe(Delete);
        }
Ejemplo n.º 2
0
        public MainPageViewModel(INavigationService navigationService, IPageDialogService pageDialogService, IGoogleService googleService, IFacebookService facebookService, IAppleService appleService)
            : base(navigationService)
        {
            _pageDialogService = pageDialogService;
            _googleService     = googleService;
            _facebookService   = facebookService;
            _appleService      = appleService;

            _multiFactorService = new MultiFactorService(this);

            Title = "Main Page";

            _registration = CrossFirebaseAuth.Current.Instance.AddAuthStateChangedListener((auth) =>
            {
                _isSignedIn.Value = auth.CurrentUser != null;
            });

            ShowUserCommand = _isSignedIn.ToAsyncReactiveCommand();

            SignUpCommand.Subscribe(SignUp);
            SignInWithEmailAndPasswordCommand.Subscribe(SignInWithEmailAndPassword);
            SignInWithGoogleCommand.Subscribe(SignInWithGoogle);
            SignInWithTwitterCommand.Subscribe(() => SignInWithProvider("twitter.com"));
            SignInWithFacebookCommand.Subscribe(SignInWithFacebook);
            SignInWithGitHubCommand.Subscribe(() => SignInWithProvider("github.com"));
            SignInWithYahooCommand.Subscribe(() => SignInWithProvider("yahoo.com"));
            SignInWithMicrosoftCommand.Subscribe(() => SignInWithProvider("microsoft.com"));
            SignInWithAppleCommand.Subscribe(SignInWithApple);
            SignInWithPhoneNumberCommand.Subscribe(SignInWithPhoneNumber);
            SignInAnonymouslyCommand.Subscribe(SignInSignInAnonymously);
            ShowUserCommand.Subscribe(async() => await NavigationService.NavigateAsync <UserPageViewModel>());
        }
        public SignInWithEmailAndPasswordPageViewModel(INavigationService navigationService, IPageDialogService pageDialogService) : base(navigationService)
        {
            _pageDialogService = pageDialogService;

            _multiFactorService = new MultiFactorService(this);

            Title = "Sign In ";

            SignInCommand = new[] {
                Email.Select(s => string.IsNullOrEmpty(s)),
                Password.Select(s => string.IsNullOrEmpty(s))
            }.CombineLatest(x => x.All(y => !y))
            .ToAsyncReactiveCommand();

            SignInCommand.Subscribe(async() =>
            {
                try
                {
                    var result = await CrossFirebaseAuth.Current
                                 .Instance
                                 .SignInWithEmailAndPasswordAsync(Email.Value, Password.Value);

                    await NavigationService.GoBackAsync(result.User);
                }
                catch (FirebaseAuthException e)
                {
                    if (e.Resolver == null)
                    {
                        System.Diagnostics.Debug.WriteLine(e);

                        await _pageDialogService.DisplayAlertAsync("Failure", e.Message, "OK");
                    }
                    else
                    {
                        try
                        {
                            var result = await _multiFactorService.ResolveAsync(e.Resolver);

                            await _pageDialogService.DisplayAlertAsync("Success", result.User.DisplayName, "OK");
                        }
                        catch (Exception ex)
                        {
                            System.Diagnostics.Debug.WriteLine(ex);

                            await _pageDialogService.DisplayAlertAsync("Failure", ex.Message, "OK");
                        }
                    }
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine(e);

                    await _pageDialogService.DisplayAlertAsync("Failure", e.Message, "OK");
                }
            });

            ResetPasswordCommand = new[] {
                Email.Select(s => string.IsNullOrEmpty(s)),
            }.CombineLatest(x => x.All(y => !y))
            .ToAsyncReactiveCommand();

            ResetPasswordCommand.Subscribe(async() =>
            {
                try
                {
                    await CrossFirebaseAuth.Current
                    .Instance
                    .SendPasswordResetEmailAsync(Email.Value);

                    await _pageDialogService.DisplayAlertAsync(null, "Email has been sent.", "OK");
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine(e);

                    await _pageDialogService.DisplayAlertAsync("Failure", e.Message, "OK");
                }
            });
        }