Skip to content

Commit

Permalink
authentication web test refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
djordjedjukic committed Feb 5, 2021
1 parent 707a3dc commit 7a366d6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
40 changes: 35 additions & 5 deletions src/WebTest/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using Hanssens.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System.Diagnostics;
Expand Down Expand Up @@ -42,17 +43,46 @@ public IActionResult GoogleAuth()
string clientSecret = _config.GetSection("GoogleData").GetSection("ClientSecret").Value;
string redirectUrl = "https://localhost:44366/home/authentication";

var url = $"https://accounts.google.com/o/oauth2/v2/auth?redirect_uri={redirectUrl}&prompt=consent&response_type=code&client_id={clientId}&scope=profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email&access_type=offline";
var url = $"https://accounts.google.com/o/oauth2/v2/auth?redirect_uri={redirectUrl}&prompt=consent&response_type=code&client_id={clientId}&scope=profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email&access_type=offline";
return Redirect(url);
}

public IActionResult Authentication(string code)
{
using (var client = new HttpClient())
var storage = new LocalStorage();
if (!storage.Exists("jwtToken") || string.IsNullOrEmpty(storage.Get("jwtToken").ToString()))
{
var jwtToken = GetGoogleToken(code, client);
using (var client = new HttpClient())
{
var jwtToken = GetGoogleToken(code, client);
storage.Store("jwtToken", jwtToken);
storage.Persist();
}
}

return Redirect("Index");
}

public IActionResult Logout()
{
var storage = new LocalStorage();
if (storage.Exists("jwtToken"))
{
storage.Store("jwtToken", "");
storage.Persist();
}

return Redirect("Index");
}

if (!string.IsNullOrEmpty(jwtToken))
public IActionResult CreateEntry()
{
var storage = new LocalStorage();
var jwtToken = storage.Get("jwtToken").ToString();

if (!string.IsNullOrEmpty(jwtToken))
{
using (var client = new HttpClient())
{
var newEntry = new
{
Expand Down
24 changes: 20 additions & 4 deletions src/WebTest/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
@{
ViewData["Title"] = "Home Page";
@using Hanssens.Net;

@{

ViewData["Title"] = "Home Page";
var storage = new LocalStorage();
var jwtToken = storage.Exists("jwtToken") ? storage.Get("jwtToken").ToString() : "";
var isAuthenticated = storage.Exists("jwtToken") && !string.IsNullOrEmpty(jwtToken);
}

<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>


@if (isAuthenticated)
{
<input type="button" class="btn btn-primary" value="Create Entry" onclick="location.href='@Url.Action("CreateEntry", "Home")'" />
<input type="button" class="btn btn-primary" value="Logout" onclick="location.href='@Url.Action("Logout", "Home")'" />
}
else
{
<input type="button" class="btn btn-primary" value="Continue with Google" onclick="location.href='@Url.Action("GoogleAuth", "Home")'" />
}


<input type="button" class="btn btn-primary" title="Delete" value="Continue with Google" onclick="location.href='@Url.Action("GoogleAuth", "Home")'" />


<h1>@ViewBag.CreatedEntryId</h1>
Expand Down
1 change: 1 addition & 0 deletions src/WebTest/WebTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LocalStorage" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
</ItemGroup>

Expand Down

0 comments on commit 7a366d6

Please sign in to comment.