예제 #1
0
        public async Task <ActionResult <ApiResponse <LoginResponse> > > Login([FromBody] ApiRequest <LoginRequest> request)
        {
            try
            {
                var user = await this._usersDm
                           .GetByAsync(
                    email : request.Data.Email,
                    password : request.Data.Password);

                if (user != null)
                {
                    /* Agregar token con JWT */
                    var token = Jwt.GenerateToken(
                        secret: this._appSettings.Secret,
                        issuer: this._appSettings.Issuer,
                        audience: this._appSettings.Audience,
                        expirationInMinutes: int.Parse(this._appSettings.TokenExpirationInMinutes),
                        claims: new List <Claim>
                    {
                        new Claim("UserId", user.Id.ToString()),
                        new Claim("Email", user.Email)
                    });
                    var expiresInTicks = Jwt.GetExpiresTokenInTicks(int.Parse(this._appSettings.TokenExpirationInMinutes));

                    return(Ok(new ApiResponse <LoginResponse>
                    {
                        Result = new LoginResponse
                        {
                            Token = token,
                            ExpiresToken = expiresInTicks
                        },
                        Error = null,
                        Status = HttpStatusCode.OK
                    }));
                }
                else
                {
                    return(NotFound(new ApiResponse
                    {
                        Error = "User not found",
                        Status = HttpStatusCode.NotFound
                    }));
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Error users/login");
                return(BadRequest(new ApiResponse
                {
                    Error = e.Message,
                    Status = HttpStatusCode.BadRequest
                }));
            }
        }