Skip to content

Instantly share code, notes, and snippets.

View nishanc's full-sized avatar
🚀
This is the way!

Nishan Chathuranga Wickramarathna nishanc

🚀
This is the way!
View GitHub Profile
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;
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;
using System.Threading.Tasks;
namespace JWTAuth.API.Data
{
public class AuthRepository : IAuthRepository
{
private readonly DataContext _context;
public AuthRepository(DataContext context)
{
_context = context;
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)
namespace JWTAuth.API.Dtos
{
public class UserForRegisterDto
{
public string Username { get; set; }
public string Password { get; set; }
}
}
[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");
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
using System.ComponentModel.DataAnnotations;
namespace JWTAuth.API.Dtos
{
public class UserForRegisterDto
{
[Required]
public string Username { get; set; }
[Required]
namespace JWTAuth.API.Dtos
{
public class UserForLoginDto
{
public string Username { get; set; }
public string Password { get; set; }
}
}
{
"AppSettings":{
"Token": "secret key for jwt"
},
"ConnectionStrings":{
"DefaultConnection":"Server=.;Database=JWTAuthDB;Trusted_Connection=True;ConnectRetryCount=0"
},
"Logging": {
"LogLevel": {
"Default": "Information",