2022-09-10 00:33:17 +03:00
|
|
|
using System.Security.Claims;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using BlueWest.WebApi.Context.Users;
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
namespace BlueWest.WebApi.Controllers;
|
|
|
|
|
2022-09-10 07:12:03 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Auth controller
|
|
|
|
/// </summary>
|
2022-09-10 00:33:17 +03:00
|
|
|
[Route("api/[controller]")]
|
|
|
|
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
|
|
|
[Authorize(AuthenticationSchemes = CookieAuthenticationDefaults.AuthenticationScheme)]
|
|
|
|
[ApiController]
|
|
|
|
public class AuthController : Controller
|
|
|
|
{
|
|
|
|
private readonly IAuthManager _authManager;
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
|
2022-09-10 07:12:03 +03:00
|
|
|
/// <summary>
|
|
|
|
///
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="mapper"></param>
|
|
|
|
/// <param name="authManager"></param>
|
|
|
|
/// <param name="userManager"></param>
|
|
|
|
public AuthController( IAuthManager authManager, IUserManager userManager)
|
2022-09-10 00:33:17 +03:00
|
|
|
{
|
|
|
|
_authManager = authManager;
|
|
|
|
_userManager = userManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-10 07:12:03 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Signup user
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="registerViewModel"></param>
|
|
|
|
/// <returns></returns>
|
2022-09-10 00:33:17 +03:00
|
|
|
[AllowAnonymous]
|
|
|
|
[HttpPost("register")]
|
|
|
|
public async Task<ActionResult<IdentityResult>> SignupUserAsync(RegisterViewModel registerViewModel)
|
|
|
|
{
|
|
|
|
return await _authManager.CreateUserAsync(registerViewModel);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
[HttpPost("login")]
|
|
|
|
public async Task<ActionResult<IdentityResult>> GetTokenAsync(LoginViewModel loginViewModel)
|
|
|
|
{
|
|
|
|
var loginResultSucceded = await _authManager.GetToken(loginViewModel);
|
|
|
|
|
|
|
|
if (loginResultSucceded != null)
|
|
|
|
{
|
2022-09-10 07:12:03 +03:00
|
|
|
return Ok(loginResultSucceded);
|
2022-09-10 00:33:17 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
return Problem();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
[HttpPost("login2")]
|
|
|
|
public async Task<ActionResult<IdentityResult>> DoLoginAsync(LoginViewModel loginDto)
|
|
|
|
{
|
|
|
|
var user = await _userManager.FindByEmailAsync(loginDto.Email);
|
2022-09-10 07:12:03 +03:00
|
|
|
|
2022-09-10 00:33:17 +03:00
|
|
|
if (user != null)
|
|
|
|
{
|
|
|
|
if(await _userManager.CheckPasswordAsync(user, loginDto.Password))
|
|
|
|
{
|
|
|
|
var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
|
|
|
|
identity.AddClaim(new Claim(ClaimTypes.Email, user.Email));
|
|
|
|
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));
|
|
|
|
return Json(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Json(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
[HttpPost("logout")]
|
|
|
|
public async Task<ActionResult<IdentityResult>> DoLogoutAsync(LoginViewModel loginDto)
|
|
|
|
{
|
|
|
|
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
|
|
|
|
return Json(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet("test")]
|
|
|
|
public ActionResult TestRequest()
|
|
|
|
{
|
|
|
|
return Ok(new {Message = "Test"});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|