-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
44 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,10 +6,13 @@ | |
|
||
import static com.mongodb.client.model.Filters.*; | ||
|
||
import java.util.prefs.Preferences; | ||
|
||
public class Login { | ||
private static String userID; | ||
|
||
public static boolean loginAccount(String username, String password) { | ||
|
||
public static boolean loginAccount(String username, String password, boolean autoLogin) { | ||
String uri = "mongodb+srv://aditijain:[email protected]/?retryWrites=true&w=majority"; | ||
MongoClient mongoClient = MongoClients.create(uri); | ||
MongoDatabase PantryPalDB = mongoClient.getDatabase("PantryPal"); | ||
|
@@ -23,6 +26,11 @@ public static boolean loginAccount(String username, String password) { | |
System.out.println("Success"); | ||
userID = user.getObjectId("_id").toString(); | ||
System.out.println("User ID: " + userID); | ||
|
||
if (autoLogin){ | ||
storeCredentials(username, password); | ||
} | ||
|
||
return true; | ||
} | ||
return false; | ||
|
@@ -31,4 +39,24 @@ public static boolean loginAccount(String username, String password) { | |
public static String getID() { | ||
return userID; | ||
} | ||
|
||
|
||
private static void storeCredentials(String username, String password) { | ||
// Use Preferences API to securely store credentials | ||
Preferences prefs = Preferences.userNodeForPackage(Login.class); | ||
prefs.put("username", username); | ||
prefs.put("password", password); | ||
} | ||
|
||
public static boolean attemptAutoLogin() { | ||
// Attempt automatic login using stored credentials | ||
Preferences prefs = Preferences.userNodeForPackage(Login.class); | ||
String storedUsername = prefs.get("username", null); | ||
String storedPassword = prefs.get("password", null); | ||
|
||
boolean autoLogin = storedUsername != null && storedPassword != null; | ||
|
||
return loginAccount(storedUsername, storedPassword, autoLogin); | ||
} | ||
|
||
} |