I want to create an authentication route for my Lift Application.
www.myapp.com/user/login
Users.login(email, password)
method to validate the credentials.Q:
How do I tell Lift to authenticate the credentials incoming via /user/login
?
This is overly simplistic, but something like this will allow you to create a url that you can post to. The JSON extraction is not very safe, but should give you an idea of how this might work.
LiftRules.dispatch.append(new RestHelper{
serve {
case JsonPost("user" :: "login" :: Nil, (json, _)) =>
//extract JSON from json object to get username and password
val userEmail:String = (json \ "username").extract[String]
val password = (json \ "password").extract[String]
User.login(userEmail, password) match {
case Full(r) =>
User.current(true)
InMemoryResponse(Array(), Nil, Nil, 200)
case _ => ForbiddenResponse
}
}
})
object User {
object loggedIn extends SessionVar[Boolean](false)
}
Then you can use if(User.loggedIn.get){ ... }
to test if the user is logged in anywhere. This will work for anything added to the stateful dispatch, if you use LiftRules.statelessDispatch
the session will not exist.