2022-05-19 15:47:12 -07:00
|
|
|
using System.Security.Claims;
|
2022-05-19 13:50:10 -07:00
|
|
|
using API.Dtos;
|
|
|
|
using API.Errors;
|
2022-05-19 16:51:00 -07:00
|
|
|
using API.Extensions;
|
2022-05-19 13:50:10 -07:00
|
|
|
using Core.Entities.Identity;
|
2022-05-19 15:47:12 -07:00
|
|
|
using Core.Interfaces;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2022-05-19 13:50:10 -07:00
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
namespace API.Controllers
|
|
|
|
{
|
|
|
|
public class AccountController : BaseApiController
|
|
|
|
{
|
|
|
|
private readonly UserManager<AppUser> _userManager;
|
|
|
|
private readonly SignInManager<AppUser> _signInManager;
|
2022-05-19 15:47:12 -07:00
|
|
|
private readonly ITokenService _tokenService;
|
|
|
|
public AccountController(UserManager<AppUser> userManager, SignInManager<AppUser> signInManager, ITokenService tokenService)
|
2022-05-19 13:50:10 -07:00
|
|
|
{
|
2022-05-19 15:47:12 -07:00
|
|
|
_tokenService = tokenService;
|
2022-05-19 13:50:10 -07:00
|
|
|
_signInManager = signInManager;
|
|
|
|
_userManager = userManager;
|
|
|
|
}
|
|
|
|
|
2022-05-19 15:47:12 -07:00
|
|
|
[Authorize]
|
|
|
|
[HttpGet]
|
|
|
|
public async Task<ActionResult<UserDto>> GetCurrentUser()
|
|
|
|
{
|
2022-05-19 16:51:00 -07:00
|
|
|
var user = await _userManager.FindByEmailFromClaimsPrinciple(User);
|
2022-05-19 15:47:12 -07:00
|
|
|
return new UserDto
|
|
|
|
{
|
|
|
|
Email = user.Email,
|
|
|
|
Token = _tokenService.CreateToken(user),
|
|
|
|
DisplayName = user.DisplayName
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet("emailexists")]
|
|
|
|
public async Task<ActionResult<bool>> CheckEmailExistsAsync([FromQuery] string email){
|
|
|
|
return await _userManager.FindByEmailAsync(email) != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
[Authorize]
|
|
|
|
[HttpGet("address")]
|
|
|
|
public async Task<ActionResult<Address>> GetUserAddress()
|
|
|
|
{
|
2022-05-19 16:51:00 -07:00
|
|
|
var user = await _userManager.FindUserByClaimsPrincipleWithAddressAsync(User);
|
2022-05-19 15:47:12 -07:00
|
|
|
return user.Address;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-05-19 13:50:10 -07:00
|
|
|
[HttpPost("login")]
|
|
|
|
public async Task<ActionResult<UserDto>> Login(LoginDto loginDto)
|
|
|
|
{
|
|
|
|
var user = await _userManager.FindByEmailAsync(loginDto.Email);
|
|
|
|
if (user == null) return Unauthorized(new ApiResponse(401));
|
|
|
|
var results = await _signInManager.CheckPasswordSignInAsync(user, loginDto.Password, false);
|
|
|
|
if(!results.Succeeded) return Unauthorized(new ApiResponse(401));
|
|
|
|
return new UserDto
|
|
|
|
{
|
|
|
|
Email = user.Email,
|
2022-05-19 15:47:12 -07:00
|
|
|
Token = _tokenService.CreateToken(user),
|
2022-05-19 13:50:10 -07:00
|
|
|
DisplayName = user.DisplayName
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost("register")]
|
|
|
|
public async Task<ActionResult<UserDto>> Register(RegisterDto registerDto)
|
|
|
|
{
|
|
|
|
var user = new AppUser
|
|
|
|
{
|
|
|
|
DisplayName = registerDto.DisplayName,
|
|
|
|
Email = registerDto.Email,
|
|
|
|
UserName = registerDto.Email
|
|
|
|
};
|
|
|
|
|
|
|
|
var results = await _userManager.CreateAsync(user, registerDto.Password);
|
|
|
|
if(!results.Succeeded) return BadRequest(new ApiResponse(400));
|
|
|
|
return new UserDto
|
|
|
|
{
|
|
|
|
DisplayName = user.DisplayName,
|
2022-05-19 15:47:12 -07:00
|
|
|
Token = _tokenService.CreateToken(user),
|
2022-05-19 13:50:10 -07:00
|
|
|
Email = user.Email
|
|
|
|
};
|
|
|
|
}
|
2022-05-19 15:47:12 -07:00
|
|
|
|
2022-05-19 13:50:10 -07:00
|
|
|
}
|
|
|
|
}
|