Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated pw hash#84 #85

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions lib/cli/user.dart
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -30,26 +31,37 @@ 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<void> update(String? username, String? newPass, String oldPass) async {
bool authed = BCrypt.checkpw(oldPass, this.password);
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<void> 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<void> logout() async {
//abhi ke liye no checks
await UserIO.updateDB(User(username, password, false));
}

@override
String toString() =>
'User(username: $username, password: $password, loggedIn: $loggedIn)';
}
2 changes: 1 addition & 1 deletion lib/prompt_chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down