Skip to content
Permalink
Browse files

Merge pull request #3 from danpdc/try#2-Persistent_authentication

Persist authentication and logout functionality
  • Loading branch information...
danpdc committed May 28, 2019
2 parents 5a09dbc + 58f0716 commit c030bc30dba44ae1976056bd1b0d706d2e5ce09c
@@ -46,17 +46,31 @@ public async Task Login(PostModel model)

public async Task Logout()
{
await _storage.RemoveItemAsync("token");
IsLoggedIn = false;
User = null;
}

public async Task<bool> CheckIfAuthenticated()
{
var token = await _storage.GetItemAsync<string>("token");
if (token != null)
{
Token = token;
User = await _apiClient.GetLoggedInUser(Token);
return true;
}
else
return false;
}

private async Task SaveToken(HttpResponseMessage response)
{
var responseContent = await response.Content.ReadAsStringAsync();
var jwt = Json.Deserialize<GitHubTokenResponse>(responseContent);
Token = jwt.access_token;
//await _storage.SetItemAsync("token", jwt.access_token);
await _storage.SetItemAsync("token", jwt.access_token);
//var token = await _storage.GetItemAsync<string>("token");
}

}
}
@@ -21,7 +21,7 @@ public GitHubApiClient(HttpClient client)
{
_client = client;
_client.BaseAddress = new Uri("https://gdblazor.azurewebsites.net");
_client.DefaultRequestHeaders.Add("User-Agent", "Git Dashboard with Blazor Prod");
_client.DefaultRequestHeaders.Add("User-Agent", "Git Dashboard with Blazor Dev");
}

public async Task<User> GetLoggedInUser(string token)
@@ -2,30 +2,39 @@
@using GitHubDashboard.Web.Features.Login;
@using GitHubDashboard.Web.Shared;

@if (AppState.IsLoggedIn)

@if (AuthCheckProcessCompleted == false)
{
<em>Loading....</em>
}
else
{
<div class="wrapper">
@if (IsAuthenticated)
{
<div class="wrapper">
<Sidebar></Sidebar>
<div class="main-panel">
<NavMenu></NavMenu>
<div class="content">
@Body
<footer class="footer">
<div class="container">
<div class="copyright">
&copy;
@CurrentYear. #Blazor FTW!
<div class="wrapper">
<Sidebar></Sidebar>
<div class="main-panel">
<NavMenu></NavMenu>
<div class="content">
@Body
<footer class="footer">
<div class="container">
<div class="copyright">
&copy;
@CurrentYear. #Blazor FTW!
</div>
</div>
</div>
</footer>
</footer>
</div>
</div>
</div>
</div>
</div>
}
else
{
<Login></Login>
}
else
{
<Login></Login>
}
}


@@ -12,8 +12,19 @@ public class MainLayoutModel : LayoutComponentBase
public MainLayoutModel()
{
CurrentYear = DateTime.Now.Year.ToString();
IsAuthenticated = false;
AuthCheckProcessCompleted = false;
}
public string CurrentYear { get; set; }
public bool IsAuthenticated { get; set; }
public bool AuthCheckProcessCompleted { get; set; }

protected async override Task OnAfterRenderAsync()
{
IsAuthenticated = await AppState.CheckIfAuthenticated();
AuthCheckProcessCompleted = true;
StateHasChanged();
}

}
}
@@ -1,14 +1,18 @@
using System;
using System.Threading.Tasks;
using GitHubDashboard.Web.Core;
using Microsoft.AspNetCore.Components;

namespace GitHubDashboard.Web.Shared
{
public class NavMenuModel : ComponentBase
{
[Inject] protected AppState AppState { get; set; }
[Inject] IUriHelper UriHelper { get; set; }
public async Task Logout()
{

await AppState.Logout();
UriHelper.NavigateTo("/");
}
}
}

0 comments on commit c030bc3

Please sign in to comment.
You can’t perform that action at this time.