Skip to content

Commit 91f8170

Browse files
authored
Merge pull request #3 from Svetloslav15/snovoselski/add-authentication-to-endpoints
feat(auth): add authentication
2 parents 96f026a + 708de92 commit 91f8170

18 files changed

+676
-16
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
namespace NorthwindCRUD.Controllers
2+
{
3+
using AutoMapper;
4+
using Microsoft.AspNetCore.Authorization;
5+
using Microsoft.AspNetCore.Mvc;
6+
using NorthwindCRUD.Models.DbModels;
7+
using NorthwindCRUD.Models.InputModels;
8+
using NorthwindCRUD.Services;
9+
10+
[ApiController]
11+
[Route("[controller]")]
12+
public class AuthController : Controller
13+
{
14+
private readonly IConfiguration configuration;
15+
private readonly AuthService authService;
16+
private readonly IMapper mapper;
17+
private readonly ILogger logger;
18+
19+
public AuthController(IConfiguration configuration, AuthService authService, IMapper mapper, ILogger logger)
20+
{
21+
this.configuration = configuration;
22+
this.authService = authService;
23+
this.mapper = mapper;
24+
this.logger = logger;
25+
}
26+
27+
[AllowAnonymous]
28+
[HttpPost("Login")]
29+
public ActionResult<string> Login(LoginInputModel userModel)
30+
{
31+
try
32+
{
33+
if (ModelState.IsValid)
34+
{
35+
if (this.authService.IsAuthenticated(userModel.Email, userModel.Password))
36+
{
37+
var token = this.authService.GenerateJwtToken(userModel.Email);
38+
39+
return Ok(token);
40+
}
41+
return BadRequest("Email or password are not correct!");
42+
}
43+
44+
return BadRequest(ModelState);
45+
}
46+
catch (Exception error)
47+
{
48+
logger.LogError(error.Message);
49+
return StatusCode(500);
50+
}
51+
}
52+
53+
[AllowAnonymous]
54+
[HttpPost("Register")]
55+
public ActionResult<string> Register(RegisterInputModel userModel)
56+
{
57+
try
58+
{
59+
if (ModelState.IsValid)
60+
{
61+
if (userModel.Password != userModel.ConfirmedPassword)
62+
{
63+
return BadRequest("Passwords does not match!");
64+
}
65+
66+
if (this.authService.DoesUserExists(userModel.Email))
67+
{
68+
return BadRequest("User does not exists!");
69+
}
70+
71+
var mappedModel = this.mapper.Map<RegisterInputModel, UserDb>(userModel);
72+
var user = this.authService.RegisterUser(mappedModel);
73+
74+
if (user != null)
75+
{
76+
var token = this.authService.GenerateJwtToken(user.Email);
77+
return Ok(token);
78+
79+
}
80+
81+
return BadRequest("Email or password are not correct!");
82+
}
83+
84+
return BadRequest(ModelState);
85+
}
86+
catch (Exception error)
87+
{
88+
logger.LogError(error.Message);
89+
return StatusCode(500);
90+
}
91+
}
92+
}
93+
}

NorthwindCRUD/Controllers/CategoryController.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace NorthwindCRUD.Controllers
22
{
33
using AutoMapper;
4+
using Microsoft.AspNetCore.Authorization;
45
using Microsoft.AspNetCore.Mvc;
56
using NorthwindCRUD.Models.DbModels;
67
using NorthwindCRUD.Models.InputModels;
@@ -22,6 +23,7 @@ public CategoryController(CategoryService categoryService, IMapper mapper, ILogg
2223
}
2324

2425
[HttpGet]
26+
[Authorize]
2527
public ActionResult<CategoryInputModel[]> GetAll()
2628
{
2729
try
@@ -38,6 +40,7 @@ public ActionResult<CategoryInputModel[]> GetAll()
3840
}
3941

4042
[HttpGet("{id}")]
43+
[Authorize]
4144
public ActionResult<CategoryInputModel> GetById(int id)
4245
{
4346
try
@@ -59,6 +62,7 @@ public ActionResult<CategoryInputModel> GetById(int id)
5962
}
6063

6164
[HttpPost]
65+
[Authorize]
6266
public ActionResult<CategoryInputModel> Create(CategoryInputModel model)
6367
{
6468
try
@@ -80,6 +84,7 @@ public ActionResult<CategoryInputModel> Create(CategoryInputModel model)
8084
}
8185

8286
[HttpPut]
87+
[Authorize]
8388
public ActionResult<CategoryInputModel> Update(CategoryInputModel model)
8489
{
8590
try
@@ -101,6 +106,7 @@ public ActionResult<CategoryInputModel> Update(CategoryInputModel model)
101106
}
102107

103108
[HttpDelete("{id}")]
109+
[Authorize]
104110
public ActionResult<CategoryInputModel> Delete(int id)
105111
{
106112
try

NorthwindCRUD/Controllers/CustomerController.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace NorthwindCRUD.Controllers
22
{
33
using AutoMapper;
4+
using Microsoft.AspNetCore.Authorization;
45
using Microsoft.AspNetCore.Mvc;
56
using NorthwindCRUD.Models.DbModels;
67
using NorthwindCRUD.Models.InputModels;
@@ -22,6 +23,7 @@ public CustomerController(CustomerService customerService, IMapper mapper, ILogg
2223
}
2324

2425
[HttpGet]
26+
[Authorize]
2527
public ActionResult<CustomerInputModel[]> GetAll()
2628
{
2729
try
@@ -37,6 +39,7 @@ public ActionResult<CustomerInputModel[]> GetAll()
3739
}
3840

3941
[HttpGet("{id}")]
42+
[Authorize]
4043
public ActionResult<CustomerInputModel> GetById(string id)
4144
{
4245
try
@@ -58,6 +61,7 @@ public ActionResult<CustomerInputModel> GetById(string id)
5861
}
5962

6063
[HttpPost]
64+
[Authorize]
6165
public ActionResult<CustomerInputModel> Create(CustomerInputModel model)
6266
{
6367
try
@@ -79,6 +83,7 @@ public ActionResult<CustomerInputModel> Create(CustomerInputModel model)
7983
}
8084

8185
[HttpPut]
86+
[Authorize]
8287
public ActionResult<CustomerInputModel> Update(CustomerInputModel model)
8388
{
8489
try
@@ -100,6 +105,7 @@ public ActionResult<CustomerInputModel> Update(CustomerInputModel model)
100105
}
101106

102107
[HttpDelete("{id}")]
108+
[Authorize]
103109
public ActionResult<CustomerInputModel> Delete(string id)
104110
{
105111
try

NorthwindCRUD/Controllers/EmployeeController.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace NorthwindCRUD.Controllers
22
{
33
using AutoMapper;
4+
using Microsoft.AspNetCore.Authorization;
45
using Microsoft.AspNetCore.Mvc;
56
using NorthwindCRUD.Models.DbModels;
67
using NorthwindCRUD.Models.InputModels;
@@ -22,6 +23,7 @@ public EmployeeController(EmployeeService employeeService, IMapper mapper, ILogg
2223
}
2324

2425
[HttpGet]
26+
[Authorize]
2527
public ActionResult<EmployeeInputModel[]> GetAll()
2628
{
2729
try
@@ -37,6 +39,7 @@ public ActionResult<EmployeeInputModel[]> GetAll()
3739
}
3840

3941
[HttpGet("{id}")]
42+
[Authorize]
4043
public ActionResult<EmployeeInputModel> GetById(int id)
4144
{
4245
try
@@ -58,6 +61,7 @@ public ActionResult<EmployeeInputModel> GetById(int id)
5861
}
5962

6063
[HttpPost]
64+
[Authorize]
6165
public ActionResult<EmployeeInputModel> Create(EmployeeInputModel model)
6266
{
6367
try
@@ -79,6 +83,7 @@ public ActionResult<EmployeeInputModel> Create(EmployeeInputModel model)
7983
}
8084

8185
[HttpPut]
86+
[Authorize]
8287
public ActionResult<EmployeeInputModel> Update(EmployeeInputModel model)
8388
{
8489
try
@@ -100,6 +105,7 @@ public ActionResult<EmployeeInputModel> Update(EmployeeInputModel model)
100105
}
101106

102107
[HttpDelete("{id}")]
108+
[Authorize]
103109
public ActionResult<EmployeeInputModel> Delete(int id)
104110
{
105111
try

NorthwindCRUD/Controllers/OrderController.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace NorthwindCRUD.Controllers
22
{
33
using AutoMapper;
4+
using Microsoft.AspNetCore.Authorization;
45
using Microsoft.AspNetCore.Mvc;
56
using NorthwindCRUD.Models.DbModels;
67
using NorthwindCRUD.Models.InputModels;
@@ -22,6 +23,7 @@ public OrderController(OrderService orderService, IMapper mapper, ILogger logger
2223
}
2324

2425
[HttpGet]
26+
[Authorize]
2527
public ActionResult<OrderInputModel[]> GetAll()
2628
{
2729
try
@@ -37,6 +39,7 @@ public ActionResult<OrderInputModel[]> GetAll()
3739
}
3840

3941
[HttpGet("{id}")]
42+
[Authorize]
4043
public ActionResult<OrderInputModel> GetById(int id)
4144
{
4245
try
@@ -58,6 +61,7 @@ public ActionResult<OrderInputModel> GetById(int id)
5861
}
5962

6063
[HttpPost]
64+
[Authorize]
6165
public ActionResult<OrderInputModel> Create(OrderInputModel model)
6266
{
6367
try
@@ -79,6 +83,7 @@ public ActionResult<OrderInputModel> Create(OrderInputModel model)
7983
}
8084

8185
[HttpPut]
86+
[Authorize]
8287
public ActionResult<OrderInputModel> Update(OrderInputModel model)
8388
{
8489
try
@@ -100,6 +105,7 @@ public ActionResult<OrderInputModel> Update(OrderInputModel model)
100105
}
101106

102107
[HttpDelete("{id}")]
108+
[Authorize]
103109
public ActionResult<OrderInputModel> Delete(int id)
104110
{
105111
try

NorthwindCRUD/DataContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public DataContext(DbContextOptions options) : base(options)
1616
public DbSet<CustomerDb> Customers { get; set; }
1717
public DbSet<EmployeeDb> Employees { get; set; }
1818
public DbSet<OrderDb> Orders { get; set; }
19+
public DbSet<UserDb> Users { get; set; }
1920

2021
protected override void OnModelCreating(ModelBuilder modelBuilder)
2122
{

NorthwindCRUD/Helpers/MappingProfiles.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public MappingProfiles()
1313
CreateMap<EmployeeInputModel, EmployeeDb>().ReverseMap();
1414
CreateMap<OrderInputModel, OrderDb>().ReverseMap();
1515
CreateMap<AddressInputModel, AddressDb>().ReverseMap();
16+
CreateMap<LoginInputModel, UserDb>().ReverseMap();
17+
CreateMap<RegisterInputModel, UserDb>().ReverseMap();
1618
}
1719
}
1820
}

0 commit comments

Comments
 (0)