|
@@ -1,10 +1,13 @@
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
+using Microsoft.EntityFrameworkCore;
|
|
|
using Microsoft.Extensions.Options;
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
using MTWorkHR.Application.Exceptions;
|
|
|
using MTWorkHR.Application.Filters;
|
|
|
using MTWorkHR.Application.Identity;
|
|
|
+using MTWorkHR.Application.Mapper;
|
|
|
using MTWorkHR.Application.Models;
|
|
|
+using MTWorkHR.Application.Services.Interfaces;
|
|
|
using MTWorkHR.Core.Global;
|
|
|
using MTWorkHR.Infrastructure.Entities;
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
@@ -18,16 +21,19 @@ namespace MTWorkHR.Identity.Services
|
|
|
private readonly UserManager<ApplicationUser> _userManager;
|
|
|
private readonly SignInManager<ApplicationUser> _signInManager;
|
|
|
private readonly AppSettingsConfiguration _configuration;
|
|
|
- public AuthService(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager, AppSettingsConfiguration configuration)
|
|
|
+ private readonly IUserService _userService;
|
|
|
+
|
|
|
+ public AuthService(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager, AppSettingsConfiguration configuration, IUserService userService)
|
|
|
{
|
|
|
_userManager = userManager;
|
|
|
_signInManager = signInManager;
|
|
|
_configuration = configuration;
|
|
|
+ _userService = userService;
|
|
|
}
|
|
|
public async Task<AuthResponse> Login(AuthRequest request)
|
|
|
{
|
|
|
- var user = await _userManager.FindByEmailAsync(request.Email);
|
|
|
- if(user == null)
|
|
|
+ var user = await _userManager.FindByEmailAsync(request.Email);
|
|
|
+ if (user == null)
|
|
|
{
|
|
|
throw new AppException(ExceptionEnum.RecordNotExist);
|
|
|
}
|
|
@@ -36,13 +42,17 @@ namespace MTWorkHR.Identity.Services
|
|
|
{
|
|
|
throw new AppException($"Credentials for '{request.Email} are not valid'.");
|
|
|
}
|
|
|
+ var userResponse = await _userService.GetById(user.Id);
|
|
|
+
|
|
|
JwtSecurityToken jwtToken = await GenerateToken(user);
|
|
|
+
|
|
|
var response = new AuthResponse
|
|
|
{
|
|
|
- Id = user.Id,
|
|
|
- Email = user.Email,
|
|
|
- UserName = user.UserName,
|
|
|
- UserTypeId = user.UserType,
|
|
|
+ User = userResponse,
|
|
|
+ //Id = user.Id,
|
|
|
+ //Email = user.Email,
|
|
|
+ //UserName = user.UserName,
|
|
|
+ //UserTypeId = user.UserType,
|
|
|
Token = new JwtSecurityTokenHandler().WriteToken( jwtToken),
|
|
|
Expiration = jwtToken.ValidTo
|
|
|
};
|