Fix admin route access and backend configuration
- Added /admin redirect to login page in nginx config - Fixed backend server.js route ordering for proper admin handling - Updated authentication middleware and routes - Added user management routes - Configured PostgreSQL integration - Updated environment configuration
This commit is contained in:
85
Sky_Art_shop/Controllers/AdminController.cs
Normal file
85
Sky_Art_shop/Controllers/AdminController.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using SkyArtShop.Models;
|
||||
using SkyArtShop.Services;
|
||||
|
||||
namespace SkyArtShop.Controllers
|
||||
{
|
||||
[Route("admin")]
|
||||
[Authorize(Roles = "Admin")]
|
||||
public class AdminController : Controller
|
||||
{
|
||||
private readonly MongoDBService _mongoService;
|
||||
private readonly SignInManager<SkyArtShop.Data.ApplicationUser> _signInManager;
|
||||
private readonly UserManager<SkyArtShop.Data.ApplicationUser> _userManager;
|
||||
|
||||
public AdminController(MongoDBService mongoService,
|
||||
SignInManager<SkyArtShop.Data.ApplicationUser> signInManager,
|
||||
UserManager<SkyArtShop.Data.ApplicationUser> userManager)
|
||||
{
|
||||
_mongoService = mongoService;
|
||||
_signInManager = signInManager;
|
||||
_userManager = userManager;
|
||||
}
|
||||
|
||||
[HttpGet("login")]
|
||||
[AllowAnonymous]
|
||||
public IActionResult Login()
|
||||
{
|
||||
if (User.Identity?.IsAuthenticated == true)
|
||||
{
|
||||
return RedirectToAction("Dashboard");
|
||||
}
|
||||
return View();
|
||||
}
|
||||
|
||||
[HttpPost("login")]
|
||||
[AllowAnonymous]
|
||||
public async Task<IActionResult> Login(string email, string password)
|
||||
{
|
||||
var user = await _userManager.FindByEmailAsync(email);
|
||||
if (user == null)
|
||||
{
|
||||
ViewBag.Error = "Invalid email or password";
|
||||
return View();
|
||||
}
|
||||
var result = await _signInManager.PasswordSignInAsync(user, password, true, false);
|
||||
if (!result.Succeeded)
|
||||
{
|
||||
ViewBag.Error = "Invalid email or password";
|
||||
return View();
|
||||
}
|
||||
return RedirectToAction("Dashboard");
|
||||
}
|
||||
|
||||
[HttpGet("logout")]
|
||||
public async Task<IActionResult> Logout()
|
||||
{
|
||||
await _signInManager.SignOutAsync();
|
||||
return RedirectToAction("Login");
|
||||
}
|
||||
|
||||
[HttpGet("dashboard")]
|
||||
public async Task<IActionResult> Dashboard()
|
||||
{
|
||||
var products = await _mongoService.GetAllAsync<Product>("Products");
|
||||
var projects = await _mongoService.GetAllAsync<PortfolioProject>("PortfolioProjects");
|
||||
var blogPosts = await _mongoService.GetAllAsync<BlogPost>("BlogPosts");
|
||||
var pages = await _mongoService.GetAllAsync<Page>("Pages");
|
||||
var settings = (await _mongoService.GetAllAsync<SiteSettings>("SiteSettings")).FirstOrDefault();
|
||||
|
||||
ViewBag.ProductCount = products.Count;
|
||||
ViewBag.ProjectCount = projects.Count;
|
||||
ViewBag.BlogCount = blogPosts.Count;
|
||||
ViewBag.PageCount = pages.Count;
|
||||
ViewBag.SiteName = settings?.SiteName ?? "Sky Art Shop";
|
||||
ViewBag.AdminEmail = User.Identity?.Name;
|
||||
|
||||
return View();
|
||||
}
|
||||
|
||||
[HttpGet("")]
|
||||
public IActionResult Index() => RedirectToAction("Dashboard");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user