From 3bc3ca256c65fbb3b27859e13bf03fc1bea27244 Mon Sep 17 00:00:00 2001 From: Alexandertp Date: Wed, 6 Dec 2023 14:44:14 +0100 Subject: [PATCH] Add user registration, add password hashing, add touchcode generation --- backend/Controllers/UserController.cs | 28 +++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/backend/Controllers/UserController.cs b/backend/Controllers/UserController.cs index c7306b1..7a62422 100644 --- a/backend/Controllers/UserController.cs +++ b/backend/Controllers/UserController.cs @@ -1,6 +1,8 @@ using Microsoft.AspNetCore.Mvc; using backend.Application; using backend.Models; +using System.Text.Json.Nodes; +using Microsoft.AspNetCore.Identity; namespace backend.Controllers; @@ -8,9 +10,31 @@ namespace backend.Controllers; public class UserController : ControllerBase { [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(); + 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(); + + Console.WriteLine("Created user: " + user.Username); + + return Ok(); } }