0

I have a grails application in which Im using db.authenticate for a login page but I understand this method has been deprecated and therefore I would like to upgrade my application to using the MongoCredential object for authentication. However, unlike the db.authenticate which nicely returns a boolean to get authentication done, the MongoCredential doesn't return a boolean so how can I go about accomplishing the code replacement with minimal headache. Ideally, I'd like to derive some kind of a boolean to tell me if authentication was achieved. Thanks for your patience. Im a newbie with Mongodb.

This is part of the code I need to replace which currently makes use of the deprecated method "authenticate":

MongoClient mongoClient = new MongoClient("localhost", 27017)
            DB db = mongoClient.getDB("twcdb");
            def userName = params.username
            def passWord = params.password
//deprecated method being used in the line below.
            boolean auth = db.authenticate(userName, passWord.toCharArray())                

            if (auth) {
                userloggedin = params.username
                render(contentType: 'text/json') {
                    [success: true, url: createLink(controller: 'admin', action: 'loggedin')]
                }
            }
            else {
                render(contentType: 'text/json') { 
                    ["success": false, "message": 'Login or Password is incorrect.']
                }

Edit: I know that the answer must lie in testing a property of the MongoClient object somehow to see if it contains a valid authenticated connection but I am still stuck on how to do this. Given I knowingly feed the MongoClient constructor with a bogus MongoCredential, it still creates an object that isn't null. I was betting on the null test but no joy. So, how do I replace the deprecated db.authenticate?

Anonymous Human
  • 1,858
  • 1
  • 21
  • 47
  • It would be nice to see what code you have to replace. Have you seen this [**question**](http://stackoverflow.com/questions/21859579/authentication-during-connection-to-mongodb-server-instance-using-java). – Swapnil Sawant May 19 '14 at 06:22
  • @Swapnilsawant, I have edit my original post to include the code to be replaced. Thanks for your attention to my question. I will see the other post you linked to. Thanks again. – Anonymous Human May 20 '14 at 00:50

0 Answers0