Add user registration, add password hashing, add touchcode generation

This commit is contained in:
Alexandertp 2023-12-06 14:44:14 +01:00
parent c55a5c1b36
commit 3bc3ca256c

View File

@ -1,6 +1,8 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using backend.Application; using backend.Application;
using backend.Models; using backend.Models;
using System.Text.Json.Nodes;
using Microsoft.AspNetCore.Identity;
namespace backend.Controllers; namespace backend.Controllers;
@ -8,9 +10,31 @@ namespace backend.Controllers;
public class UserController : ControllerBase public class UserController : ControllerBase
{ {
[HttpPost("Register")] [HttpPost("Register")]
public void Register() public IActionResult Register([FromBody] JsonObject input)
{ {
ApplicationState.DbContext!.Add(new User { Username = "test", Password = "test", TouchCode = "1234" }); if (input["username"] == null || input["password"] == null) {
return BadRequest("Username and password required");
}
var passwordHasher = new PasswordHasher<object>();
string hashedPassword = passwordHasher.HashPassword(null, input["password"]!.ToString());
string touchCode = "";
for (int i = 0; i < 4; i++) {
touchCode += (1 + new Random().Next() % 5).ToString();
}
var user = new User {
Username = input["username"]!.ToString(),
Password = hashedPassword,
TouchCode = touchCode,
};
ApplicationState.DbContext!.Add(user);
ApplicationState.DbContext!.SaveChanges(); ApplicationState.DbContext!.SaveChanges();
Console.WriteLine("Created user: " + user.Username);
return Ok();
} }
} }