From 3c340fdbde059c440b4938df9016399e966140b1 Mon Sep 17 00:00:00 2001
From: "Sharafiddin.Mukhtorov"
<129146459+SharafiddinMukhtorov@users.noreply.github.com>
Date: Fri, 14 Jun 2024 18:55:51 +0500
Subject: [PATCH] Updated AuthController
---
.../CheckDrive.Web/CheckDrive.Web.csproj | 2 ++
.../Controllers/AuthController.cs | 36 ++++++++++++++-----
2 files changed, 29 insertions(+), 9 deletions(-)
diff --git a/CheckDrive.Web/CheckDrive.Web/CheckDrive.Web.csproj b/CheckDrive.Web/CheckDrive.Web/CheckDrive.Web.csproj
index cc036b69..a6be0f67 100644
--- a/CheckDrive.Web/CheckDrive.Web/CheckDrive.Web.csproj
+++ b/CheckDrive.Web/CheckDrive.Web/CheckDrive.Web.csproj
@@ -36,6 +36,8 @@
+
+
diff --git a/CheckDrive.Web/CheckDrive.Web/Controllers/AuthController.cs b/CheckDrive.Web/CheckDrive.Web/Controllers/AuthController.cs
index 43d9b057..af86f34c 100644
--- a/CheckDrive.Web/CheckDrive.Web/Controllers/AuthController.cs
+++ b/CheckDrive.Web/CheckDrive.Web/Controllers/AuthController.cs
@@ -1,7 +1,10 @@
using CheckDrive.Web.Constants;
+using CheckDrive.Web.Models;
using CheckDrive.Web.Stores.User;
using CheckDrive.Web.ViewModels;
using Microsoft.AspNetCore.Mvc;
+using System.IdentityModel.Tokens.Jwt;
+using System.Security.Claims;
namespace CheckDrive.Web.Controllers
{
@@ -41,16 +44,31 @@ public async Task Index(LoginViewModel loginViewModel)
var (success, token) = await _userDataStore.AuthenticateLoginAsync(user);
- if (success)
+ var tokenHandler = new JwtSecurityTokenHandler();
+ var jwtToken = tokenHandler.ReadToken(token) as JwtSecurityToken;
+ if (jwtToken == null)
{
- HttpContext.Response.Cookies.Append("tasty-cookies", token, new CookieOptions
- {
- Secure = true,
- SameSite = SameSiteMode.Strict,
- HttpOnly = true,
- IsEssential = true
- });
- return RedirectToAction("Index", "Dashboard");
+ return RedirectToAction("Login", "Account");
+ }
+ var roleId = jwtToken.Claims.First(claim => claim.Type == ClaimTypes.Role).Value;
+
+ switch (roleId)
+ {
+ case "1":
+ return RedirectToAction("Index", "Dashboard");
+ break;
+ case "3":
+ return RedirectToAction("Index", "PersonalDoctorReviews");
+ break;
+ case "4":
+ return RedirectToAction("Index", "PersonalOperatorReviews");
+ break;
+ case "5":
+ return RedirectToAction("Index", "Dashboard");
+ break;
+ case "6":
+ return RedirectToAction("Index", "Dashboard");
+ break;
}
ModelState.AddModelError(string.Empty, "Invalid login attempt.");