Skip to content

Commit

Permalink
split EntranceController to separate controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
josxha committed Oct 22, 2023
1 parent 7d84937 commit 6e96a7e
Show file tree
Hide file tree
Showing 15 changed files with 199 additions and 150 deletions.
147 changes: 0 additions & 147 deletions KratosSelfService/Controllers/EntranceController.cs

This file was deleted.

2 changes: 1 addition & 1 deletion KratosSelfService/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace KratosSelfService.Controllers;

public class HomeController(ILogger<EntranceController> logger) : Controller
public class HomeController(ILogger<HomeController> logger) : Controller
{
[HttpGet("")]
public IActionResult Home()
Expand Down
58 changes: 58 additions & 0 deletions KratosSelfService/Controllers/LoginController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using KratosSelfService.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
using Ory.Kratos.Client.Client;
using Ory.Kratos.Client.Model;

namespace KratosSelfService.Controllers;

public class LoginController(ILogger<LoginController> logger, ApiService api) : Controller
{
[HttpGet("login")]
public async Task<IActionResult> Login([FromQuery(Name = "flow")] string? flowId)
{
if (flowId == null)
{
// initiate flow
var url = api.GetUrlForFlow("login");
return Redirect(url);
}

KratosLoginFlow flow;
try
{
flow = await api.Frontend.GetLoginFlowAsync(flowId, Request.Headers.Cookie);
}
catch (ApiException exception)
{
logger.LogError(exception.Message);
// restart flow
return Redirect("login");
}

if (flow.Ui.Messages?.Any(text => text.Id == 4000010) ?? false)
// the login requires that the user verifies their email address before logging in
// we will create a new verification flow and redirect the user to the verification page
try
{
var response = await api.Frontend
.CreateBrowserVerificationFlowWithHttpInfoAsync(flow.ReturnTo);
var verificationFlow = response.Data;
// we need the csrf cookie from the verification flow
Response.Headers.Add(HeaderNames.SetCookie, response.Headers[HeaderNames.SetCookie].ToString());
// encode the verification flow id in the query parameters
var parameters = $"flow={verificationFlow.Id}&message={flow.Ui.Messages}";

var baseUrl = new Uri(Request.Path).Segments;
//Navigation.NavigateTo(baseUrl);
}
catch (Exception exception)
{
logger.LogError(exception.Message);
// restart flow
return Redirect("login");
}

return View("Login", flow);
}
}
27 changes: 27 additions & 0 deletions KratosSelfService/Controllers/LogoutController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using KratosSelfService.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
using Ory.Kratos.Client.Client;
using Ory.Kratos.Client.Model;

namespace KratosSelfService.Controllers;

public class LogoutController(ILogger<LogoutController> logger, ApiService api) : Controller
{
[HttpGet("logout")]
public async Task<IActionResult> Logout()
{
KratosLogoutFlow flow;
try
{
flow = await api.Frontend.CreateBrowserLogoutFlowAsync(Request.Headers.Cookie);
}
catch (ApiException exception)
{
Console.WriteLine(exception.Message);
throw;
}

return Redirect(flow.LogoutUrl);
}
}
33 changes: 33 additions & 0 deletions KratosSelfService/Controllers/RecoveryController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using KratosSelfService.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
using Ory.Kratos.Client.Client;
using Ory.Kratos.Client.Model;

namespace KratosSelfService.Controllers;

public class RecoveryController(ILogger<RecoveryController> logger, ApiService api) : Controller
{
[HttpGet("recovery")]
public async Task<IActionResult> Recovery([FromQuery(Name = "flow")] string? flowId)
{
if (flowId == null)
{
return Redirect(api.GetUrlForFlow("recovery"));
}

KratosRecoveryFlow flow;
try
{
flow = await api.Frontend.GetRecoveryFlowAsync(flowId, Request.Headers.Cookie);
}
catch (ApiException exception)
{
logger.LogError(exception.Message);
// restart flow
return Redirect("recovery");
}

return View("Recovery", flow);
}
}
35 changes: 35 additions & 0 deletions KratosSelfService/Controllers/RegistrationController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using KratosSelfService.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
using Ory.Kratos.Client.Client;
using Ory.Kratos.Client.Model;

namespace KratosSelfService.Controllers;

public class RegistrationController(ILogger<RegistrationController> logger, ApiService api) : Controller
{
[HttpGet("registration")]
public async Task<IActionResult> Registration([FromQuery(Name = "flow")] string? flowId)
{
if (flowId == null)
{
// initiate flow
var url = api.GetUrlForFlow("registration");
return Redirect(url);
}

KratosRegistrationFlow flow;
try
{
flow = await api.Frontend.GetRegistrationFlowAsync(flowId, Request.Headers.Cookie);
}
catch (ApiException exception)
{
logger.LogError(exception.Message);
// restart flow
return Redirect("registration");
}

return View("Registration", flow);
}
}
34 changes: 34 additions & 0 deletions KratosSelfService/Controllers/VerificationController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using KratosSelfService.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
using Ory.Kratos.Client.Client;
using Ory.Kratos.Client.Model;

namespace KratosSelfService.Controllers;

public class VerificationController(ILogger<VerificationController> logger, ApiService api) : Controller
{
[HttpGet("verification")]
public async Task<IActionResult> Verification([FromQuery(Name = "flow")] string? flowId)
{
if (flowId == null)
{
// initiate flow
var url = api.GetUrlForFlow("verification");
return Redirect(url);
}

KratosVerificationFlow flow;
try
{
flow = await api.Frontend.GetVerificationFlowAsync(flowId, Request.Headers.Cookie);
}
catch (ApiException exception)
{
Console.WriteLine(exception.Message);
throw;
}

return View("Verification", flow);
}
}
11 changes: 10 additions & 1 deletion KratosSelfService/KratosSelfService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,16 @@
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="Views\Entrance\Verification.cshtml"/>
<AdditionalFiles Include="Views\Login\Login.cshtml" />
<AdditionalFiles Include="Views\Login\Recovery.cshtml" />
<AdditionalFiles Include="Views\Login\Registration.cshtml" />
<AdditionalFiles Include="Views\Recovery\Login.cshtml" />
<AdditionalFiles Include="Views\Recovery\Recovery.cshtml" />
<AdditionalFiles Include="Views\Recovery\Registration.cshtml" />
<AdditionalFiles Include="Views\Registration\Login.cshtml" />
<AdditionalFiles Include="Views\Registration\Recovery.cshtml" />
<AdditionalFiles Include="Views\Registration\Registration.cshtml" />
<AdditionalFiles Include="Views\Verification\Verification.cshtml" />
<AdditionalFiles Include="Views\Settings\Settings.cshtml"/>
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion KratosSelfService/Resources/CustomTranslator.de.resx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
<value>Läuft ab am</value>
</data>
<data name="sessions.noOtherActiveSessions" xml:space="preserve">
<value>Es gibt eine weiteren aktiven Sitzungen</value>
<value>Es gibt keine weiteren aktiven Sitzungen</value>
</data>
<data name="sessions.otherSessions" xml:space="preserve">
<value>Weitere Sitzungen</value>
Expand Down
File renamed without changes.

0 comments on commit 6e96a7e

Please sign in to comment.