-
Notifications
You must be signed in to change notification settings - Fork 525
Expand file tree
/
Copy pathTokenController.cs
More file actions
43 lines (37 loc) · 1.26 KB
/
TokenController.cs
File metadata and controls
43 lines (37 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using Grand.Module.Api.Commands.Models.Common;
using Grand.Module.Api.Models.Common;
using Grand.Business.Core.Interfaces.Customers;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Grand.Module.Api.Controllers;
[ApiController]
[Area("Api")]
[Route("[area]/[controller]/[action]")]
[ApiExplorerSettings(IgnoreApi = false, GroupName = "v1")]
public class TokenController : Controller
{
private readonly IMediator _mediator;
private readonly IUserApiService _userApiService;
public TokenController(IMediator mediator, IUserApiService userApiService)
{
_userApiService = userApiService;
_mediator = mediator;
}
[AllowAnonymous]
[IgnoreAntiforgeryToken]
[HttpPost]
public async Task<IActionResult> Create([FromBody] LoginModel model)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
var claims = new Dictionary<string, string> {
{ "Email", model.Email }
};
var user = await _userApiService.GetUserByEmail(model.Email);
if (user != null)
claims.Add("Token", user.Token);
var token = await _mediator.Send(new GenerateTokenCommand { Claims = claims });
return Content(token);
}
}