I am trying to check were the users already exists with the same username here is example
Here is my code,
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users");
Query checkUser = reference.orderByChild("userName").equalTo(userEnteredUserName);
but without the uid it works perfect, but whenever there is uid unable to check the username, Is there is anyway to check username
The query is expecting userName to be a direct field in username node and because userId is not same for everyone you cannot specify a neste path. If you remove the username node and restructure the DB as shown below, the same query should work perfectly:
Users
| // remove [username] node from here
|-userId // userId from Firebase Auth
|
|-userName
|-otherFields
If you want to retain existing structure then you can just check if a node with given username exists:
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users").child("userName");
Related
I recently implemented unique username into my app when registering, all good far here, by the way I also need to set it to when the user is editting it's profile.
I tried to do the same thing, but I'm facing an issue here. The app don't let save the profile, because it's checking if the username's taken and, as we're already using one, it won't let us do it.
Ex.: My username is "bob", I changed my profile pic or my display name, so when I click to save, the app will do a username checking in the background and will not let me save it because the username is already taken, but the problem is that it's already my user.
I've tried to set this, but failed:
if (document.equals(setup_username.getText().toString()) || document.isEmpty()){
updateProfile();
Here's my code:
setup_progressbar.setVisibility(View.VISIBLE);
FirebaseFirestore.getInstance().collection("Users").whereEqualTo("username",setup_username.getText().toString()).get().addOnCompleteListener((task) -> {
if (task.isSuccessful()){
List<DocumentSnapshot> document = task.getResult().getDocuments();
if (document.equals(setup_username.getText().toString()) || document.isEmpty()){
updateProfile();
} else {
setup_progressbar.setVisibility(View.INVISIBLE);
setup_username.setError(getString(R.string.username_taken));
return;
}
} else {
setup_progressbar.setVisibility(View.INVISIBLE);
String error = task.getException().getMessage();
FancyToast.makeText(getApplicationContext(), error, FancyToast.LENGTH_SHORT, FancyToast.ERROR, false).show();
}
});
So how to get around this and only forbid it when I try to change my username to another that is taken? Like: "bob" to "bill", but "bill" is already taken, so it won't allow.
You'll need to have some indication in each Users document to indicate which user has claimed that specific name. Given that you store the username inside the document, ownership would typically be established by using the UID of the user as the ID of the document.
Once you have run your query to find the document for the username, you can then check the UID of the owner of that username against the currently signed in user. If the two UIDs are the same, the current user owns the username and is allowed to update the document.
Compare new username with previous username(store it in a variable while displaying user profile data), if both are same don't update it all else check for its uniqueness.
or if you don't have existing username data create relationship with that document and fetch previous username first.
I am currently working on register/login GUI window and l am trying to store passwords and usernames in MySQL DB. I also want to check if a user with such password exists but l don't know how to get just 1 password and 1 user. I want to save from each row the username and the password inside a HashMap (username -> key| password -> value)
This is how my DB looks like.
This is what rows l want to get in the Map
How can l set each username and password into a HashMap?
I only can think of this:
SELECT username, password
FROM table
I can't continue from here...
Your usernames should be unique, so to retrieve one pair of username and its corresponding password, you can limit your query by adding WHERE username= and append whatever username for which you want to retrieve the username/password pair. After that, storing the fields of the result set in a HashMap is pretty trivial.
I am doing automation testing at my work and i'm stuck in a scenario.
My first test consist of creating a new user account which creates a username and a password. P.S. My password is always the same
My second test is basically login as a user with the username and password that I created in the first test.
Also, as the account is created, the database gets updated, and i'm able to connect to the database and execute the query (select user_name from accounts where password = 'pass123' order by created_dttm desc;)
So, since i'm able to execute and get the value from the db, how can I fetch that value and insert it into my username field in order to login as a customer.
Because i'm stuck in this situation, I always have to edit my 2nd test case with a different username that is created in my first tc. I would like my test script to take the username from the db and insert it into the username field automatically.
I'm kinda new to Java and automation, therefore, any kind of help will be highly appreciated. Thanks a ton!
You need to just pass the username value read from database to the sendKeys function of Webdriver. For example, if you have stored the username value from database in String variable user and if you are using id property to recognize the username field, then you can enter that value in username field using following:
driver.findElement(By.id("u")).sendKeys(user);
In the above code, u is the id of username field. You can move your database access code to separate method as shown below:
public static String getUsernameFromDB() throws ClassNotFoundException, SQLException {
//Accessing driver from the JAR file
Class.forName("oracle.jdbc.OracleDriver");
System.out.println("Oracle JDBC driver loaded ok.");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:#151.120.92.150:1602:pppst1","ppp_app","ppp_app_pppst1");
System.out.println("DB Connected Successfuly");
Statement stmt = con.createStatement();
ResultSet result = stmt.executeQuery("select user_name from accounts where password = 'pass123' order by created_dttm desc;");
String account = null;
while(result.next()){
account = result.getString("USER_NAME");
System.out.println("BAID: " + account);
}
con.close();
return account;
}
Your test case code will become:
String username = getUsernameFromDB();
driver.findElement(By.id("u")).sendKeys(username);
I am creating a JFrameApplet (In Java) with a log in (SQLite) but I am struggling to understand how to compare a wanted username, against a username that is already taken:
For Example: I want the username JoeBloggs, but it is taken, how do I compare a wanted username against one already made.
I have an SQLite users.db and the field is USERNAME.
Thank you for any assistance.
Try searching up the UNIQUE constraint.
When creating a table useCREATE TABLE Users(Id INTEGER, USERNAME TEXT UNIQUE);
so if JoeBloggs is already an entry in the db trying to add it again with INSERT INTO Users VALUES(2, 'JoeBloggs'); will give you an Error: column USERNAME is not unique.
I have an authorization window, where you have to input login and password, logins and password are stored in a MS Access Database like this:
ID login password ID_in_another_table
---- ------- ---------- ---------------------
1 admin admin
2 user1 user1 1
3 user2 user2 2
For authorization I'm using
SELECT * FROM Table_name
For administrator I have his own menu, but for user I must open data which has the same ID as ID_in_another_table, can I somehow pass that ID_in_another_table to a function or a class to work with it?
If you use:
SELECT ID_in_another_table
FROM Table_name
WHERE login = :login
AND password = :password
You would only get a result if the name and password match, and for the admin user you would get a single row returned but with a null value. No result would mean the username or password were wrong.