diff --git a/Moonlight/App/Models/Misc/LoginDataModel.cs b/Moonlight/App/Models/Forms/LoginDataModel.cs
similarity index 92%
rename from Moonlight/App/Models/Misc/LoginDataModel.cs
rename to Moonlight/App/Models/Forms/LoginDataModel.cs
index d6c00204..63a5ac28 100644
--- a/Moonlight/App/Models/Misc/LoginDataModel.cs
+++ b/Moonlight/App/Models/Forms/LoginDataModel.cs
@@ -1,6 +1,6 @@
using System.ComponentModel.DataAnnotations;
-namespace Moonlight.App.Models.Misc;
+namespace Moonlight.App.Models.Forms;
public class LoginDataModel
{
diff --git a/Moonlight/App/Models/Forms/LoginTotpDataModel.cs b/Moonlight/App/Models/Forms/LoginTotpDataModel.cs
new file mode 100644
index 00000000..b1dcca02
--- /dev/null
+++ b/Moonlight/App/Models/Forms/LoginTotpDataModel.cs
@@ -0,0 +1,9 @@
+using System.ComponentModel.DataAnnotations;
+
+namespace Moonlight.App.Models.Forms;
+
+public class LoginTotpDataModel
+{
+ [Required(ErrorMessage = "You need to enter a 2fa code")]
+ public string Code { get; set; } = "";
+}
\ No newline at end of file
diff --git a/Moonlight/Shared/Components/Auth/Login.razor b/Moonlight/Shared/Components/Auth/Login.razor
index 28ca0ce0..dd642e39 100644
--- a/Moonlight/Shared/Components/Auth/Login.razor
+++ b/Moonlight/Shared/Components/Auth/Login.razor
@@ -13,6 +13,8 @@
@using Moonlight.App.Models.Misc
@using Moonlight.App.Services.OAuth2
@using Moonlight.App.Services.Sessions
+@using System.ComponentModel.DataAnnotations
+@using Moonlight.App.Models.Forms
@inject AlertService AlertService
@inject UserService UserService
@@ -26,9 +28,9 @@
-
- @if (!TotpRequired)
- {
+ @if (!TotpRequired)
+ {
+
Sign In
@@ -64,11 +66,11 @@
-
+
-
+
@@ -84,29 +86,29 @@
Sign-in
- }
- else
- {
+
+
+
Not registered yet?
+
+
+ Sign up
+
+
+
+ }
+ else
+ {
+
-
+
-
-
+
- }
-
-
-
Not registered yet?
-
-
- Sign up
-
-
-
+
+ }
@@ -114,22 +116,39 @@
@code
{
- private LoginDataModel User = new();
+ private LoginDataModel LoginData = new();
+ private LoginTotpDataModel TotpData = new();
private bool TotpRequired = false;
- private string TotpCode = "";
private async Task DoLogin()
{
try
{
- User.Email = User.Email.ToLower().Trim();
+ LoginData.Email = LoginData.Email.ToLower().Trim();
- TotpRequired = await UserService.CheckTotp(User.Email, User.Password);
-
- if (!TotpRequired)
+ if (string.IsNullOrEmpty(TotpData.Code))
{
- var token = await UserService.Login(User.Email, User.Password);
+ TotpRequired = await UserService.CheckTotp(LoginData.Email, LoginData.Password);
+
+ if (!TotpRequired)
+ {
+ var token = await UserService.Login(LoginData.Email, LoginData.Password);
+ await CookieService.SetValue("token", token, 10);
+
+ if (NavigationManager.Uri.EndsWith("login"))
+ NavigationManager.NavigateTo("/", true);
+ else
+ NavigationManager.NavigateTo(NavigationManager.Uri, true);
+ }
+ else
+ {
+ await InvokeAsync(StateHasChanged);
+ }
+ }
+ else
+ {
+ var token = await UserService.Login(LoginData.Email, LoginData.Password, TotpData.Code);
await CookieService.SetValue("token", token, 10);
if (NavigationManager.Uri.EndsWith("login"))
@@ -137,13 +156,14 @@
else
NavigationManager.NavigateTo(NavigationManager.Uri, true);
}
- else
- {
- await InvokeAsync(StateHasChanged);
- }
}
catch (DisplayException e)
{
+ // Reset state
+ LoginData = new();
+ TotpData = new();
+ TotpRequired = false;
+
await AlertService.Error(
SmartTranslateService.Translate("Error"),
SmartTranslateService.Translate(e.Message)
@@ -151,6 +171,11 @@
}
catch (Exception e)
{
+ // Reset state
+ LoginData = new();
+ TotpData = new();
+ TotpRequired = false;
+
await AlertService.Error(
SmartTranslateService.Translate("Error"),
SmartTranslateService.Translate("An error occured while logging you in")
diff --git a/Moonlight/resources/lang/de_de.lang b/Moonlight/resources/lang/de_de.lang
index bc1ff821..8e77baa8 100644
--- a/Moonlight/resources/lang/de_de.lang
+++ b/Moonlight/resources/lang/de_de.lang
@@ -424,3 +424,5 @@ Enable;Enable
Your account is secured with 2fa;Your account is secured with 2fa
anyone write a fancy text here?;anyone write a fancy text here?
Disable;Disable
+You need to enter a 2fa code;You need to enter a 2fa code
+2FA code invalid;2FA code invalid