Skip to content

Instantly share code, notes, and snippets.

@Hajto
Created September 12, 2015 16:25
Show Gist options
  • Save Hajto/01cbb8ce6aa0ca6987b3 to your computer and use it in GitHub Desktop.
Save Hajto/01cbb8ce6aa0ca6987b3 to your computer and use it in GitHub Desktop.
Authirization thing
def authorized(block : (User) => Future[Result] )(implicit req : Request[_]) : Future[Result] = {
req.session.get(USER_CACHE).map { session =>
val user = Cache.getAs[User](USER_CACHE)
if (user.isEmpty) {
Database.users.find(Json.obj(USER_CACHE -> session)).one[User].flatMap[Result] { r => r.map { res =>
val u = res.copy(password = "****")
Cache.set(USER_CACHE,u, 1.hour)
block(u)
}.get
} fallbackTo Future.successful(InternalServerError)
} else {
Cache.set(USER_CACHE, user, 1.hour)
block(user.get)
}
}
} getOrElse Future.successful(Unauthorized)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment