|
4 | 4 | using System.Text.Json.Serialization;
|
5 | 5 | using System.Threading.Tasks;
|
6 | 6 | using Microsoft.AspNetCore.Authorization;
|
| 7 | +using Microsoft.AspNetCore.Http; |
7 | 8 | using Microsoft.AspNetCore.Mvc;
|
8 | 9 | using Microsoft.EntityFrameworkCore;
|
| 10 | +using Microsoft.Extensions.Options; |
| 11 | +using TravelBlog.Configuration; |
9 | 12 | using TravelBlog.Database;
|
10 | 13 | using TravelBlog.Database.Entities;
|
11 | 14 | using TravelBlog.Extensions;
|
| 15 | +using TravelBlog.Services; |
12 | 16 |
|
13 | 17 | namespace TravelBlog.Controllers;
|
14 | 18 |
|
15 | 19 | [ApiController]
|
16 | 20 | public class AdminApiController : ControllerBase
|
17 | 21 | {
|
| 22 | + private readonly IOptions<SiteOptions> options; |
18 | 23 | private readonly DatabaseContext database;
|
| 24 | + private readonly AuthenticationService authentication; |
19 | 25 |
|
20 |
| - public AdminApiController(DatabaseContext database) |
| 26 | + public AdminApiController(IOptions<SiteOptions> options, DatabaseContext database, AuthenticationService authentication) |
21 | 27 | {
|
| 28 | + this.options = options; |
22 | 29 | this.database = database;
|
| 30 | + this.authentication = authentication; |
| 31 | + } |
| 32 | + |
| 33 | + [HttpPost] |
| 34 | + public async Task<IActionResult> Login(LoginRequest request) |
| 35 | + { |
| 36 | + if (request.Password == options.Value.AdminPassword) |
| 37 | + { |
| 38 | + await authentication.SignInAsync(HttpContext, "admin", Constants.AdminRole); |
| 39 | + return StatusCode(StatusCodes.Status204NoContent); |
| 40 | + } |
| 41 | + else |
| 42 | + { |
| 43 | + return StatusCode(StatusCodes.Status401Unauthorized); |
| 44 | + } |
23 | 45 | }
|
24 | 46 |
|
25 | 47 | [HttpGet("~/api/admin/subscribers")]
|
@@ -51,6 +73,8 @@ public async Task<IActionResult> EditSubscriber(int id, [FromBody] JsonSubscribe
|
51 | 73 | return StatusCode(204);
|
52 | 74 | }
|
53 | 75 |
|
| 76 | + public record LoginRequest(string Password); |
| 77 | + |
54 | 78 | public class JsonSubscriber
|
55 | 79 | {
|
56 | 80 | [JsonConstructor]
|
|
0 commit comments