update profile works

This commit is contained in:
LilleBRG 2024-08-27 13:06:31 +02:00
parent 384f25ddbe
commit 95a6d3d9f1
7 changed files with 47 additions and 29 deletions

View File

@ -14,30 +14,39 @@ namespace API.Application.Users.Commands
_repository = repository; _repository = repository;
} }
public async Task<IActionResult> Handle(UpdateUserDTO UpdateUserDTO) public async Task<IActionResult> Handle(UpdateUserDTO updateUserDTO)
{ {
List<User> existingUsers = await _repository.QueryAllUsersAsync(); List<User> existingUsers = await _repository.QueryAllUsersAsync();
User currentUser = await _repository.QueryUserByIdAsync(UpdateUserDTO.Id); User currentUser = await _repository.QueryUserByIdAsync(updateUserDTO.Id);
foreach (User existingUser in existingUsers) 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." }); 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." }); return new ConflictObjectResult(new { message = "Email is already in use." });
} }
} }
if (updateUserDTO.Password != "")
string hashedPassword = BCrypt.Net.BCrypt.HashPassword(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); bool success = await _repository.UpdateUserAsync(currentUser);
if (success) if (success)

View File

@ -1,9 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:mobile/models.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'dart:convert'; import 'dart:convert';
import 'base/variables.dart';
enum ApiService { enum ApiService {
auth, auth,

View File

@ -89,6 +89,8 @@ class _SideMenuState extends State<SideMenu> {
Navigator.pushReplacementNamed(context, '/home'); Navigator.pushReplacementNamed(context, '/home');
}, },
), ),
...(_isLoggedIn
? [
ListTile( ListTile(
title: const Text('Favorites'), title: const Text('Favorites'),
leading: const Icon(Icons.star), leading: const Icon(Icons.star),
@ -110,8 +112,7 @@ class _SideMenuState extends State<SideMenu> {
thickness: 2, thickness: 2,
indent: 40, indent: 40,
), ),
...(_isLoggedIn
? [
ListTile( ListTile(
title: const Text('Log out'), title: const Text('Log out'),
leading: const Icon(Icons.logout), leading: const Icon(Icons.logout),

View File

@ -1,5 +1,3 @@
import 'dart:math';
import 'dart:developer' as useMAN;
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:mobile/models.dart'; import 'package:mobile/models.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
@ -22,6 +20,8 @@ class _ProfilePageState extends State<EditProfilePage> {
TextEditingController passwordInput = TextEditingController(); TextEditingController passwordInput = TextEditingController();
TextEditingController confirmPasswordInput = TextEditingController(); TextEditingController confirmPasswordInput = TextEditingController();
set userData(User userData) {}
@override @override
void initState() { void initState() {
@ -66,17 +66,20 @@ class _ProfilePageState extends State<EditProfilePage> {
return; return;
} }
useMAN.log('data');
if (response != null) { if (response != null) {
user = User( User updatedUser = User(
id!, id!,
emailInput.text, emailInput.text,
usernameInput.text, usernameInput.text,
DateTime.now(), DateTime.now(),
); );
setState(() {
user = updatedUser;
});
Navigator.of(context).pop(); // Close the dialog Navigator.of(context).pop(); // Close the dialog
Navigator.pushReplacementNamed(context, '/profile');
} else { } else {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Something went wrong! Please contact an admin.')), const SnackBar(content: Text('Something went wrong! Please contact an admin.')),

View File

@ -1,11 +1,11 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:mobile/base/sidemenu.dart'; import 'package:mobile/base/sidemenu.dart';
import 'package:mobile/base/variables.dart';
import 'package:mobile/models.dart'; import 'package:mobile/models.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:google_fonts/google_fonts.dart'; import 'package:google_fonts/google_fonts.dart';
import 'dart:convert'; import 'dart:convert';
import 'api.dart' as api; import 'api.dart' as api;
import 'base/variables.dart';
class LoginPage extends StatefulWidget { class LoginPage extends StatefulWidget {
const LoginPage({super.key}); const LoginPage({super.key});
@ -40,6 +40,14 @@ class _LoginPageState extends State<LoginPage> {
} }
} }
@override
void initState() {
super.initState();
setState(() {
user = null;
});
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return SideMenu( return SideMenu(

View File

@ -1,5 +1,4 @@
import 'dart:convert'; import 'dart:convert';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:mobile/base/variables.dart'; import 'package:mobile/base/variables.dart';
import 'package:mobile/models.dart'; import 'package:mobile/models.dart';
@ -25,15 +24,6 @@ class _ProfilePageState extends State<ProfilePage> {
} }
Future<void> getProfile() async { Future<void> 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) { if (user != null) {
setState(() { setState(() {
userData = user!; userData = user!;

View File

@ -1,6 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:mobile/base/sidemenu.dart'; import 'package:mobile/base/sidemenu.dart';
import 'package:google_fonts/google_fonts.dart'; import 'package:google_fonts/google_fonts.dart';
import 'package:mobile/base/variables.dart';
import 'api.dart' as api; import 'api.dart' as api;
class RegisterPage extends StatefulWidget { class RegisterPage extends StatefulWidget {
@ -36,6 +37,14 @@ class _RegisterPageState extends State<RegisterPage> {
} }
} }
@override
void initState() {
super.initState();
setState(() {
user = null;
});
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return SideMenu( return SideMenu(