|
@@ -1,4 +1,5 @@
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
+using Microsoft.AspNetCore.Mvc;
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
using Microsoft.Extensions.Options;
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
@@ -40,7 +41,7 @@ namespace MTWorkHR.Identity.Services
|
|
|
var result = await _signInManager.CheckPasswordSignInAsync(user, request.Password, false);
|
|
|
if(!result.Succeeded)
|
|
|
{
|
|
|
- throw new AppException(GlobalInfo.lang == "en"? $"Credentials for '{request.Email} are not valid'." : $"كلمة المرور غير صحيحة.");
|
|
|
+ throw new AppException(ExceptionEnum.WrongCredentials);
|
|
|
}
|
|
|
var userResponse = await _userService.GetById(user.Id);
|
|
|
|
|
@@ -58,32 +59,6 @@ namespace MTWorkHR.Identity.Services
|
|
|
};
|
|
|
return response;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
|
|
|
|
|
|
private async Task<JwtSecurityToken> GenerateToken(ApplicationUser user)
|
|
@@ -112,7 +87,37 @@ namespace MTWorkHR.Identity.Services
|
|
|
return jwtSecurityToken;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+ public async Task<AuthResponse> AdminLogin(AuthRequest request)
|
|
|
+ {
|
|
|
+ var user = await _userManager.FindByEmailAsync(request.Email);
|
|
|
+ if (user == null)
|
|
|
+ {
|
|
|
+ throw new AppException(ExceptionEnum.EmailNotExist);
|
|
|
+ }
|
|
|
+ var roles = await _userManager.GetRolesAsync(user);
|
|
|
+ if(!roles.Any(r => r == "Admin"))
|
|
|
+ {
|
|
|
+ throw new AppException(ExceptionEnum.NotAuthorized);
|
|
|
+ }
|
|
|
+ var roleClaims = roles.Select(r => new Claim(ClaimTypes.Role, r)).ToList();
|
|
|
+
|
|
|
+ var result = await _signInManager.CheckPasswordSignInAsync(user, request.Password, false);
|
|
|
+ if (!result.Succeeded)
|
|
|
+ {
|
|
|
+ throw new AppException(ExceptionEnum.WrongCredentials);
|
|
|
+ }
|
|
|
+ var userResponse = await _userService.GetById(user.Id);
|
|
|
+
|
|
|
+ JwtSecurityToken jwtToken = await GenerateToken(user);
|
|
|
+
|
|
|
+ var response = new AuthResponse
|
|
|
+ {
|
|
|
+ User = userResponse,
|
|
|
+ Token = new JwtSecurityTokenHandler().WriteToken(jwtToken),
|
|
|
+ Expiration = jwtToken.ValidTo
|
|
|
+ };
|
|
|
+ return response;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
}
|