private async void RegisterCommandExecute() { try { ShowLoading = true; var user = new User { Email = Email, Name = Name, Password = Password, Phone = Phone, Picture = Image }; await App.ApiService.CreateUser(user); _pushNotification.RegisterPush(); await _dialogService.DisplayAlertAsync("Sucesso", "Usuário criado com sucesso.", "Ok"); await _navigationService.NavigateAsync($"app:///NavigationPage/{nameof(LoginPage)}"); } catch (Exception ex) { if (ex is Refit.ApiException) { var refitEx = ex as Refit.ApiException; await _dialogService.DisplayAlertAsync("Erro!", refitEx.Content, "Ok"); } Debug.WriteLine(ex.StackTrace); } finally { ShowLoading = false; } }
public async Task <bool> LoginAsync() { try { Initialize(); var user = await _auth.LoginAsync(Client, MobileServiceAuthenticationProvider.Facebook); if (user == null) { Settings.FacebookAuthToken = string.Empty; Settings.FacebookUserId = string.Empty; Device.BeginInvokeOnMainThread(async() => { await Application.Current.MainPage.DisplayAlert("Ops!", "Nao conseguimos efetuar seu login, tente novamente", "OK"); }); return(false); } else { var newUser = false; identities = await Client.InvokeApiAsync <List <AppServiceIdentity> >("/.auth/me"); var name = identities[0].UserClaims.Find(c => c.Type.Equals("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name")).Value; var email = identities[0].UserClaims.Find(c => c.Type.Equals("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress")).Value; var appUser = await App.ApiService.GetUserByEmail(email); var userToken = identities[0].AccessToken; var requestUrl = $"https://graph.facebook.com/v2.9/me/?fields=picture.width(640)&access_token={userToken}"; var httpClient = new HttpClient(); var userJson = await httpClient.GetStringAsync(requestUrl); var facebookProfile = JsonConvert.DeserializeObject <FacebookProfile>(userJson); if (appUser == null) { appUser = new User { Email = email, FacebookId = Settings.FacebookUserId, Name = name, PictureUrl = facebookProfile.Picture.Data.Url }; appUser = await App.ApiService.CreateFacebookUser(appUser); newUser = true; } var loginResponse = await App.ApiService.LoginFacebook(new Login { Email = appUser.Email, Password = Settings.FacebookUserId }); if (loginResponse != null) { Settings.AuthToken = loginResponse.AuthToken; Settings.UserId = loginResponse.UserId.ToString(); } if (!appUser.PictureUrl.Equals(facebookProfile.Picture.Data.Url)) { try { appUser.PictureUrl = facebookProfile.Picture.Data.Url; await App.ApiService.UpdateUser(appUser.Email, appUser, "bearer " + Settings.AuthToken); } catch { } } if (newUser) { _pushNotification.RegisterPush(); } return(true); } } catch (Exception ex) { Debug.WriteLine(ex.StackTrace); return(false); } }