Created
May 8, 2019 15:26
-
-
Save nishanc/807e59c237f931c99c90f2223b9b0d87 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Mvc; | |
using JWTAuth.API.Data; | |
using System.Threading.Tasks; | |
using JWTAuth.API.Models; | |
using JWTAuth.API.Dtos; | |
namespace JWTAuth.API.Controllers | |
{ | |
[Route("api/[controller]")] | |
public class AuthController : Controller | |
{ | |
private readonly IAuthRepository _repo; | |
public AuthController(IAuthRepository repo) | |
{ | |
_repo = repo; | |
} | |
[HttpPost("register")] //<host>/api/auth/register | |
public async Task<IActionResult> Register([FromBody] UserForRegisterDto userForRegisterDto){ //Data Transfer Object containing username and password. | |
// validate request | |
if(!ModelState.IsValid) | |
return BadRequest(ModelState); | |
userForRegisterDto.Username = userForRegisterDto.Username.ToLower(); //Convert username to lower case before storing in database. | |
if(await _repo.UserExists(userForRegisterDto.Username)) | |
return BadRequest("Username is already taken"); | |
var userToCreate = new User{ | |
Username = userForRegisterDto.Username | |
}; | |
var createUser = await _repo.Register(userToCreate, userForRegisterDto.Password); | |
return StatusCode(201); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment