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 System; | |
using System.Threading.Tasks; | |
using JWTAuth.API.Models; | |
using Microsoft.EntityFrameworkCore; | |
namespace JWTAuth.API.Data | |
{ | |
public class AuthRepository : IAuthRepository | |
{ | |
private readonly DataContext _context; |
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 System; | |
using System.Threading.Tasks; | |
using JWTAuth.API.Models; | |
using Microsoft.EntityFrameworkCore; | |
namespace JWTAuth.API.Data | |
{ | |
public class AuthRepository : IAuthRepository | |
{ | |
private readonly DataContext _context; |
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 System.Threading.Tasks; | |
namespace JWTAuth.API.Data | |
{ | |
public class AuthRepository : IAuthRepository | |
{ | |
private readonly DataContext _context; | |
public AuthRepository(DataContext context) | |
{ | |
_context = context; |
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; | |
namespace JWTAuth.API.Controllers | |
{ | |
[Route("api/[controller]")] | |
public class AuthController : Controller | |
{ | |
private readonly IAuthRepository _repo; | |
public AuthController(IAuthRepository repo) |
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
namespace JWTAuth.API.Dtos | |
{ | |
public class UserForRegisterDto | |
{ | |
public string Username { get; set; } | |
public string Password { get; set; } | |
} | |
} |
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
[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"); |
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 |
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 System.ComponentModel.DataAnnotations; | |
namespace JWTAuth.API.Dtos | |
{ | |
public class UserForRegisterDto | |
{ | |
[Required] | |
public string Username { get; set; } | |
[Required] |
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
namespace JWTAuth.API.Dtos | |
{ | |
public class UserForLoginDto | |
{ | |
public string Username { get; set; } | |
public string Password { get; set; } | |
} | |
} |
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
{ | |
"AppSettings":{ | |
"Token": "secret key for jwt" | |
}, | |
"ConnectionStrings":{ | |
"DefaultConnection":"Server=.;Database=JWTAuthDB;Trusted_Connection=True;ConnectRetryCount=0" | |
}, | |
"Logging": { | |
"LogLevel": { | |
"Default": "Information", |