Skip to content

Commit

Permalink
fix 2fa login, add ViewComponents
Browse files Browse the repository at this point in the history
  • Loading branch information
josxha committed Nov 3, 2023
1 parent d0a63f6 commit 26b0fa5
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 95 deletions.
1 change: 1 addition & 0 deletions KratosSelfService/KratosSelfService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<AdditionalFiles Include="Views\Logout\Logout.cshtml" />
<AdditionalFiles Include="Views\Recovery\Recovery.cshtml" />
<AdditionalFiles Include="Views\Registration\Registration.cshtml" />
<AdditionalFiles Include="Views\Shared\Components\KratosUiTextMessages\Default.cshtml" />
<AdditionalFiles Include="Views\Verification\Verification.cshtml" />
<AdditionalFiles Include="Views\Settings\Settings.cshtml"/>
</ItemGroup>
Expand Down
8 changes: 6 additions & 2 deletions KratosSelfService/Models/models.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ List<KratosSession> OtherSessions
);

public record SettingsModel(
KratosSettingsFlow flow,
string postUri
KratosSettingsFlow flow
);

public record VerificationModel(
Expand Down Expand Up @@ -63,3 +62,8 @@ public record KratosUiNodeModel(
string? forgotPasswordUrl = null
);

public record KratosUiModel(
KratosUiContainer ui,
FlowType flowType,
string? forgotPasswordUrl = null
);
13 changes: 13 additions & 0 deletions KratosSelfService/ViewComponents/KratosUi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using KratosSelfService.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewComponents;

namespace KratosSelfService.ViewComponents;

public class KratosUi : ViewComponent
{
public async Task<ViewViewComponentResult> InvokeAsync(KratosUiModel model)
{
return View("Default", model);
}
}
13 changes: 13 additions & 0 deletions KratosSelfService/ViewComponents/KratosUiTextMessages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewComponents;
using Ory.Kratos.Client.Model;

namespace KratosSelfService.ViewComponents;

public class KratosUiTextMessages : ViewComponent
{
public async Task<ViewViewComponentResult> InvokeAsync(List<KratosUiText>? model)
{
return View("Default", model);
}
}
51 changes: 22 additions & 29 deletions KratosSelfService/Views/Login/Login.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,28 @@
</a>
</p>
<hr/>
<form method="@Model.flow.Ui.Method.ToLower()" action="@Model.flow.Ui.Action">
<div class="mb-5">
@if (Model.flow.Ui.Messages != null)
{
foreach (var message in Model.flow.Ui.Messages)
{
@await Component.InvokeAsync("KratosUiTextMessage", message)
}
}
@foreach (var node in Model.flow.Ui.Nodes)
<div class="mb-5">
@await Component.InvokeAsync("KratosUiTextMessages", Model.flow.Ui.Messages)

@await Component.InvokeAsync("KratosUi", new KratosUiModel(
Model.flow.Ui,
FlowType.Login,
Model.forgotPasswordUrl))
</div>

<!--<div class="field separator">Or sign in with</div>
<div class="field">
<div class="columns is-mobile is-multiline">
@for (var i = 0; i < 1; i++)
{
var model = new KratosUiNodeModel(node, FlowType.Login, Model.forgotPasswordUrl);
@await Component.InvokeAsync("KratosUiNodeInput", model)
<div class="column is-one-third">
<a class="button is-fullwidth">
<span class="icon">
<i class="fab fa-google"></i>
</span>
<span>Google</span>
</a>
</div>
}
</div>
<!--<div class="field separator">Or sign in with</div>
<div class="field">
<div class="columns is-mobile is-multiline">
@for (var i = 0; i < 1; i++)
{
<div class="column is-one-third">
<a class="button is-fullwidth">
<span class="icon">
<i class="fab fa-google"></i>
</span>
<span>Google</span>
</a>
</div>
}
</div>
</div>-->
</form>
</div>-->
19 changes: 5 additions & 14 deletions KratosSelfService/Views/Recovery/Recovery.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,8 @@
</a>
</p>
<hr/>
<form method="@Model.flow.Ui.Method.ToLower()" action="@Model.flow.Ui.Action">
@if (Model.flow.Ui.Messages != null)
{
foreach (var message in Model.flow.Ui.Messages)
{
@await Component.InvokeAsync("KratosUiTextMessage", message)
}
}
@foreach (var node in Model.flow.Ui.Nodes)
{
var model = new KratosUiNodeModel(node, FlowType.Recovery);
@await Component.InvokeAsync("KratosUiNodeInput", model)
}
</form>
@await Component.InvokeAsync("KratosUiTextMessages", Model.flow.Ui.Messages)

@await Component.InvokeAsync("KratosUi", new KratosUiModel(
Model.flow.Ui,
FlowType.Recovery))
49 changes: 20 additions & 29 deletions KratosSelfService/Views/Registration/Registration.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,26 @@
</a>
</p>
<hr/>
<form method="@Model.flow.Ui.Method.ToLower()" action="@Model.flow.Ui.Action">
<div class="mb-5">
@if (Model.flow.Ui.Messages != null)
<div class="mb-2">
@await Component.InvokeAsync("KratosUiTextMessages", Model.flow.Ui.Messages)

@await Component.InvokeAsync("KratosUi", new KratosUiModel(
Model.flow.Ui,
FlowType.Registration))
</div>
<!--<div class="field separator">Or sign up with</div>
<div class="field">
<div class="columns is-mobile is-multiline">
@for (var i = 0; i < 1; i++)
{
foreach (var message in Model.flow.Ui.Messages)
{
@await Component.InvokeAsync("KratosUiTextMessage", message)
}
}
@foreach (var node in Model.flow.Ui.Nodes)
{
var model = new KratosUiNodeModel(node, FlowType.Registration);
@await Component.InvokeAsync("KratosUiNodeInput", model)
<div class="column is-one-third">
<a class="button is-fullwidth">
<span class="icon">
<i class="fab fa-google"></i>
</span>
<span>Google</span>
</a>
</div>
}
</div>
<!--<div class="field separator">Or sign up with</div>
<div class="field">
<div class="columns is-mobile is-multiline">
@for (var i = 0; i < 1; i++)
{
<div class="column is-one-third">
<a class="button is-fullwidth">
<span class="icon">
<i class="fab fa-google"></i>
</span>
<span>Google</span>
</a>
</div>
}
</div>
</div>-->
</form>
</div>-->
8 changes: 1 addition & 7 deletions KratosSelfService/Views/Settings/Settings.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,7 @@
@OryTranslator.Get("settings.subtitle-instructions")
</div>
}
@if (Model.flow.Ui.Messages != null)
{
foreach (var message in Model.flow.Ui.Messages)
{
@await Component.InvokeAsync("KratosUiTextMessage", message)
}
}
@await Component.InvokeAsync("KratosUiTextMessages", Model.flow.Ui.Messages)
</div>
@foreach (var group in groups)
{
Expand Down
30 changes: 30 additions & 0 deletions KratosSelfService/Views/Shared/Components/KratosUi/Default.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
@model KratosUiModel

@{
// this component is used by all flows except the settings flow
var allGroups = Model.ui.Nodes
.GroupBy(node => node.Group).ToList();
var defaultNodes = allGroups
.First(group => group.Key == KratosUiNode.GroupEnum.Default)
.ToArray();
var groups = allGroups
.Where(group => group.Key != KratosUiNode.GroupEnum.Default)
.ToArray();
}

@foreach (var group in groups)
{
<form class="mb-3" action="@Model.ui.Action" method="@Model.ui.Method">
@foreach (var node in defaultNodes)
{
var model = new KratosUiNodeModel(node, Model.flowType, Model.forgotPasswordUrl);
@await Component.InvokeAsync("KratosUiNodeInput", model)
}
@foreach (var node in group)
{
var model = new KratosUiNodeModel(node, Model.flowType, Model.forgotPasswordUrl);
@await Component.InvokeAsync("KratosUiNodeInput", model)
}
</form>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@model List<KratosUiText>?

@if (Model != null)
{
foreach (var message in Model)
{
@await Component.InvokeAsync("KratosUiTextMessage", message)
}
}
19 changes: 5 additions & 14 deletions KratosSelfService/Views/Verification/Verification.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,8 @@

<h1 class="title has-text-centered">@OryTranslator.Get("verification.title")</h1>
<hr/>
<form method="@Model.flow.Ui.Method.ToLower()" action="@Model.flow.Ui.Action">
@if (Model.flow.Ui.Messages != null)
{
foreach (var message in Model.flow.Ui.Messages)
{
@await Component.InvokeAsync("KratosUiTextMessage", message)
}
}
@foreach (var node in Model.flow.Ui.Nodes)
{
var model = new KratosUiNodeModel(node, FlowType.Verification);
@await Component.InvokeAsync("KratosUiNodeInput", model)
}
</form>
@await Component.InvokeAsync("KratosUiTextMessages", Model.flow.Ui.Messages)

@await Component.InvokeAsync("KratosUi", new KratosUiModel(
Model.flow.Ui,
FlowType.Verification))

0 comments on commit 26b0fa5

Please sign in to comment.