Skip to content

Commit 9c4dd58

Browse files
committed
Fix login errors
1 parent 436b314 commit 9c4dd58

File tree

2 files changed

+52
-20
lines changed

2 files changed

+52
-20
lines changed

ReplayBrowser/Controllers/AccountController.cs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
using Microsoft.EntityFrameworkCore;
88
using ReplayBrowser.Data;
99
using ReplayBrowser.Data.Models;
10+
using ReplayBrowser.Data.Models.Account;
1011
using ReplayBrowser.Helpers;
12+
using Serilog;
1113

1214
namespace ReplayBrowser.Controllers;
1315

@@ -18,11 +20,13 @@ public class AccountController : Controller
1820
{
1921
private readonly IConfiguration _configuration;
2022
private readonly ReplayDbContext _context;
23+
private readonly Ss14ApiHelper _ss14ApiHelper;
2124

22-
public AccountController(IConfiguration configuration, ReplayDbContext context)
25+
public AccountController(IConfiguration configuration, ReplayDbContext context, Ss14ApiHelper ss14ApiHelper)
2326
{
2427
_configuration = configuration;
2528
_context = context;
29+
_ss14ApiHelper = ss14ApiHelper;
2630
}
2731

2832
[Route("login")]
@@ -41,6 +45,48 @@ public async Task<IActionResult> Logout()
4145
return Redirect("/");
4246
}
4347

48+
/// <summary>
49+
/// Creates an account in the database, then redirects to the home page.
50+
/// </summary>
51+
[Authorize]
52+
[Route("redirect")]
53+
public async Task<IActionResult> RedirectFromLogin()
54+
{
55+
if (!User.Identity.IsAuthenticated)
56+
{
57+
return Unauthorized();
58+
}
59+
60+
var guid = AccountHelper.GetAccountGuid(User);
61+
62+
if (guid == null)
63+
return BadRequest("Guid is null. This should not happen.");
64+
65+
var user = _context.Accounts.FirstOrDefault(a => a.Guid == guid);
66+
var data = await _ss14ApiHelper.FetchPlayerDataFromGuid((Guid)guid);
67+
if (user == null)
68+
{
69+
user = new Account()
70+
{
71+
Guid = (Guid) guid,
72+
Username = data?.Username ?? "API Error",
73+
};
74+
_context.Accounts.Add(user);
75+
await _context.SaveChangesAsync();
76+
Log.Information("Created new account for {Guid} with username {Username}", guid, data?.Username);
77+
}
78+
else
79+
{
80+
if (data?.Username != user.Username)
81+
{
82+
user.Username = data?.Username ?? "API Error";
83+
await _context.SaveChangesAsync();
84+
Log.Information("Updated username for {Guid} to {Username}", guid, data?.Username);
85+
}
86+
}
87+
return Redirect("/");
88+
}
89+
4490
/// <summary>
4591
/// Deletes the account from the logged in user.
4692
/// </summary>

ReplayBrowser/Pages/Shared/MainLayout.razor

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
@using Microsoft.AspNetCore.Components.Authorization
2-
@using Microsoft.EntityFrameworkCore
32
@using ReplayBrowser.Data
43
@using ReplayBrowser.Data.Models.Account
54
@using ReplayBrowser.Helpers
@@ -9,6 +8,7 @@
98
@inject AuthenticationStateProvider AuthenticationStateProvider
109
@inject ReplayDbContext DbContext
1110
@inject Ss14ApiHelper Ss14ApiHelper
11+
@inject NavigationManager NavigationManager
1212

1313
@code
1414
{
@@ -23,27 +23,13 @@
2323
{
2424
var guid = (Guid)AccountHelper.GetAccountGuid(authState)!;
2525
_account = DbContext.Accounts.FirstOrDefault(a => a.Guid == guid);
26-
var data = await Ss14ApiHelper.FetchPlayerDataFromGuid(guid);
27-
2826
if (_account == null)
2927
{
30-
_account = new Account()
28+
Log.Error("Account {Guid} is authenticated but not found in the database", guid);
29+
NavigationManager.NavigateTo("account/redirect", new NavigationOptions()
3130
{
32-
Guid = guid,
33-
Username = data.Username
34-
};
35-
36-
DbContext.Accounts.Add(_account);
37-
DbContext.SaveChanges();
38-
39-
Log.Information("Created new account for {Guid} with username {Username}", guid, data.Username);
40-
}
41-
42-
if (_account.Username != data.Username)
43-
{
44-
_account.Username = data.Username;
45-
await DbContext.SaveChangesAsync();
46-
Log.Information("Updated username for account {Guid} to {Username}", guid, data.Username);
31+
ForceLoad = true
32+
});
4733
}
4834
}
4935
}

0 commit comments

Comments
 (0)