예제 #1
0
        async void Change_username_btn_Clicked(System.Object sender, System.EventArgs e)
        {
            var  user        = new MyUser();
            var  newUsername = New_username_entry.Text;
            bool usernameOk;
            var  authProvider = new FirebaseAuthProvider(new FirebaseConfig(WebAPIKey));

            void UsernameValidation()
            {
                //deve contenere tra i 6 e i 18 caratteri alfanumerici
                var userPattern = "[A-Za-z][A-Za-z0-9._]{5,17}";

                if (newUsername != null)
                {
                    if (Regex.IsMatch(newUsername, userPattern))
                    {
                        usernameOk = true;
                        LabelUserError.TextColor = Color.Transparent;
                    }

                    else
                    {
                        usernameOk = false;
                        LabelUserError.TextColor = Color.Red;
                    }
                }
                else
                {
                    usernameOk = false;
                    LabelUserError.TextColor = Color.Red;
                }
            }

            UsernameValidation();

            if (usernameOk)
            {
                try
                {
                    //This is the saved firebaseauthentication that was saved during the time of login
                    var savedfirebaseauth = JsonConvert.DeserializeObject <Firebase.Auth.FirebaseAuth>(Preferences.Get("MyLoginToken", ""));
                    //Here we are Refreshing the token
                    var RefreshedContent = await authProvider.RefreshAuthAsync(savedfirebaseauth);

                    Preferences.Set("MyLoginToken", JsonConvert.SerializeObject(RefreshedContent));
                    //Cambio password utente
                    await authProvider.UpdateProfileAsync(savedfirebaseauth.FirebaseToken, newUsername, "");

                    await App.Current.MainPage.DisplayAlert("Success.", "New Username set correctly.", "Ok");

                    await Navigation.PushAsync(new LoginPage());
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    await App.Current.MainPage.DisplayAlert("Ops... Something went wrong.", "Try to logout and log back in.", "Ok");
                }
            }
        }
예제 #2
0
        //procedura di sign up
        async void Sign_up_btn_Clicked(object sender, EventArgs e)
        {
            MyUser user           = new MyUser();
            string username       = Entry_Username.Text;
            string emailLowerCase = Entry_Email.Text;

            if (Entry_Email.Text != null)
            {
                emailLowerCase = Entry_Email.Text.ToLower();
            }
            string pwd = Entry_Password.Text;

            bool isEmailOk    = MailValidation(emailLowerCase);
            bool isPassConfOk = PasswordConfirmationValidation(user);
            bool isUsernameOk = UsernameValidation(username);
            bool isPassOk     = PasswordValidation(pwd);
            bool isAgeOk      = AgeValidation();

            //bool test = true;

            //se tutti i campi sono rispettati la procedura ha successos
            if (isEmailOk && isPassConfOk && isUsernameOk && isPassOk && isAgeOk)
            {
                try
                {
                    var authProvider = new FirebaseAuthProvider(new FirebaseConfig(AuthFirebase.Instance.GetKey()));
                    //signup con email e password
                    var auth = await authProvider.CreateUserWithEmailAndPasswordAsync(Entry_Email.Text, Entry_Password.Text);

                    //invia email di verifica
                    await authProvider.SendEmailVerificationAsync(auth);

                    await authProvider.UpdateProfileAsync(auth.FirebaseToken, username, "");

                    Preferences.Set("UserEmailFirebase", emailLowerCase.Replace(".", "-").Replace("@", "-at-"));
                    //alert
                    await App.Current.MainPage.DisplayAlert("Success!", "Don't forget to verify your Email.", "Ok");

                    //aggiunto utente nel database
                    var viewModel = (MyUserViewModel)BindingContext;
                    if (viewModel.AddUserCommand.CanExecute(null))
                    {
                        viewModel.AddUserCommand.Execute(null);
                    }

                    //navigazione alla pagina di login
                    await Navigation.PushAsync(new LoginPage());
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    await App.Current.MainPage.DisplayAlert("Ops... Something went wrong.", "Try to Sign Up again.", "Ok");
                }
            }
        }
        public async Task UpdateUserInfo()
        {
            try
            {
                if (!Model.FirebaseHelper.Instance.IsLoggedIn)
                {
                    return;
                }
                await authProvider.UpdateProfileAsync(
                    FirebaseHelper.Instance.Token,
                    FirebaseHelper.Instance.User.DisplayName,
                    FirebaseHelper.Instance.User.PhotoUrl);

                var querier = FirebaseHelper.Instance.Querier();
                await querier.Child("Users/" + FirebaseHelper.Instance.User.LocalId).
                PutAsync(Utilities.User2JsonDB(FirebaseHelper.Instance.User));
            }
            catch (FirebaseAuthException e)
            {
                throw e;
            }
        }