public async Task <TokenViewModel> Login(LoginViewModel loginViewModel = null) { string token; TokenViewModel result = null; if (loginViewModel == null) { token = await localStorage.GetItem <string>("token"); } else { result = await Http.PostJsonAsync <TokenViewModel>("/api/Token/Login", loginViewModel); token = result.Token; await localStorage.SetItem <string>("token", token); } IsAuthorize = token != null; if (IsAuthorize) { Http.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}"); try { await UpdateUserData(); } catch { await Logout(); } UpdateAfterAuthorization?.Invoke(); } return(result); }
public async Task Logout() { await localStorage.RemoveItem("token"); Http.DefaultRequestHeaders.Remove("Authorization"); IsAuthorize = false; DataAboutUser = null; UpdateAfterAuthorization.Invoke(); }