From 945291fdf33683a543ff7a823de0daf53c545450 Mon Sep 17 00:00:00 2001
From: Gradyn Wursten <me@gradyn.com>
Date: Mon, 30 Oct 2023 10:48:56 -0600
Subject: [PATCH] new user fixes

---
 JournalyApiV2/Controllers/AuthController.cs | 4 ++++
 JournalyApiV2/Services/BLL/AuthService.cs   | 2 ++
 2 files changed, 6 insertions(+)

diff --git a/JournalyApiV2/Controllers/AuthController.cs b/JournalyApiV2/Controllers/AuthController.cs
index 2d89899..c4bb6ee 100644
--- a/JournalyApiV2/Controllers/AuthController.cs
+++ b/JournalyApiV2/Controllers/AuthController.cs
@@ -27,6 +27,10 @@ public async Task<IActionResult> CreateUser([FromBody] NewUserRequest request)
         {
             await _authService.CreateUser(request.Email, request.Password, request.FirstName, request.LastName);
         }
+        catch (ArgumentException ex)
+        {
+            return StatusCode(409, ex.Message);
+        }
         catch (Exception ex)
         {
             return StatusCode(500, ex.Message);
diff --git a/JournalyApiV2/Services/BLL/AuthService.cs b/JournalyApiV2/Services/BLL/AuthService.cs
index 141bab8..1c024d0 100644
--- a/JournalyApiV2/Services/BLL/AuthService.cs
+++ b/JournalyApiV2/Services/BLL/AuthService.cs
@@ -83,6 +83,7 @@ public async Task<AuthenticationResponse> SignIn(string email, string password)
     
     public async Task CreateUser(string email, string password, string firstName, string lastName)
     {
+        if (await _userManager.FindByEmailAsync(email) != null) throw new ArgumentException("Email already in use");
         var result = await _userManager.CreateAsync(new JournalyUser
         {
             FirstName = firstName,
@@ -145,6 +146,7 @@ public async Task<AuthenticationResponse> ChangeEmail(string email, Guid userId,
         var user = await _userManager.FindByIdAsync(userId.ToString());
         if (user == null) throw new ArgumentException("User not found");
         user.Email = email;
+        user.UserName = email;
         await _userManager.UpdateAsync(user);
         
         // Generate new JWT and associated refresh token with the name updated