Example #1
0
        public static Task LoginWithEmailAndPassword(string email, string password)
        {
            return(Task.Factory.StartNew(() =>
            {
                try
                {
                    var authProvider = new FirebaseAuthProvider(new FirebaseConfig(FireCore.FIREBASE_KEY));
                    auth = authProvider.SignInWithEmailAndPasswordAsync(email, password).Result;



                    Console.WriteLine("First Token: " + auth.FirebaseToken);
                    expirationTime = Time.CurrentTimeMillis() + (auth.ExpiresIn * 1000);
                    Settings.RefreshToken = auth.RefreshToken;
                    Settings.LastUserID = auth.User.LocalId;
                    Settings.LastAuthToken = auth.FirebaseToken;
                    //RenewAuthToken(auth.RefreshToken);
                    if (!Settings.ApnsToken.Equals(string.Empty))
                    {
                        FireDatabase.write("users/" + auth.User.LocalId + "/device_info/apns_token", Settings.ApnsToken);
                    }

                    Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
                    {
                        UIInstance.nav.PushModalAsync(new MainPage());
                    });
                }
                catch (Exception e)
                {
                    auth = null;
                    Console.WriteLine("Bad Login Info! ");
                    Console.WriteLine(e.StackTrace);
                    Console.WriteLine(e.GetBaseException());
                    Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
                    {
                        UIInstance.app.MainPage.DisplayAlert("ERROR", "Unable to log you in. Check password or internet connection.", "OK");
                    });
                }

                /*
                 * var firebase = new FirebaseClient("https://droverrideshare-69d73.firebaseio.com/");
                 * var dinos = firebase
                 *      .Child("city-lookups");
                 * var query = QueryExtensions.WithAuth(dinos, auth.FirebaseToken).OnceAsync<Object>().Result;
                 *
                 * foreach (var item in query)
                 * {
                 *      Console.WriteLine(item.Key + ", " + item.Object);
                 * }
                 */
            }));
        }
Example #2
0
        public static Task SignUpWithEmailAndPassword(string email, string password, string first_name, string last_name, long phone)
        {
            return(Task.Factory.StartNew(() =>
            {
                try
                {
                    var authProvider = new FirebaseAuthProvider(new FirebaseConfig(FireCore.FIREBASE_KEY));
                    auth = authProvider.CreateUserWithEmailAndPasswordAsync(email, password).Result;


                    expirationTime = Time.CurrentTimeMillis() + (auth.ExpiresIn * 1000);
                    Settings.RefreshToken = auth.RefreshToken;
                    Settings.LastUserID = auth.User.LocalId;
                    Settings.LastAuthToken = auth.FirebaseToken;

                    /*FireDatabase.write("users/"+auth.User.LocalId ,"temp");*/
                    FireDatabase.write("users/" + auth.User.LocalId + "/is_driver", true);
                    FireDatabase.write("users/" + auth.User.LocalId + "/personal_info/name_first", first_name);
                    FireDatabase.write("users/" + auth.User.LocalId + "/personal_info/name_last", last_name);
                    FireDatabase.write("users/" + auth.User.LocalId + "/personal_info/phone", phone);
                    Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
                    {
                        UIInstance.nav.PushModalAsync(new MainPage());
                    });
                }
                catch (Exception e)
                {
                    auth = null;
                    Console.WriteLine(e);
                    Console.WriteLine("Bad Login Info!");
                    Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
                    {
                        UIInstance.app.MainPage.DisplayAlert("ERROR", "Unable to Create Account. Check your email and internet.", "OK");
                    });
                }

                /*
                 * var firebase = new FirebaseClient("https://droverrideshare-69d73.firebaseio.com/");
                 * var dinos = firebase
                 *      .Child("city-lookups");
                 * var query = QueryExtensions.WithAuth(dinos, auth.FirebaseToken).OnceAsync<Object>().Result;
                 *
                 * foreach (var item in query)
                 * {
                 *      Console.WriteLine(item.Key + ", " + item.Object);
                 * }
                 */
            }));
        }
 void Handle_DriverSwitch_Toggled(object sender, Xamarin.Forms.ToggledEventArgs e)
 {
     Console.WriteLine("TOGGLED THE DRIVER SWITCH!");
     if (driver_switch.IsToggled)
     {
         GenericSocket.REQUEST_DRIVER_MODE();
         DependencyService.Get <IRegisterDriverMode>().Register();
         //set_pickup_button.Is
     }
     else
     {
         DependencyService.Get <IRegisterDriverMode>().UnRegister();
         FireDatabase.write("users/" + FireAuth.auth.User.LocalId + "/stage_1_driving", null);
     }
 }
Example #4
0
 public static Task InitFireAuth()
 {
     return(Task.Factory.StartNew(() =>
     {
         //CHECK TO SEE IF WE HAVE A VALID ACCOUNT ON THIS DEVICE
         if (!Settings.RefreshToken.Equals(string.Empty))
         {
             //CHECK TO SEE IF OUR AUTH TOKEN NEEDS RENEWING
             if (isTokenExpired())
             //ATTEMPT TO RENEW OUR AUTH TOKEN
             {
                 bool success = RenewAuthToken(Settings.RefreshToken).Result;
                 if (success)
                 {
                     //IF TOKEN WAS RENEWED. GO ONTO MAIN PAGE.
                     Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
                     {
                         //UIInstance.app.MainPage.Navigation.PushModalAsync(new MainPage());
                     });
                     return;
                 }
                 //ELSE. CONTINUE TO PUSH THE LAUNCH PAGE.
             }
             //SEND US TO THE MAIN PAGE. WE ARE ALREADY LOGGED IN.
             else
             {
                 if (!Settings.ApnsToken.Equals(string.Empty))
                 {
                     FireDatabase.write("users/" + auth.User.LocalId + "/device_info/apns_token", Settings.ApnsToken);
                 }
                 Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
                 {
                     UIInstance.app.MainPage.Navigation.PushModalAsync(new MainPage());
                 });
                 return;
             }
         }
         //SEND US TO THE LOGIN PAGE. WE ARE LOGGED OUT.
         Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
         {
             UIInstance.app.MainPage.Navigation.PushModalAsync(new LaunchPage());
         });
     }));
 }
Example #5
0
        public static Task <Boolean> RenewAuthToken(string refreshToken)
        {
            return(Task <Boolean> .Factory.StartNew(() =>
            {
                string responseData = "N/A";
                HttpClient client = new HttpClient();

                try
                {
                    var tokenPost = $"{{\"grant_type\":\"refresh_token\",\"refresh_token\":\"{refreshToken}\"}}";
                    var response = client.PostAsync(new Uri("https://securetoken.googleapis.com/v1/token?key=" + FireCore.FIREBASE_KEY), new StringContent(tokenPost, Encoding.UTF8, "application/json")).Result;
                    responseData = response.Content.ReadAsStringAsync().Result;
                    response.EnsureSuccessStatusCode();
                    FirebaseAuthLink test = JsonConvert.DeserializeObject <FirebaseAuthLink>(responseData);
                    Console.WriteLine("REFRESH TOKEN FROM: " + test.RefreshToken);
                    var auth2 = JsonConvert.DeserializeObject <Dictionary <string, Object> >(responseData);

                    //auth.FirebaseToken = auth2["access_token"].ToString();
                    Settings.LastAuthToken = auth2["access_token"].ToString();
                    //auth.RefreshToken = auth2["refresh_token"].ToString();
                    Settings.RefreshToken = auth2["refresh_token"].ToString();
                    var u = new User();
                    u.LocalId = Settings.LastUserID;
                    string stringData = $"{{\"localId\":\"{Settings.LastUserID}\", \"idToken\":\"{Settings.LastAuthToken}\"}}";

                    FirebaseAuthLink tempAuth = JsonConvert.DeserializeObject <FirebaseAuthLink>(stringData);
                    tempAuth.User = u;
                    auth = tempAuth;
                    expirationTime = Time.CurrentTimeMillis() + (long.Parse(auth2["expires_in"].ToString()) * 1000);
                    if (!Settings.ApnsToken.Equals(string.Empty))
                    {
                        FireDatabase.write("users/" + auth.User.LocalId + "/device_info/apns_token", Settings.ApnsToken);
                    }
                    return true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("ERROR");
                    Console.WriteLine(ex);
                    return false;
                }
            }));
        }