C
C#13mo ago
AbishkarKafle

❔ i want to add jwt in my api i am making should I put this in the same controller as my register

using HotelRservationAPI.DTO;
using HotelRservationAPI.Interface;
using HotelRservationAPI.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace HotelRservationAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class RegisterController : Controller
{
private readonly IRegisterRepository _registerRepository;

public RegisterController(IRegisterRepository registerRepository)
{
_registerRepository = registerRepository;
}

#region Post
[HttpPost]
[ProducesResponseType(204)]
[ProducesResponseType(400)]
public IActionResult CreateRegister([FromBody] RegisterDto registerCreate)
{
if (registerCreate == null)
return BadRequest(ModelState);

var category = _registerRepository.GetRegisters()
.Where(c => c.Username.Trim().ToUpper() == registerCreate.Username.TrimEnd().ToUpper())
.FirstOrDefault();

if (category != null)
{
ModelState.AddModelError("", "Category already exists");
return StatusCode(422, ModelState);
}

if (!ModelState.IsValid)
return BadRequest(ModelState);

var categoryMap = new Register
{
Username = registerCreate.Username,
Password = registerCreate.Password,
Email = registerCreate.Email
};

if (!_registerRepository.CreateRegister(categoryMap))
{
ModelState.AddModelError("", "Something went wrong while saving");
return StatusCode(500, ModelState);
}

return Ok("Successfully created");

}
#endregion
}
}
using HotelRservationAPI.DTO;
using HotelRservationAPI.Interface;
using HotelRservationAPI.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace HotelRservationAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class RegisterController : Controller
{
private readonly IRegisterRepository _registerRepository;

public RegisterController(IRegisterRepository registerRepository)
{
_registerRepository = registerRepository;
}

#region Post
[HttpPost]
[ProducesResponseType(204)]
[ProducesResponseType(400)]
public IActionResult CreateRegister([FromBody] RegisterDto registerCreate)
{
if (registerCreate == null)
return BadRequest(ModelState);

var category = _registerRepository.GetRegisters()
.Where(c => c.Username.Trim().ToUpper() == registerCreate.Username.TrimEnd().ToUpper())
.FirstOrDefault();

if (category != null)
{
ModelState.AddModelError("", "Category already exists");
return StatusCode(422, ModelState);
}

if (!ModelState.IsValid)
return BadRequest(ModelState);

var categoryMap = new Register
{
Username = registerCreate.Username,
Password = registerCreate.Password,
Email = registerCreate.Email
};

if (!_registerRepository.CreateRegister(categoryMap))
{
ModelState.AddModelError("", "Something went wrong while saving");
return StatusCode(500, ModelState);
}

return Ok("Successfully created");

}
#endregion
}
}
by watching some tutorial i have made this much but i heard we need token auth to be safe so i was wondering is this where you put the jwt auth?
2 Replies
JakenVeina
JakenVeina13mo ago
I don't see what any of this has to do with JWTs
Accord
Accord13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server