Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

1.3 KB
Raw Blame
using Grand.Module.Api.Commands.Models.Common;
using Grand.Module.Api.Models.Common;
using Grand.Business.Core.Interfaces.Customers;
using Grand.Mediator;
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);
    }
}