From 109d5f88ea480175044f848eed7d57242b203ea1 Mon Sep 17 00:00:00 2001 From: Blitblank Date: Fri, 20 Mar 2026 21:08:32 -0500 Subject: [PATCH] user dtos as strict object templates --- api/src/Controllers/AuthController.cs | 4 ++-- api/src/Models/DTO.cs | 15 --------------- api/src/Models/User.cs | 2 +- client/src/api/AuthApi.ts | 6 +++--- client/src/models/User.ts | 12 ++++++++++++ client/src/pages/LoginForm.vue | 9 +++++---- client/src/pages/RegisterForm.vue | 9 +++++---- 7 files changed, 28 insertions(+), 29 deletions(-) delete mode 100644 api/src/Models/DTO.cs diff --git a/api/src/Controllers/AuthController.cs b/api/src/Controllers/AuthController.cs index e38f04b..6cb3db8 100644 --- a/api/src/Controllers/AuthController.cs +++ b/api/src/Controllers/AuthController.cs @@ -22,9 +22,9 @@ public class AuthController : ControllerBase { public async Task Register(RegisterDto dto) { var user = new User { Name = dto.Username, - Email = "dummy@goofy.xyz", + Email = dto.Email, PasswordHash = BCrypt.Net.BCrypt.HashPassword(dto.Password), // TODO: secondary hashing stage in client - Role = "admin (FOR NOW !!)", + Role = "user", CreatedAt = DateTime.UtcNow // yeah why not utc }; diff --git a/api/src/Models/DTO.cs b/api/src/Models/DTO.cs deleted file mode 100644 index ea508f6..0000000 --- a/api/src/Models/DTO.cs +++ /dev/null @@ -1,15 +0,0 @@ - -public class RegisterDto { - - public string Username { get; set; } = ""; - public string Email { get; set; } = ""; - public string Password { get; set; } = ""; - -} - -public class LoginDto { - - public string Username { get; set; } = ""; - public string Password { get; set; } = ""; - -} diff --git a/api/src/Models/User.cs b/api/src/Models/User.cs index 2a44e84..ab3618a 100644 --- a/api/src/Models/User.cs +++ b/api/src/Models/User.cs @@ -7,7 +7,7 @@ public class User { public string Name { get; set; } = ""; public string Email { get; set; } = ""; public string PasswordHash { get; set; } = ""; - public string Role { get; set; } + public string Role { get; set; } = ""; public DateTime CreatedAt { get; set; } }; diff --git a/client/src/api/AuthApi.ts b/client/src/api/AuthApi.ts index 8ed5b4c..3fac7ec 100644 --- a/client/src/api/AuthApi.ts +++ b/client/src/api/AuthApi.ts @@ -3,11 +3,11 @@ // handles user registration, user logins, tokens, password reset, etc. import api from "./axios.ts" -import type { User } from "../models/User.ts"; +import type { User, RegisterDto, LoginDto } from "../models/User.ts"; const API_URL: string = "/auth"; -export const register = async (user: { username: string; email: string; password: string }) => { +export const register = async (user: RegisterDto) => { try { const response = await api.post(`${API_URL}/register`, user); @@ -24,7 +24,7 @@ export const register = async (user: { username: string; email: string; password } -export const login = async (user: { username: string; password: string }) => { +export const login = async (user: LoginDto ) => { try { const response = await api.post(`${API_URL}/login`, user); diff --git a/client/src/models/User.ts b/client/src/models/User.ts index a5e3504..6bc0c95 100644 --- a/client/src/models/User.ts +++ b/client/src/models/User.ts @@ -1,5 +1,6 @@ // models are the data objects stored in the database. models defined here must match models defined in api/models +// dtos here must match the the dtos in api/src/Modelts/Dto.cs in name (case insensitive) (types are intermediately serialized to strings) export interface User { id: number; @@ -7,3 +8,14 @@ export interface User { email: string; password: string; } + +export interface RegisterDto { + name: string; + email: string; + password: string; +} + +export interface LoginDto { + name: string; + password: string; +} diff --git a/client/src/pages/LoginForm.vue b/client/src/pages/LoginForm.vue index ce652f3..7f79c15 100644 --- a/client/src/pages/LoginForm.vue +++ b/client/src/pages/LoginForm.vue @@ -4,13 +4,14 @@ import { onMounted, reactive } from "vue"; import { useRoute, useRouter } from "vue-router"; +import type { LoginDto } from "../models/User.ts"; import * as authApi from "../api/AuthApi"; const router = useRouter(); -const user = reactive({ - username: "", - password: "" +const user = reactive({ // the template ensures type consistency + name: "", + password: "", }); onMounted(() => { @@ -37,7 +38,7 @@ async function login(): Promise {

Login

- + diff --git a/client/src/pages/RegisterForm.vue b/client/src/pages/RegisterForm.vue index 179e7fb..97a904e 100644 --- a/client/src/pages/RegisterForm.vue +++ b/client/src/pages/RegisterForm.vue @@ -4,14 +4,15 @@ import { onMounted, reactive } from "vue"; import { useRoute, useRouter } from "vue-router"; +import type { RegisterDto } from "../models/User.ts"; import * as authApi from "../api/AuthApi"; const router = useRouter(); -const user = reactive({ - username: "", +const user = reactive({ // the template ensures type consistency + name: "", email: "", - password: "" + password: "", }); onMounted(() => { @@ -39,7 +40,7 @@ async function register(): Promise {

Register

- +