public async Task <ActionResult <BookDTO> > CreateUserAsync(CreateBookDTOs userDTO)
        {
            Book newUser = new()
            {
                Id       = Guid.NewGuid(),
                BookName = userDTO.BookName,
                Password = ph.hashPass(userDTO.Password),
                Author   = "newAuthor"
            };

            await repo.Add(newUser);

            return(CreatedAtAction(nameof(GetUserAsync), new { id = newUser.Id }, newUser.AsDTO()));
        }
Exemple #2
0
        public async Task <IActionResult> Post(CreateBookDTOs _createBookData)
        {
            if (_createBookData != null && _createBookData.BookName != null && _createBookData.Password != null)
            {
                var book = await GetBook(_createBookData.BookName, _createBookData.Password);

                if (book != null)
                {
                    // create claims details based on the user information
                    var claims = new[]
                    {
                        new Claim(JwtRegisteredClaimNames.Sub, _configuration["Jwt:Subject"]),
                        new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                        new Claim(JwtRegisteredClaimNames.Iat, DateTime.UtcNow.ToString()),
                        // new Claim("Id", 5.ToString()),
                        new Claim("BookName", book.BookName),
                        new Claim("Password", book.Password)
                    };

                    var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["Jwt:Key"]));

                    var signIn = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

                    var token = new JwtSecurityToken(_configuration["Jwt:Issuer"], _configuration["Jwt:Audience"], claims, expires: DateTime.UtcNow.AddDays(1), signingCredentials: signIn);

                    return(Ok(new JwtSecurityTokenHandler().WriteToken(token)));
                }
                else
                {
                    return(BadRequest("Invalid credentials"));
                }
            }
            else
            {
                return(BadRequest());
            }
        }