diff --git a/API/Application/Users/Commands/UpdateUser.cs b/API/Application/Users/Commands/UpdateUser.cs index c530041..16ec259 100644 --- a/API/Application/Users/Commands/UpdateUser.cs +++ b/API/Application/Users/Commands/UpdateUser.cs @@ -14,30 +14,39 @@ namespace API.Application.Users.Commands _repository = repository; } - public async Task Handle(UpdateUserDTO UpdateUserDTO) + public async Task Handle(UpdateUserDTO updateUserDTO) { List existingUsers = await _repository.QueryAllUsersAsync(); - User currentUser = await _repository.QueryUserByIdAsync(UpdateUserDTO.Id); + User currentUser = await _repository.QueryUserByIdAsync(updateUserDTO.Id); foreach (User existingUser in existingUsers) { - if (existingUser.Username == UpdateUserDTO.Username && existingUser.Username != currentUser.Username) + if (existingUser.Username == updateUserDTO.Username && existingUser.Username != currentUser.Username) { return new ConflictObjectResult(new { message = "Username is already in use." }); } - if (existingUser.Email == UpdateUserDTO.Email && existingUser.Email != currentUser.Email) + if (existingUser.Email == updateUserDTO.Email && existingUser.Email != currentUser.Email) { return new ConflictObjectResult(new { message = "Email is already in use." }); } } - - string hashedPassword = BCrypt.Net.BCrypt.HashPassword(UpdateUserDTO.Password); + if (updateUserDTO.Password != "") + { + if (IsPasswordSecure(updateUserDTO.Password)) + { + string hashedPassword = BCrypt.Net.BCrypt.HashPassword(updateUserDTO.Password); + currentUser.HashedPassword = hashedPassword; + } + } + if (updateUserDTO.Username != "") + currentUser.Username = updateUserDTO.Username; + if (updateUserDTO.Email != "") + currentUser.Email = updateUserDTO.Email; + + - currentUser.Username = UpdateUserDTO.Username; - currentUser.Email = UpdateUserDTO.Email; - currentUser.HashedPassword = hashedPassword; bool success = await _repository.UpdateUserAsync(currentUser); if (success) diff --git a/Mobile/lib/api.dart b/Mobile/lib/api.dart index 23d65ac..496e157 100644 --- a/Mobile/lib/api.dart +++ b/Mobile/lib/api.dart @@ -1,9 +1,7 @@ import 'package:flutter/material.dart'; -import 'package:mobile/models.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:http/http.dart' as http; import 'dart:convert'; -import 'base/variables.dart'; enum ApiService { auth, diff --git a/Mobile/lib/base/sidemenu.dart b/Mobile/lib/base/sidemenu.dart index 089b470..411db7c 100644 --- a/Mobile/lib/base/sidemenu.dart +++ b/Mobile/lib/base/sidemenu.dart @@ -89,6 +89,8 @@ class _SideMenuState extends State { Navigator.pushReplacementNamed(context, '/home'); }, ), + ...(_isLoggedIn + ? [ ListTile( title: const Text('Favorites'), leading: const Icon(Icons.star), @@ -110,8 +112,7 @@ class _SideMenuState extends State { thickness: 2, indent: 40, ), - ...(_isLoggedIn - ? [ + ListTile( title: const Text('Log out'), leading: const Icon(Icons.logout), diff --git a/Mobile/lib/editprofile.dart b/Mobile/lib/editprofile.dart index ec66acf..0db8d2b 100644 --- a/Mobile/lib/editprofile.dart +++ b/Mobile/lib/editprofile.dart @@ -1,5 +1,3 @@ -import 'dart:math'; -import 'dart:developer' as useMAN; import 'package:flutter/material.dart'; import 'package:mobile/models.dart'; import 'package:shared_preferences/shared_preferences.dart'; @@ -21,6 +19,8 @@ class _ProfilePageState extends State { TextEditingController emailInput = TextEditingController(); TextEditingController passwordInput = TextEditingController(); TextEditingController confirmPasswordInput = TextEditingController(); + + set userData(User userData) {} @override @@ -66,17 +66,20 @@ class _ProfilePageState extends State { return; } - useMAN.log('data'); - if (response != null) { - user = User( + User updatedUser = User( id!, emailInput.text, usernameInput.text, DateTime.now(), ); + setState(() { + user = updatedUser; + }); Navigator.of(context).pop(); // Close the dialog + Navigator.pushReplacementNamed(context, '/profile'); + } else { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Something went wrong! Please contact an admin.')), diff --git a/Mobile/lib/login.dart b/Mobile/lib/login.dart index dcf17d4..799e94a 100644 --- a/Mobile/lib/login.dart +++ b/Mobile/lib/login.dart @@ -1,11 +1,11 @@ import 'package:flutter/material.dart'; import 'package:mobile/base/sidemenu.dart'; -import 'package:mobile/base/variables.dart'; import 'package:mobile/models.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:google_fonts/google_fonts.dart'; import 'dart:convert'; import 'api.dart' as api; +import 'base/variables.dart'; class LoginPage extends StatefulWidget { const LoginPage({super.key}); @@ -40,6 +40,14 @@ class _LoginPageState extends State { } } + @override + void initState() { + super.initState(); + setState(() { + user = null; + }); + } + @override Widget build(BuildContext context) { return SideMenu( diff --git a/Mobile/lib/profile.dart b/Mobile/lib/profile.dart index 3f97794..d52c41e 100644 --- a/Mobile/lib/profile.dart +++ b/Mobile/lib/profile.dart @@ -1,5 +1,4 @@ import 'dart:convert'; - import 'package:flutter/material.dart'; import 'package:mobile/base/variables.dart'; import 'package:mobile/models.dart'; @@ -25,15 +24,6 @@ class _ProfilePageState extends State { } Future getProfile() async { - if (!await api.isLoggedIn(context)) { - WidgetsBinding.instance.addPostFrameCallback((_) { - ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Please log in to view this page'))); - Navigator.pushReplacementNamed(context, '/login'); - }); - - return; - } - if (user != null) { setState(() { userData = user!; diff --git a/Mobile/lib/register.dart b/Mobile/lib/register.dart index 6eb4036..91d81ec 100644 --- a/Mobile/lib/register.dart +++ b/Mobile/lib/register.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:mobile/base/sidemenu.dart'; import 'package:google_fonts/google_fonts.dart'; +import 'package:mobile/base/variables.dart'; import 'api.dart' as api; class RegisterPage extends StatefulWidget { @@ -36,6 +37,14 @@ class _RegisterPageState extends State { } } + @override + void initState() { + super.initState(); + setState(() { + user = null; + }); + } + @override Widget build(BuildContext context) { return SideMenu(