Files
moadiran/Back/Controllers/UserController.cs
mmrbnjd 7b8127dc72 ...
2024-04-29 07:58:41 +03:30

59 lines
2.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Back.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Shared.DTOs;
namespace Back.Controllers
{
[Route("api/[controller]")]
[Authorize]
[ApiController]
public class UserController : ControllerBase
{
private readonly servUser _servUser;
public UserController(servUser servUser)
{
_servUser = servUser;
}
[HttpPost("authenticate")]
[AllowAnonymous]
public async Task<ActionResult<UserAuthenticationDTO>> Login([FromBody]Authentication model)
{
var result = await _servUser.UserAuthentication(model.Username, model.Password);
if (result != null) return Ok(result);
else return NotFound("کاربری با این مشخصات یافت نشد");
}
[HttpGet("CheckAuthenticate")]
public async Task<ActionResult<UserAuthenticationDTO>> CheckAuthenticate()
{
// var accessToken = Request.Headers["Authorization"].ToString().Split(' ')[1];
var claim = HttpContext.User.Claims.First(c => c.Type == "UserID");
var UserID = claim.Value;
var result = await _servUser.UserAuthentication(UserID,newtoken:false);
return Ok(result);
}
[HttpPost("ChangePassword")]
public async Task<ActionResult<bool>> ChangePassword(ChangePasswordDto item)
{
if (item.newPass.Trim() != item.renewPass.Trim())
return BadRequest(new List<string> { "تکرار کلمه عبور با کلمه عبور مطابقت ندارد" });
if (item.newPass.Trim().Length <= 3)
return BadRequest(new List<string> { "کلمه عبور جدید باید بیشتر از 3کاراکتر باشد" });
var UserID = HttpContext.User.Claims.First(c => c.Type == "UserID").Value;
if (!await _servUser.PermissionChangePassword(item.oldPass.Trim(), Convert.ToInt32(UserID)))
return BadRequest(new List<string> { "کلمه عبور قبلی صحیح نمی باشد" });
return Ok(await _servUser.ChangePassword(item.newPass.Trim(), Convert.ToInt32(UserID)));
}
}
}