public async Task<bool> TryRefreshToken() { UserAppSession usuarioSessao = GetUsuario(); ApiReturn retorno; retorno = await Login(usuarioSessao.PhoneNumber, usuarioSessao.Token); return retorno.Success; }
public async Task<ApiReturn> Login(string phoneNumber, string token) { var api = new ApiLoginService(); api.TryLoginUser = false; ApiReturn<UserAppSessionSerializeModel> objectReturned = await api.Login(phoneNumber, token); if (objectReturned.Success) { UserAppSession sessao = objectReturned.Object.ConvertToSession(); sessao.Logged = true; sessao.PhoneNumber = phoneNumber; sessao.Token = token; SaveSessionUser(sessao); } return objectReturned.NoType(); }
public void SaveSessionUser(UserAppSession UserAppSession) { using (Realm realm = CreateNewRealmInstance()) { var userSession = GetUser(realm); using (var trans = realm.BeginWrite()) { userSession.Id = Convert.ToInt64(UserAppSession.Id); userSession.PhoneNumber = UserAppSession.PhoneNumber; userSession.Token = UserAppSession.Token; userSession.TutorialVisualized = UserAppSession.TutorialVisualized; userSession.Logged = UserAppSession.Logged; realm.Add(userSession, true); trans.Commit(); } } }
protected override void OnInitialized() { InitializeComponent(); var userService = new UserService(); UserAppSession userAppSession = userService.GetUsuario(true); AgendaCCBApiService.BehaviorIfNotLogged = async() => { await NavigationService.NavigateAsync("NavigationPage/MainPage"); }; if (userAppSession.Logged) { NavigationService.NavigateAsync($"NavigationPage/{AppSettings.HomeApplication}"); } else { NavigationService.NavigateAsync("NavigationPage/MainPage"); } }
private void PrepareHeaders(HttpClient client, Dictionary <string, string> headers, bool basicAuthentication) { string authKey = "Authorization"; if (headers == null) { headers = new Dictionary <string, string>(); } if (headers.ContainsKey(authKey)) { headers.Remove(authKey); } if (basicAuthentication) { headers.Add(authKey, BasicAuthHeader); } else { var userService = new UserService(); UserAppSession userAppSession = userService.GetUsuario(true); if (!string.IsNullOrEmpty(userAppSession.Token)) { headers.Add(authKey, $"Bearer {userAppSession.Token}"); } } foreach (var item in headers) { if (client.DefaultRequestHeaders.Contains(item.Key)) { client.DefaultRequestHeaders.Remove(item.Key); } client.DefaultRequestHeaders.Add(item.Key, item.Value); } }