Files
rr3-server/RR3CommunityServer/Pages/DeviceSettings.cshtml.cs
Daniel Elliott e03c1d9856 Add admin panel authentication and login system
Features:
- Login page with username/email + password
- Registration page for new accounts
- Logout functionality
- Cookie-based authentication (30-day sessions)
- Auto-redirect to login for unauthorized access
- User dropdown in navbar with logout link

Security:
- All admin pages now require authentication
- [Authorize] attribute on all admin PageModels
- Redirect to /Login if not authenticated
- Auto-login after registration

UI:
- Beautiful gradient login/register pages
- Consistent styling with admin panel
- User info displayed in navbar
- Logout link in dropdown menu

Starting resources for new users:
- 100,000 Gold
- 500,000 Cash
- Level 1
- Full admin panel access

Ready for production deployment!
2026-02-19 15:06:08 -08:00

111 lines
3.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using RR3CommunityServer.Data;
using RR3CommunityServer.Models;
namespace RR3CommunityServer.Pages;
[Authorize]
public class DeviceSettingsModel : PageModel
{
private readonly RR3DbContext _context;
private readonly ILogger<DeviceSettingsModel> _logger;
public DeviceSettingsModel(RR3DbContext context, ILogger<DeviceSettingsModel> logger)
{
_context = context;
_logger = logger;
}
public List<UserSettings> DeviceSettings { get; set; } = new();
public string CurrentServerUrl { get; set; } = string.Empty;
public async Task OnGetAsync()
{
CurrentServerUrl = $"{Request.Scheme}://{Request.Host}";
DeviceSettings = await _context.UserSettings
.OrderByDescending(s => s.LastUpdated)
.ToListAsync();
_logger.LogInformation($"📋 Loaded {DeviceSettings.Count} device settings");
}
public async Task<IActionResult> OnPostAddOrUpdateAsync(string deviceId, string mode, string serverUrl)
{
try
{
if (string.IsNullOrWhiteSpace(deviceId))
{
TempData["Error"] = "Device ID is required";
return RedirectToPage();
}
_logger.LogInformation($"🔄 Adding/Updating settings: deviceId={deviceId}, mode={mode}, url={serverUrl}");
var existingSettings = await _context.UserSettings
.Where(s => s.DeviceId == deviceId)
.FirstOrDefaultAsync();
if (existingSettings == null)
{
// Create new
var newSettings = new UserSettings
{
DeviceId = deviceId,
Mode = mode,
ServerUrl = serverUrl ?? string.Empty,
LastUpdated = DateTime.UtcNow
};
_context.UserSettings.Add(newSettings);
_logger.LogInformation($" Created new settings for {deviceId}");
TempData["Message"] = $"Settings created for device: {deviceId}";
}
else
{
// Update existing
existingSettings.Mode = mode;
existingSettings.ServerUrl = serverUrl ?? string.Empty;
existingSettings.LastUpdated = DateTime.UtcNow;
_logger.LogInformation($"✏️ Updated settings for {deviceId}");
TempData["Message"] = $"Settings updated for device: {deviceId}";
}
await _context.SaveChangesAsync();
}
catch (Exception ex)
{
_logger.LogError(ex, "❌ Error saving device settings");
TempData["Error"] = "Failed to save settings";
}
return RedirectToPage();
}
public async Task<IActionResult> OnPostDeleteAsync(string deviceId)
{
try
{
var settings = await _context.UserSettings
.Where(s => s.DeviceId == deviceId)
.FirstOrDefaultAsync();
if (settings != null)
{
_context.UserSettings.Remove(settings);
await _context.SaveChangesAsync();
_logger.LogInformation($"🗑️ Deleted settings for {deviceId}");
TempData["Message"] = $"Settings deleted for device: {deviceId}";
}
}
catch (Exception ex)
{
_logger.LogError(ex, "❌ Error deleting device settings");
TempData["Error"] = "Failed to delete settings";
}
return RedirectToPage();
}
}