diff --git a/lib/cli/user.dart b/lib/cli/user.dart index aa951ad..b287c95 100644 --- a/lib/cli/user.dart +++ b/lib/cli/user.dart @@ -1,6 +1,7 @@ -import 'package:prompt_chat/db/database_crud.dart'; import 'package:bcrypt/bcrypt.dart'; +import 'package:prompt_chat/db/database_crud.dart'; + class User { late String username; late String password; @@ -30,7 +31,7 @@ class User { throw Exception("Error : Incorrect password"); } loggedIn = true; - await UserIO.updateDB(User(username, password, true)); + await UserIO.updateDB(User(username, this.password, true)); } Future update(String? username, String? newPass, String oldPass) async { @@ -38,18 +39,29 @@ class User { if (!(authed)) { throw Exception("Error : Incorrect password"); } - await UserIO.updateDB( - User(username ?? this.username, newPass ?? password, true)); + if (newPass != null) { + this.password = newPass; + hashPassword(); + } + await UserIO.updateDB(User(username ?? this.username, this.password, true)); } Future register() async { + hashPassword(); + await DatabaseIO.addToDB(this, "users"); + } + + void hashPassword() { var salt = BCrypt.gensalt(); password = BCrypt.hashpw(password, salt); - await DatabaseIO.addToDB(this, "users"); } Future logout() async { //abhi ke liye no checks await UserIO.updateDB(User(username, password, false)); } + + @override + String toString() => + 'User(username: $username, password: $password, loggedIn: $loggedIn)'; } diff --git a/lib/prompt_chat.dart b/lib/prompt_chat.dart index 3002b48..4df9dd3 100644 --- a/lib/prompt_chat.dart +++ b/lib/prompt_chat.dart @@ -39,8 +39,8 @@ class ChatAPI { validUsername(username); var newUser = User(username, password, false); - users.add(newUser); await newUser.register(); + users.add(newUser); } void validUsername(String username) {