Skip to content

Commit ed6232b

Browse files
committedJan 27, 2025·
add authorization handler for http client
1 parent c0106f3 commit ed6232b

File tree

9 files changed

+218
-265
lines changed

9 files changed

+218
-265
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace CheckDrive.Web.Constants;
2+
3+
public static class HeaderConstants
4+
{
5+
public const string AccessTokenHeader = "access_token";
6+
public const string RefreshTokenHeader = "refresh_token";
7+
public const string AuthenticationSchema = "Bearer";
8+
}
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,24 @@
1-
using CheckDrive.ApiContracts.Account;
2-
using CheckDrive.Web.Stores.Accounts;
3-
using CheckDrive.Web.Stores.User;
1+
using CheckDrive.Web.Requests.Auth;
2+
using CheckDrive.Web.Stores.Auth;
43
using Microsoft.AspNetCore.Mvc;
5-
using System.IdentityModel.Tokens.Jwt;
6-
using System.Security.Claims;
74

8-
public class AuthController : Controller
9-
{
10-
private readonly IUserDataStore _userDataStore;
11-
private readonly IAccountDataStore _accountDataStore;
5+
namespace CheckDrive.Web.Controllers;
126

13-
public AuthController(IUserDataStore userDataStore, IAccountDataStore accountDataStore)
14-
{
15-
_userDataStore = userDataStore;
16-
_accountDataStore = accountDataStore;
17-
}
18-
19-
public IActionResult Login()
20-
{
21-
HttpContext.Response.Cookies.Delete("tasty-cookies");
22-
return RedirectToAction("Index", "Auth");
23-
}
24-
25-
public IActionResult Index()
26-
{
27-
return View();
28-
}
7+
public class AuthController(IAuthStore authStore) : Controller
8+
{
9+
public IActionResult Login() => View();
2910

3011
[HttpPost]
31-
public async Task<IActionResult> Index(AccountForLoginDto loginViewModel)
12+
public async Task<IActionResult> Login(LoginRequest request)
3213
{
3314
if (!ModelState.IsValid)
3415
{
35-
return View(loginViewModel);
16+
return View(request);
3617
}
3718

38-
var user = new AccountForLoginDto
39-
{
40-
Login = loginViewModel.Login,
41-
Password = loginViewModel.Password,
42-
};
43-
44-
var (success, token) = await _userDataStore.AuthenticateLoginAsync(user);
45-
46-
if (success)
47-
{
48-
HttpContext.Response.Cookies.Append("tasty-cookies", token, new CookieOptions
49-
{
50-
Secure = true,
51-
SameSite = SameSiteMode.Strict,
52-
HttpOnly = true,
53-
IsEssential = true
54-
});
55-
56-
var tokenHandler = new JwtSecurityTokenHandler();
57-
var jwtToken = tokenHandler.ReadToken(token) as JwtSecurityToken;
58-
if (jwtToken == null)
59-
{
60-
return RedirectToAction("Login", "Account");
61-
}
62-
var roleId = jwtToken.Claims.First(claim => claim.Type == ClaimTypes.Role).Value;
63-
var accountId = jwtToken.Claims.First(claim => claim.Type == ClaimTypes.NameIdentifier).Value;
64-
int accountIds = Int32.Parse(accountId);
65-
66-
switch (roleId)
67-
{
68-
case "1":
69-
return RedirectToAction("Index", "Dashboard");
70-
case "3":
71-
TempData["AccountId"] = accountId;
72-
return RedirectToAction("PersonalIndex", "DoctorReviews");
73-
case "4":
74-
TempData["AccountId"] = accountId;
75-
return RedirectToAction("PersonalIndex", "OperatorReviews");
76-
case "5":
77-
TempData["AccountId"] = accountId;
78-
return RedirectToAction("PersonalIndex", "DispatcherReviews");
79-
case "6":
80-
TempData["AccountId"] = accountId;
81-
return RedirectToAction("PersonalIndex", "MechanicHandovers");
82-
default:
83-
return RedirectToAction("Index", "Auth");
84-
}
85-
}
19+
await authStore.LoginAsync(request);
8620

87-
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
88-
ModelState.AddModelError("Password", "Incorrect password or login");
89-
return View(loginViewModel);
21+
return RedirectToAction("Index", "Home");
9022
}
9123
}
9224

‎CheckDrive.Web/CheckDrive.Web/Controllers/CarsController.cs

+118-119
Original file line numberDiff line numberDiff line change
@@ -3,155 +3,154 @@
33
using CheckDrive.Web.Stores.Cars;
44
using Microsoft.AspNetCore.Mvc;
55

6-
namespace CheckDrive.Web.Controllers
7-
{
8-
public class CarsController : Controller
9-
{
10-
private readonly ICarDataStore _carDataStore;
6+
namespace CheckDrive.Web.Controllers;
117

12-
public CarsController(ICarDataStore carDataStore)
13-
{
14-
_carDataStore = carDataStore;
15-
}
16-
17-
public async Task<IActionResult> Index(string? searchString, int? pageNumber)
18-
{
19-
var cars = await _carDataStore.GetCarsAsync(searchString, pageNumber);
8+
public class CarsController : Controller
9+
{
10+
private readonly ICarDataStore _carDataStore;
2011

21-
ViewBag.SearchString = searchString;
12+
public CarsController(ICarDataStore carDataStore)
13+
{
14+
_carDataStore = carDataStore;
15+
}
2216

23-
ViewBag.PageSize = cars.PageSize;
24-
ViewBag.PageCount = cars.TotalPages;
25-
ViewBag.TotalCount = cars.TotalCount;
26-
ViewBag.CurrentPage = cars.PageNumber;
27-
ViewBag.HasPreviousPage = cars.HasPreviousPage;
28-
ViewBag.HasNextPage = cars.HasNextPage;
17+
public async Task<IActionResult> Index(string? searchString, int? pageNumber)
18+
{
19+
var cars = await _carDataStore.GetCarsAsync(searchString, pageNumber);
2920

30-
var _cars = cars.Data.Select(c => new
31-
{
32-
c.Id,
33-
c.Model,
34-
c.Number,
35-
c.Mileage,
36-
c.Color,
37-
c.RemainingFuel,
38-
c.MeduimFuelConsumption,
39-
c.FuelTankCapacity,
40-
c.ManufacturedYear,
41-
Status = ((CarStatusDto)c.CarStatus) switch
42-
{
43-
CarStatusDto.Free => "Bo'sh",
44-
CarStatusDto.Busy => "Band",
45-
CarStatusDto.Limited => "Limit tugagan",
46-
_ => "No`malum holat"
47-
}
48-
}).ToList();
49-
50-
ViewBag.Cars = _cars;
51-
return View();
52-
}
21+
ViewBag.SearchString = searchString;
5322

54-
public async Task<IActionResult> CarHistoryIndex(string? searchString, int? pageNumber, int? year, int? month)
55-
{
56-
var cars = await _carDataStore.GetCarsHistoryAsync(searchString, pageNumber, year, month);
57-
58-
ViewBag.SearchString = searchString;
59-
ViewBag.Cars = cars.Data;
60-
61-
ViewBag.PageSize = cars.PageSize;
62-
ViewBag.PageCount = cars.TotalPages;
63-
ViewBag.TotalCount = cars.TotalCount;
64-
ViewBag.CurrentPage = cars.PageNumber;
65-
ViewBag.HasPreviousPage = cars.HasPreviousPage;
66-
ViewBag.HasNextPage = cars.HasNextPage;
67-
return View();
68-
}
23+
ViewBag.PageSize = cars.PageSize;
24+
ViewBag.PageCount = cars.TotalPages;
25+
ViewBag.TotalCount = cars.TotalCount;
26+
ViewBag.CurrentPage = cars.PageNumber;
27+
ViewBag.HasPreviousPage = cars.HasPreviousPage;
28+
ViewBag.HasNextPage = cars.HasNextPage;
6929

70-
public async Task<IActionResult> Details(int id)
30+
var _cars = cars.Data.Select(c => new
7131
{
72-
var car = await _carDataStore.GetCarAsync(id);
73-
if (car == null)
32+
c.Id,
33+
c.Model,
34+
c.Number,
35+
c.Mileage,
36+
c.Color,
37+
c.RemainingFuel,
38+
c.MeduimFuelConsumption,
39+
c.FuelTankCapacity,
40+
c.ManufacturedYear,
41+
Status = ((CarStatusDto)c.CarStatus) switch
7442
{
75-
return NotFound();
43+
CarStatusDto.Free => "Bo'sh",
44+
CarStatusDto.Busy => "Band",
45+
CarStatusDto.Limited => "Limit tugagan",
46+
_ => "No`malum holat"
7647
}
77-
return View(car);
78-
}
48+
}).ToList();
7949

80-
public async Task<IActionResult> DetailsForMechanicAcceptance(int id)
81-
{
82-
var car = await _carDataStore.GetCarAsync(id);
83-
if (car == null)
84-
{
85-
return NotFound();
86-
}
87-
return PartialView("_CarDetailsForMechanicAcceptance", car);
88-
}
50+
ViewBag.Cars = _cars;
51+
return View();
52+
}
53+
54+
public async Task<IActionResult> CarHistoryIndex(string? searchString, int? pageNumber, int? year, int? month)
55+
{
56+
var cars = await _carDataStore.GetCarsHistoryAsync(searchString, pageNumber, year, month);
57+
58+
ViewBag.SearchString = searchString;
59+
ViewBag.Cars = cars.Data;
60+
61+
ViewBag.PageSize = cars.PageSize;
62+
ViewBag.PageCount = cars.TotalPages;
63+
ViewBag.TotalCount = cars.TotalCount;
64+
ViewBag.CurrentPage = cars.PageNumber;
65+
ViewBag.HasPreviousPage = cars.HasPreviousPage;
66+
ViewBag.HasNextPage = cars.HasNextPage;
67+
return View();
68+
}
8969

90-
public async Task<IActionResult> DetailsForMechanicHandover(int id)
70+
public async Task<IActionResult> Details(int id)
71+
{
72+
var car = await _carDataStore.GetCarAsync(id);
73+
if (car == null)
9174
{
92-
var car = await _carDataStore.GetCarAsync(id);
93-
if (car == null)
94-
{
95-
return NotFound();
96-
}
97-
return PartialView("_CarDetailsForMechanicHandover", car);
75+
return NotFound();
9876
}
77+
return View(car);
78+
}
9979

100-
public IActionResult Create()
80+
public async Task<IActionResult> DetailsForMechanicAcceptance(int id)
81+
{
82+
var car = await _carDataStore.GetCarAsync(id);
83+
if (car == null)
10184
{
102-
return View();
85+
return NotFound();
10386
}
87+
return PartialView("_CarDetailsForMechanicAcceptance", car);
88+
}
10489

105-
[HttpPost]
106-
[ValidateAntiForgeryToken]
107-
public async Task<IActionResult> Create([Bind("Model,Color,Number,RemainingFuel,Mileage, MeduimFuelConsumption,FuelTankCapacity,ManufacturedYear, OneYearMediumDistance")] CarForCreateDto car)
90+
public async Task<IActionResult> DetailsForMechanicHandover(int id)
91+
{
92+
var car = await _carDataStore.GetCarAsync(id);
93+
if (car == null)
10894
{
109-
if (ModelState.IsValid)
110-
{
111-
var newCar = await _carDataStore.CreateCarAsync(car);
112-
return RedirectToAction(nameof(Index));
113-
}
114-
return View(car);
95+
return NotFound();
11596
}
97+
return PartialView("_CarDetailsForMechanicHandover", car);
98+
}
99+
100+
public IActionResult Create()
101+
{
102+
return View();
103+
}
116104

117-
public async Task<IActionResult> Edit(int id)
105+
[HttpPost]
106+
[ValidateAntiForgeryToken]
107+
public async Task<IActionResult> Create([Bind("Model,Color,Number,RemainingFuel,Mileage, MeduimFuelConsumption,FuelTankCapacity,ManufacturedYear, OneYearMediumDistance")] CarForCreateDto car)
108+
{
109+
if (ModelState.IsValid)
118110
{
119-
var car = await _carDataStore.GetCarAsync(id);
120-
if (car == null)
121-
{
122-
return NotFound();
123-
}
124-
return View(car);
111+
var newCar = await _carDataStore.CreateCarAsync(car);
112+
return RedirectToAction(nameof(Index));
125113
}
114+
return View(car);
115+
}
126116

127-
[HttpPost]
128-
[ValidateAntiForgeryToken]
129-
public async Task<IActionResult> Edit(int id, [Bind("Id,Model,Color,Number,RemainingFuel,Mileage, MeduimFuelConsumption,FuelTankCapacity,ManufacturedYear, OneYearMediumDistance")] CarForUpdateDto car)
117+
public async Task<IActionResult> Edit(int id)
118+
{
119+
var car = await _carDataStore.GetCarAsync(id);
120+
if (car == null)
130121
{
131-
if (ModelState.IsValid)
132-
{
133-
var newCar = await _carDataStore.UpdateCarAsync(id, car);
134-
return RedirectToAction(nameof(Index));
135-
}
136-
return View(car);
122+
return NotFound();
137123
}
124+
return View(car);
125+
}
138126

139-
public async Task<IActionResult> Delete(int id)
127+
[HttpPost]
128+
[ValidateAntiForgeryToken]
129+
public async Task<IActionResult> Edit(int id, [Bind("Id,Model,Color,Number,RemainingFuel,Mileage, MeduimFuelConsumption,FuelTankCapacity,ManufacturedYear, OneYearMediumDistance")] CarForUpdateDto car)
130+
{
131+
if (ModelState.IsValid)
140132
{
141-
var car = await _carDataStore.GetCarAsync(id);
142-
if (car == null)
143-
{
144-
return NotFound();
145-
}
146-
return View(car);
133+
var newCar = await _carDataStore.UpdateCarAsync(id, car);
134+
return RedirectToAction(nameof(Index));
147135
}
136+
return View(car);
137+
}
148138

149-
[HttpPost, ActionName("Delete")]
150-
[ValidateAntiForgeryToken]
151-
public async Task<IActionResult> DeleteConfirmed(int id)
139+
public async Task<IActionResult> Delete(int id)
140+
{
141+
var car = await _carDataStore.GetCarAsync(id);
142+
if (car == null)
152143
{
153-
await _carDataStore.DeleteCarAsync(id);
154-
return RedirectToAction(nameof(Index));
144+
return NotFound();
155145
}
146+
return View(car);
147+
}
148+
149+
[HttpPost, ActionName("Delete")]
150+
[ValidateAntiForgeryToken]
151+
public async Task<IActionResult> DeleteConfirmed(int id)
152+
{
153+
await _carDataStore.DeleteCarAsync(id);
154+
return RedirectToAction(nameof(Index));
156155
}
157156
}

0 commit comments

Comments
 (0)
Please sign in to comment.