123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- using Abp.Json;
- using AutoMapper;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Options;
- using Microsoft.IdentityModel.Tokens;
- using System;
- using System.IdentityModel.Tokens.Jwt;
- using System.Net;
- using System.Security.Claims;
- using System.Text;
- using UnivateProperties_API.Containers.Users;
- using UnivateProperties_API.Containers.Users.Simple;
- using UnivateProperties_API.Helpers;
- using UnivateProperties_API.Model.Users;
- using UnivateProperties_API.Repository.Users;
-
- namespace UnivateProperties_API.Controllers.Users
- {
- [Route("api/[controller]")]
- [ApiController]
- public class RegisterController : ControllerBase
- {
- private readonly IRegisterRepository _Repo;
- private readonly IMapper _mapper;
- private readonly AppSettings _appSettings;
-
- public RegisterController(IRegisterRepository repo, IMapper mapper, IOptions<AppSettings> appSettings)
- {
- _Repo = repo;
- _mapper = mapper;
- _appSettings = appSettings.Value;
- }
-
- //Works
- [AllowAnonymous]
- [HttpPost("authenticate")]
- public IActionResult Authenticate([FromBody]UserDto userDto)
- {
- try
- {
- var user = _Repo.Authenticate(userDto.Username, userDto.Password);
-
-
- if (user == null)
- return BadRequest(new { message = "Username or password is incorrect" });
-
- var tokenHandler = new JwtSecurityTokenHandler();
- var key = Encoding.ASCII.GetBytes(_appSettings.Secret);
- var tokenDescriptor = new SecurityTokenDescriptor
- {
- Subject = new ClaimsIdentity(new Claim[]
- {
- new Claim(ClaimTypes.Name, user.Id.ToString()),
- new Claim(ClaimTypes.Role, user.Role)
- }),
- Expires = DateTime.UtcNow.AddMinutes(15),
- SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
- };
- var token = tokenHandler.CreateToken(tokenDescriptor);
- SimpleItem item = new SimpleItem()
- {
- User = new SimpleUserDto()
- {
- Id = user.Id,
- Role = user.Role,
- Username = user.Username,
- LoginPasswordChange = user.LoginPasswordChange
- },
- Person = _Repo.UserDetails(user.Id),
- Token = new SimpleTokenDto()
- {
- Token = tokenHandler.WriteToken(token),
- Expires = tokenDescriptor.Expires.Value
- }
- };
- // return basic user info (without password) and token to store client side
- return Ok(item);
- }
- catch(Exception ex)
- {
- return StatusCode(401, ex);
- }
-
- }
-
- //Writes to DB
- [AllowAnonymous]
- [HttpPost("register")]
- public IActionResult Register([FromBody]UserDto individual)
- {
- _mapper.Map<Individual>(individual);
-
- try
- {
- _Repo.CreatePerson(individual, PersonType.Individual, true, null);
- return Ok();
- }
- catch (AppException ex)
- {
- return BadRequest(new { message = ex.Message });
- //return StatusCode(409, ex);
- }
- }
-
- //Writes to DB
- [AllowAnonymous]
- [HttpPost("forgotPassword/{mail}")]
- public IActionResult ForgotPassword(string mail)
- {
- try
- {
- _Repo.ForgotPasswordMailCheck(mail);
- return Ok();
- }
- catch(Exception ex)
- {
- return BadRequest(new { message = ex.Message });
- }
- }
-
- //Writes to DB
- [AllowAnonymous]
- [HttpPost("registeragency")]
- public IActionResult RegisterAgency([FromBody]AgencyDto agency)
- {
- // map dto to entity
- _mapper.Map<Agency>(agency);
-
- try
- {
- // save
- _Repo.CreateAgency(agency);
- return Ok();
- }
- catch (Exception ex)
- {
- // return error message if there was an exception
- return BadRequest(new { message = ex.Message });
- }
- }
-
- public class FPTOKEN
- {
- public string token { get; set; }
- }
-
- [AllowAnonymous]
- [HttpPost("fptoken")]
- public IActionResult GetIndiv([FromBody] FPTOKEN fpToken)
- {
- try
- {
- var indiv = _Repo.GetIndividualByFPToken(fpToken.token);
- return new OkObjectResult(indiv);
- }
- catch (Exception ex)
- {
- return new NoContentResult();
- }
- }
-
- [AllowAnonymous]
- [HttpPut("passwordUpdate")]
- public IActionResult UpdateUserPassword([FromBody]UserDto userParam)
- {
- if (userParam != null)
- {
- _Repo.UpdatePassword(userParam);
- return Ok();
- }
- else
- {
- return new NoContentResult();
- }
-
-
- }
- }
- }
|