TourIAm/TIAMWebApp/Server/Controllers/UserAPIController.cs

131 lines
3.8 KiB
C#

using DevExpress.Office.Crypto;
using DevExpress.Xpo.DB;
using DevExpress.XtraPrinting;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System.Reflection.Metadata;
using System.Text.Json;
using TIAMWebApp.Shared.Application.Models;
using TIAMWebApp.Shared.Application.Models.PageModels;
namespace TIAMWebApp.Server.Controllers
{
[ApiController]
[Route("[controller]")]
public class UserAPIController : ControllerBase
{
PasswordHasher hasher = new PasswordHasher();
private User[] users = new User[]
{
new User("test@tiam.hu", "+36701234567", "asd123")
};
private readonly ILogger<SupplierAPIController> _logger;
public UserAPIController(ILogger<SupplierAPIController> logger)
{
_logger = logger;
}
[HttpPost]
[Route("Auth")]
public async Task<IActionResult> AuthenticateUser([FromBody] JsonElement SerializedLoginModel)
{
Console.WriteLine("Auth called");
Console.WriteLine(SerializedLoginModel.GetRawText());
if (string.IsNullOrEmpty(SerializedLoginModel.GetRawText()))
{
return BadRequest("SerializedLoginModel is required");
}
else
{
var user = JObject.Parse(SerializedLoginModel.GetRawText()).ToObject<LoginModel>();
Console.WriteLine(user.Email);
Console.WriteLine(user.Password);
if (user.Email == "test@tiam.hu" && user.Password == "asd123")
{
Console.WriteLine("User authenticated");
return Ok("yes");
}
else
{
Console.WriteLine("User NOT authenticated");
return Ok("no");
}
}
}
[HttpPost]
[Route("CreateUser")]
public async Task<IActionResult> CreateUser([FromBody] JsonElement SerializedRegistrationModel)
{
if (string.IsNullOrEmpty(SerializedRegistrationModel.GetRawText()))
{
return BadRequest("SerializedLoginModel is required");
}
else
{
var user = JObject.Parse(SerializedRegistrationModel.GetRawText()).ToObject<RegistrationModel>();
if (users != null)
{
//add user to users array
Array.Resize(ref users, users.Length + 1);
users[users.Length - 1] = new User(user.Email, user.PhoneNumber, user.Password);
return Ok("yes");
}
else
{
return Ok("no");
}
}
}
[HttpPost]
[Route("Test1")]
public async Task<IActionResult> TestEndpoint([FromBody] int testParam)
{
return Ok(testParam.ToString());
}
[HttpGet]
[Route("Test2")]
public string TestEndpoint2(int testParam)
{
return testParam.ToString();
}
[HttpGet]
[Route("GetUsers")]
public IEnumerable<Supplier> GetUsers()
{
throw new NotImplementedException();
}
private bool VerifyPassword(string password, string hashedPassword)
{
bool isPasswordValid = hasher.VerifyPassword(password, hashedPassword);
return isPasswordValid;
}
private string HashPassword(string password)
{
var hashedPassword = hasher.HashPassword(password);
return hashedPassword;
}
}
}