Google Groups

correct way to log a user in programmatically


Eirik Jan 24, 2011 3:10 AM
Posted in group: Lift
Hi all,

I've browsed the "exploring lift" book and forums on how to log a user in programmatically, and all sources suggests using User.logUserIn(user)

However, in my rest-api authentication I can't get this to work. I'm following the example in "Listing 9.15: Performing Basic Authentication" of exploring lift and modified it to print out the currently logged in user. It prints "Empty" (my users are called workers).

 LiftRules.authentication = HttpBasicAuthentication("myApp") {
    case (workerEmail, workerPass, _) => {
      Worker.find(By(Worker.email, workerEmail)).map{
        (w: Worker) =>
          if (w.password.match_?(workerPass)) {
            Worker.logUserIn(w)
            println(Worker.currentUser)
            userRoles(AuthRole("worker"))
            true
          } else false
      }.openOr(false)
    }: Boolean
  }

When the request hits the rest-api, Worker.currentUser is still Empty.

When I do a form-based login (generated from Worker.sitemap, url http://localhost:8080/user_mgt/login) followed by an access to the rest api, something strange occurs:
In the authentication, function above, Worker.currentUser still prints Empty. However, when the request hits the rest-api, Worker.currentUser prints the user I logged in through the form with.

Could someone please explain why currentUser is not set immediately after a call to logUserIn?
Or rather: how do I correctly log in programmatically with basic authentication?

Regards, Eirik