Skip to content

Commit

Permalink
Skip login page if user is already logged in
Browse files Browse the repository at this point in the history
  • Loading branch information
usercode committed Oct 14, 2022
1 parent 9bee806 commit fb376ec
Show file tree
Hide file tree
Showing 38 changed files with 175 additions and 151 deletions.
18 changes: 5 additions & 13 deletions src/DragonFly.API.Client/ClientContentService.ContentSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,11 @@
// https://github.com/usercode/DragonFly
// MIT License

using DragonFly.AspNetCore;
using DragonFly.AspNetCore.Exports;
using DragonFly.AspNetCore.API.Exports;
using DragonFly.AspNetCore.API.Models;
using DragonFly.Content;
using DragonFly.Contents.Content;
using DragonFly.Core;
using DragonFly.Data;
using DragonFly.Data.Models;
using DragonFly.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;

namespace DragonFly.Client;

Expand Down Expand Up @@ -63,6 +50,11 @@ public async Task UpdateAsync(ContentSchema entity)
await Client.PutAsJsonAsync($"api/schema", entity.ToRest());
}

public async Task DeleteAsync(ContentSchema entity)
{
await Client.DeleteAsync($"api/schema/{entity.Id}");
}

public async Task<QueryResult<ContentSchema>> QuerySchemasAsync()
{
try
Expand Down
19 changes: 8 additions & 11 deletions src/DragonFly.API/Extensions/ContentSchemaApiExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,12 @@
// MIT License

using DragonFly.AspNet.Middleware;
using DragonFly.AspNetCore.API.Exports;
using DragonFly.AspNetCore.API.Middlewares;
using DragonFly.AspNetCore.API.Middlewares.ContentSchemas;
using DragonFly.AspNetCore.API.Models;
using DragonFly.AspNetCore.Exports;
using DragonFly.Content;
using DragonFly.Builders;
using DragonFly.Data.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DragonFly.AspNetCore.API.Middlewares.ContentSchemas;

Expand All @@ -32,6 +21,7 @@ public static void MapContentSchemaRestApi(this IDragonFlyEndpointRouteBuilder e
endpoints.MapGet("api/schema/{name}", MapGetByName);
endpoints.MapPost("api/schema", MapCreate);
endpoints.MapPut("api/schema", MapUpdate);
endpoints.MapDelete("api/schema/{id:guid}", MapDelete);
}

private static async Task<QueryResult<RestContentSchema>> MapQuery(HttpContext context, ISchemaStorage storage)
Expand Down Expand Up @@ -83,4 +73,11 @@ private static async Task MapUpdate(HttpContext context, ISchemaStorage storage,

await storage.UpdateAsync(m);
}

private static async Task MapDelete(HttpContext context, ISchemaStorage storage, Guid id)
{
ContentSchema schema = await storage.GetSchemaAsync(id);

await storage.DeleteAsync(schema);
}
}
6 changes: 6 additions & 0 deletions src/DragonFly.App.Server/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
RUN sed -i'.bak' 's/$/ contrib/' /etc/apt/sources.list
RUN apt-get update
RUN apt-get install -y ttf-mscorefonts-installer fontconfig
RUN apt-get install -y --no-install-recommends libgdiplus libc6-dev \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
EXPOSE 80
EXPOSE 443
Expand Down
1 change: 1 addition & 0 deletions src/DragonFly.BlockField/BlockFieldManagerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Threading.Tasks;

namespace DragonFly.BlockField;

public static class BlockFieldManagerExtensions
{
public static void AddDefaults(this BlockFieldManager manager)
Expand Down
5 changes: 5 additions & 0 deletions src/DragonFly.Core/ContentItems/Queries/ContentItemQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public ContentItemQuery()
/// </summary>
public bool IncludeListFieldsOnly { get; set; }

/// <summary>
/// UsedAsset
/// </summary>
public Guid? UsedAsset { get; set; }

/// <summary>
/// Published
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions src/DragonFly.Core/ContentSchemas/ISchemaStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public interface ISchemaStorage

Task UpdateAsync(ContentSchema entity);

Task DeleteAsync(ContentSchema entity);

//IList<ContentSchema> QueryContentSchemas()
//{
// return QueryContentSchemas(new QueryParameters());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>true</ImplicitUsings>
<Authors>usercode</Authors>
<PackageProjectUrl>https://github.com/usercode/DragonFly</PackageProjectUrl>
<Description>Headless CMS based on ASP.NET Core and Blazor</Description>
Expand Down
15 changes: 9 additions & 6 deletions src/DragonFly.Identity.AspNetCore/Middlewares/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,31 @@
// MIT License

using DragonFly.AspNet.Middleware;
using DragonFly.AspNetCore.Exports;
using DragonFly.AspNetCore.Identity.Middlewares.Roles;
using DragonFly.AspNetCore.Identity.Middlewares.Users;
using DragonFly.Identity;
using DragonFly.Security;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DragonFly.AspNetCore.Identity.Middlewares;

internal static class Extensions
{
public static IDragonFlyEndpointRouteBuilder MapIdentityApi(this IDragonFlyEndpointRouteBuilder endpoints)
{
endpoints.MapGet("identity/CurrentUser", CurrentUserAsync);
endpoints.MapUserApi();
endpoints.MapRoleApi();

return endpoints;
}

private static async Task CurrentUserAsync(HttpContext context, ILoginService service)
{
IdentityUser? currentUser = await service.GetCurrentUserAsync();

await context.Response.WriteAsJsonAsync(currentUser);
}
}
10 changes: 0 additions & 10 deletions src/DragonFly.Identity.AspNetCore/Middlewares/LoginMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,7 @@
// MIT License

using DragonFly.AspNetCore.Exports;
using DragonFly.AspNet.Options;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using DragonFly.Security;

namespace DragonFly.AspNetCore.API.Middlewares.Logins;
Expand Down
28 changes: 23 additions & 5 deletions src/DragonFly.Identity.AspNetCore/Services/LoginService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@
using DragonFly.AspNetCore.Identity.MongoDB;
using DragonFly.AspNetCore.Identity.MongoDB.Models;
using DragonFly.AspNetCore.Identity.MongoDB.Services.Base;
using DragonFly.AspNetCore.Identity.MongoDB.Storages.Models;
using DragonFly.Security;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;

namespace DragonFly.Identity.AspNetCore.Services;

Expand Down Expand Up @@ -77,4 +73,26 @@ public async Task Logout()
{
await HttpContextAccessor.HttpContext!.SignOutAsync();
}

public async Task<IdentityUser?> GetCurrentUserAsync()
{
if (HttpContextAccessor.HttpContext!.User.Identity is ClaimsIdentity claimsIdentity)
{
Claim? claimUserId = claimsIdentity.Claims.FirstOrDefault(x => x.Type == "UserId");

if (claimUserId != null)
{
Guid userIdGuid = Guid.Parse(claimUserId.Value);

MongoIdentityUser? currentUser = await Store.Users.AsQueryable().FirstOrDefaultAsync(x => x.Id == userIdGuid);

if (currentUser != null)
{
return currentUser.ToModel(Store);
}
}
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@

using DragonFly.AspNetCore.Identity.MongoDB.Services.Base;
using Microsoft.AspNetCore.Cryptography.KeyDerivation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace DragonFly.AspNetCore.Identity.MongoDB.Services;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>true</ImplicitUsings>
<Authors>usercode</Authors>
<PackageProjectUrl>https://github.com/usercode/DragonFly</PackageProjectUrl>
<Description>Headless CMS based on ASP.NET Core and Blazor</Description>
Expand Down
44 changes: 30 additions & 14 deletions src/DragonFly.Identity.Razor/Logins/Login.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,27 @@
@inject IOptions<GeneralOptions> Options
@inject IDragonFlyApi Api

<div style="display:flex; flex: 1; justify-content:center">
@if (ShowLoginPage)
{
<div style="display:flex; flex: 1; justify-content:center">
<div class="form-signin" style="width: 100%; max-width: 350px; padding: 15px; margin: auto;">
<h1 class="h3 mb-3 font-weight-normal">@Options.Value.Name</h1>

<div class="form-floating mb-3">
<input @bind-value="Username" type="email" class="form-control" id="floatingInput" placeholder="Username">
<label for="floatingInput">Username</label>
<input @bind-value="Username" type="email" class="form-control" id="floatingInput" placeholder="Username">
<label for="floatingInput">Username</label>
</div>
<div class="form-floating">
<input @bind-value="Password" type="password" class="form-control" id="floatingPassword" placeholder="Password">
<label for="floatingPassword">Password</label>
<input @bind-value="Password" type="password" class="form-control" id="floatingPassword" placeholder="Password">
<label for="floatingPassword">Password</label>
</div>

<button class="btn btn-lg btn-primary btn-block mt-3" @onclick="x => LoginAsync()">Login</button>
<p class="mt-5 mb-3 text-muted"@DateTime.Now.Year</p>

</div>
</div>
</div>
}

<p style="text-align:center">Version: @Api.GetVersion()</p>

Expand All @@ -35,6 +38,8 @@
[Parameter]
public string Password { get; set; }

public bool ShowLoginPage { get; set; }

protected override async Task OnInitializedAsync()
{
//var r = await HttpClient.GetAsync("api/Login/IsLoggedIn");
Expand All @@ -46,10 +51,21 @@
// return;
//}
#if DEBUG
Username = DefaultSecurity.DefaultUsername;
Password = DefaultSecurity.DefaultPassword;
#endif
#if DEBUG
Username = DefaultSecurity.DefaultUsername;
Password = DefaultSecurity.DefaultPassword;
#endif

IdentityUser? currentUser = await LoginService.GetCurrentUserAsync();

if (currentUser != null)
{
((ApiAuthenticationStateProvider)Auth).MarkUserAsAuthenticated();

NavigationManager.NavigateTo(".");
}

ShowLoginPage = true;
}

public async Task LoginAsync()
Expand All @@ -58,9 +74,9 @@

if (result)
{
((ApiAuthenticationStateProvider)Auth).MarkUserAsAuthenticated("Test");
((ApiAuthenticationStateProvider)Auth).MarkUserAsAuthenticated();

NavigationManager.NavigateTo("./");
NavigationManager.NavigateTo(".");
}
else
{
Expand Down
19 changes: 13 additions & 6 deletions src/DragonFly.Identity.Razor/Services/LoginService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@
// MIT License

using DragonFly.AspNetCore.Exports;
using DragonFly.Client;
using DragonFly.Identity;
using DragonFly.Security;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;

namespace DragonFly.AspNetCore.Identity.Razor;

Expand All @@ -35,4 +30,16 @@ public async Task Logout()
{
await Client.PostAsync("Logout", new StringContent(string.Empty));
}

public async Task<IdentityUser?> GetCurrentUserAsync()
{
HttpResponseMessage response = await Client.GetAsync("identity/CurrentUser");

if (response.IsSuccessStatusCode == false)
{
return null;
}

return await response.Content.ReadFromJsonAsync<IdentityUser>();
}
}
1 change: 1 addition & 0 deletions src/DragonFly.Identity/DragonFly.Identity.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>true</ImplicitUsings>
<Authors>usercode</Authors>
<PackageProjectUrl>https://github.com/usercode/DragonFly</PackageProjectUrl>
<Description>Headless CMS based on ASP.NET Core and Blazor</Description>
Expand Down
5 changes: 0 additions & 5 deletions src/DragonFly.Identity/DragonFlyApiBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@

using DragonFly.Identity.Services;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DragonFly;

Expand Down
2 changes: 0 additions & 2 deletions src/DragonFly.Identity/Services/IIdentityService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DragonFly.Identity.Services;
Expand Down
Loading

0 comments on commit fb376ec

Please sign in to comment.