authenticationibm-mobilefirstmobilefirst-adapters

MobileFirst User authentication security check with database


I am following this tutorial:

http://mobilefirstplatform.ibmcloud.com/tutorials/en/foundation/8.0/authentication-and-security/user-authentication/security-check/

There they check the credentials, that they have received from the user's input in the app:

@Override
protected boolean validateCredentials(Map<String, Object> credentials) {
    if(credentials!=null && credentials.containsKey("username") && credentials.containsKey("password")){
        String username = credentials.get("username").toString();
        String password = credentials.get("password").toString();
        if(!username.isEmpty() && !password.isEmpty() && username.equals(password)) {
            return true;
        }
    }
    return false;
}

As you can see, the authentication returns true when usernameand password are equal. I have a mysqldatabse, where I have saved the registered users. So I want to check the entered credentials against the data in my database. How do I have to change the adapter and the method so I can do this?


Solution

  • To achieve this , you will need to write the code to connect to your DB, invoke the SQL query to check the data in DB.

    Within your security check's validateCredentials method, you should write the code to connect to your DB that holds the registered user information. Check for the user details against the DB and based on the outcome , return true of false.

    A sample Java SQL adapter is listed here. You can use it for your reference.