public async Task <TokenResponse> LoginFacebook( string urlBase, string servicePrefix, string controller, Models.FacebookResponse profile) { try { var request = JsonConvert.SerializeObject(profile); var content = new StringContent( request, Encoding.UTF8, "application/json"); var client = new HttpClient(); client.BaseAddress = new Uri(urlBase); var url = string.Format("{0}{1}", servicePrefix, controller); var response = await client.PostAsync(url, content); if (!response.IsSuccessStatusCode) { return(null); } var tokenResponse = await GetToken( urlBase, profile.Id, profile.Id); return(tokenResponse); } catch { return(null); } }
public static async Task NavigateToProfile(Models.FacebookResponse profile) { if (profile == null) { Application.Current.MainPage = new NavigationPage(new LoginPage()); return; } var apiService = new ApiService(); var dataService = new DataService(); var apiSecurity = Application.Current.Resources["APISecurity"].ToString(); var token = await apiService.LoginFacebook( apiSecurity, "/api", "/Users/LoginFacebook", profile); if (token == null) { Application.Current.MainPage = new NavigationPage(new LoginPage()); return; } var user = await apiService.GetUserByEmail( apiSecurity, "/api", "/Users/GetUserByEmail", token.TokenType, token.AccessToken, token.UserName); UserLocal userLocal = null; if (user != null) { userLocal = Converter.ToUserLocal(user); dataService.DeleteAllAndInsert(userLocal); dataService.DeleteAllAndInsert(token); } var mainViewModel = MainViewModel.GetInstance(); mainViewModel.Token = token.AccessToken; mainViewModel.User = userLocal; mainViewModel.Bibles = new BiblesViewModel(); Application.Current.MainPage = new MasterPage(); Settings.IsRemembered = "true"; }