7
7
using Microsoft . EntityFrameworkCore ;
8
8
using ReplayBrowser . Data ;
9
9
using ReplayBrowser . Data . Models ;
10
+ using ReplayBrowser . Data . Models . Account ;
10
11
using ReplayBrowser . Helpers ;
12
+ using Serilog ;
11
13
12
14
namespace ReplayBrowser . Controllers ;
13
15
@@ -18,11 +20,13 @@ public class AccountController : Controller
18
20
{
19
21
private readonly IConfiguration _configuration ;
20
22
private readonly ReplayDbContext _context ;
23
+ private readonly Ss14ApiHelper _ss14ApiHelper ;
21
24
22
- public AccountController ( IConfiguration configuration , ReplayDbContext context )
25
+ public AccountController ( IConfiguration configuration , ReplayDbContext context , Ss14ApiHelper ss14ApiHelper )
23
26
{
24
27
_configuration = configuration ;
25
28
_context = context ;
29
+ _ss14ApiHelper = ss14ApiHelper ;
26
30
}
27
31
28
32
[ Route ( "login" ) ]
@@ -41,6 +45,48 @@ public async Task<IActionResult> Logout()
41
45
return Redirect ( "/" ) ;
42
46
}
43
47
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
+
44
90
/// <summary>
45
91
/// Deletes the account from the logged in user.
46
92
/// </summary>
0 commit comments