public SocialMediaUser GetUserDetails() { SocialMediaUser smu = new SocialMediaUser(); smu.Email = Preferences.Get("Email", ""); smu.Name = Preferences.Get("Name", ""); return(smu); }
async void OnAuthCompleted(object sender, AuthenticatorCompletedEventArgs e) { var authenticator = sender as OAuth2Authenticator; if (authenticator != null) { authenticator.Completed -= OnAuthCompleted; authenticator.Error -= OnAuthError; } if (e.IsAuthenticated) { if (authenticator.AuthorizeUrl.Host == "www.facebook.com") { FacebookEmail facebookEmail = null; var httpClient = new HttpClient(); var json = await httpClient.GetStringAsync($"https://graph.facebook.com/me?fields=id,name,first_name,last_name,email,picture.type(large)&access_token=" + e.Account.Properties["access_token"]); facebookEmail = JsonConvert.DeserializeObject <FacebookEmail>(json); //await store.SaveAsync(account = e.Account, Constants.AppName); try { await SecureStorageAccountStore.SaveAsync(e.Account, Constants.AppName); } catch { } Application.Current.Properties.Remove("Id"); Application.Current.Properties.Remove("FirstName"); Application.Current.Properties.Remove("LastName"); Application.Current.Properties.Remove("DisplayName"); Application.Current.Properties.Remove("EmailAddress"); Application.Current.Properties.Remove("ProfilePicture"); Application.Current.Properties.Add("Id", facebookEmail.Id); Application.Current.Properties.Add("FirstName", facebookEmail.First_Name); Application.Current.Properties.Add("LastName", facebookEmail.Last_Name); Application.Current.Properties.Add("DisplayName", facebookEmail.Name); Application.Current.Properties.Add("EmailAddress", facebookEmail.Email); Application.Current.Properties.Add("ProfilePicture", facebookEmail.Picture.Data.Url); //await Navigation.PushAsync(new ProfilePage()); // await NavigationService.NavigateTo(typeof(BikeDetailViewModel), string.Empty, string.Empty, true); await NavigationService.NavigateToAsync <BikeDetailViewModel>(); } else { SocialMediaUser user = null; // If the user is authenticated, request their basic user data from Google // UserInfoUrl = https://www.googleapis.com/oauth2/v2/userinfo var request = new OAuth2Request("GET", new Uri(Constants.GoogleUserInfoUrl), null, e.Account); var response = await request.GetResponseAsync(); if (response != null) { // Deserialize the data and store it in the account store // The users email address will be used to identify data in SimpleDB string userJson = await response.GetResponseTextAsync(); user = JsonConvert.DeserializeObject <SocialMediaUser>(userJson); } /*if (account != null) * { * store.Delete(account, Constants.AppName); * } * await store.SaveAsync(account = e.Account, Constants.AppName); */ try { await SecureStorageAccountStore.SaveAsync(e.Account, Constants.AppName); } catch { } Application.Current.Properties.Remove("Id"); Application.Current.Properties.Remove("FirstName"); Application.Current.Properties.Remove("LastName"); Application.Current.Properties.Remove("DisplayName"); Application.Current.Properties.Remove("EmailAddress"); Application.Current.Properties.Remove("ProfilePicture"); Application.Current.Properties.Add("Id", user.Id); Application.Current.Properties.Add("FirstName", user.GivenName); Application.Current.Properties.Add("LastName", user.FamilyName); Application.Current.Properties.Add("DisplayName", user.Name); Application.Current.Properties.Add("EmailAddress", user.Email); Application.Current.Properties.Add("ProfilePicture", user.Picture); //await Navigation.PushAsync(new ProfilePage()); await NavigationService.NavigateToAsync <BikeDetailViewModel>(); // await NavigationService.NavigateTo(typeof(BikeDetailViewModel), string.Empty, string.Empty, true); } } }
private async Task LoginFacebookAsync() { try { //check if logged in if (_facebookClient.IsLoggedIn) { //TODO: Give appropriate handler _facebookClient.Logout(); } EventHandler <FBEventArgs <string> > userDataDelegate = null; userDataDelegate = async(object sender, FBEventArgs <string> e) => { if (e == null) { return; } string url = string.Empty; switch (e.Status) { case FacebookActionStatus.Completed: //Handles the FBEvent handler and deserialize it. This is a custom FB Event Handler, hence you can get e.Data var fbUser = await Task.Run(() => JsonConvert.DeserializeObject <FacebookProfile>(e.Data)); //A check on null picture if (fbUser.Picture.Data.Url != null) { url = fbUser.Picture.Data.Url; } //Assign the deserialize values into a generic socialMediaUser model var socialMediaProfile = new SocialMediaUser { Name = fbUser.FirstName, Email = fbUser.Email, PictureURL = url }; //Login is completed //Send the data to the "Select League" page await App.Current.MainPage.Navigation.PushModalAsync(new SelectLeagues()); break; case FacebookActionStatus.Canceled: //Redirect back to login await App.Current.MainPage.Navigation.PushModalAsync(new LoginPage()); break; } //_facebookClient.OnUserData -= userDataDelegate; }; _facebookClient.OnUserData += userDataDelegate; string[] fbRequestFields = { "email", "first_name", "last_name", "picture" }; string[] fbPermisions = { "email" }; await _facebookClient.RequestUserDataAsync(fbRequestFields, fbPermisions); } catch (Exception e) { //TODO: A proper dialog command //https://github.com/aritchie/userdialogs/blob/master/src/Samples/Samples/ViewModels/StandardViewModel.cs } }
public void SaveUserDetails(SocialMediaUser socialMediaUser) { Preferences.Set("Email", socialMediaUser.Email); Preferences.Set("Name", socialMediaUser.Name); }
private async Task LoginGoogleAsync() { try { //Check if the user is already logged in //potentially will remove this in the future otherwise create appropriate handler if (!string.IsNullOrEmpty((googleClient.AccessToken))) { googleClient.Logout(); } //Create the event/delegate of Google user type and initialize it EventHandler <GoogleClientResultEventArgs <GoogleUser> > userLoginDelegate = null; //Lets do the login userLoginDelegate = async(object sender, GoogleClientResultEventArgs <GoogleUser> e) => { switch (e.Status) { case GoogleActionStatus.Completed: var googleResultString = JsonConvert.SerializeObject(e.Data); //A check with the user profile image is empty string url = string.Empty; if (e.Data.Picture.AbsoluteUri != null) { url = e.Data.Picture.AbsoluteUri; } //map the e.data into our SocialMediaUser model var socialMediaUser = new SocialMediaUser { Email = e.Data.Email, Name = e.Data.Name, PictureURL = url }; //Once the model is set, send it to the selectLeague page await App.Current.MainPage.Navigation.PushModalAsync(new SelectLeagues()); break; case GoogleActionStatus.Canceled: /*For now we will display an alert here but we will move this elsewhere * so that we can just return a msg into that VM and display it properly * and finally navigate to login page */ await App.Current.MainPage.DisplayAlert("Google Login", "Google Login Cancelled by user", "OK"); await App.Current.MainPage.Navigation.PushModalAsync(new LoginPage()); break; case GoogleActionStatus.Error: /*For now we will display an alert here but we will move this elsewhere * so that we can just return a msg into that VM and display it properly * and finally navigate to login page */ await App.Current.MainPage.DisplayAlert("Google Login", "Error from Google signing in", "OK"); await App.Current.MainPage.Navigation.PushModalAsync(new LoginPage()); break; } googleClient.OnLogin -= userLoginDelegate; }; googleClient.OnLogin += userLoginDelegate; await googleClient.LoginAsync(); } catch (Exception e) { Debug.WriteLine(e.ToString()); //await App.Current.MainPage.DisplayAlert("Google Login", e.Message, "OK"); } }