本文整理汇总了Scala中jp.t2v.lab.play2.auth.LoginLogout类的典型用法代码示例。如果您正苦于以下问题:Scala LoginLogout类的具体用法?Scala LoginLogout怎么用?Scala LoginLogout使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了LoginLogout类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: LoginData
//设置package包名称以及导入依赖的类
package controllers.landing
import controllers.{ HasConfig, HasUserService, Security }
import javax.inject.{ Inject, Singleton }
import jp.t2v.lab.play2.auth.{ AuthElement, LoginLogout }
import models.document.DocumentService
import models.user.UserService
import play.api.Configuration
import play.api.data.Form
import play.api.data.Forms._
import play.api.i18n.{ I18nSupport, MessagesApi }
import play.api.mvc.{ Action, Controller }
import scala.concurrent.{ ExecutionContext, Future }
case class LoginData(usernameOrPassword: String, password: String)
@Singleton
class LoginLogoutController @Inject() (
val config: Configuration,
val users: UserService,
implicit val ctx: ExecutionContext,
val messagesApi: MessagesApi
) extends Controller with AuthElement with HasConfig with HasUserService with Security with LoginLogout with I18nSupport {
private val MESSAGE = "message"
private val INVALID_LOGIN = "Invalid Username or Password"
val loginForm = Form(
mapping(
"username" -> nonEmptyText,
"password" -> nonEmptyText
)(LoginData.apply)(LoginData.unapply)
)
def showLoginForm(destination: Option[String]) = Action { implicit request =>
destination match {
case None => Ok(views.html.landing.login(loginForm))
case Some(dest) => Ok(views.html.landing.login(loginForm)).withSession("access_uri" -> dest)
}
}
def processLogin = Action.async { implicit request =>
loginForm.bindFromRequest.fold(
formWithErrors =>
Future(BadRequest(views.html.landing.login(formWithErrors))),
loginData =>
users.validateUser(loginData.usernameOrPassword, loginData.password).flatMap {
case Some(validUser) => gotoLoginSucceeded(validUser.getUsername)
case None => Future(Redirect(routes.LoginLogoutController.showLoginForm()).flashing(MESSAGE -> INVALID_LOGIN))
}
)
}
def logout = Action.async { implicit request =>
gotoLogoutSucceeded
}
}
示例2: Sessions
//设置package包名称以及导入依赖的类
package controllers
import authes.AuthConfigImpl
import com.github.tototoshi.play2.json4s.Json4s
import com.google.inject.Inject
import jp.t2v.lab.play2.auth.LoginLogout
import org.json4s.DefaultFormats
import play.api.mvc.{Action, Controller}
import queries.LoginEmail
import scalikejdbc.AutoSession
import scala.concurrent.ExecutionContext
class Sessions @Inject() (json4s: Json4s, implicit val ec: ExecutionContext)
extends Controller
with AuthConfigImpl
with LoginLogout {
import json4s._
implicit val formats = DefaultFormats
def login() = Action.async(json) { implicit req =>
val result = for {
login <- req.body.extractOpt[LoginEmail]
account <- login.authenticate()(AutoSession)
} yield account
result.fold(authenticationFailed(req)) { account => gotoLoginSucceeded(account.id) }
}
def logout() = Action.async { implicit req =>
gotoLogoutSucceeded
}
}
示例3: AuthenticateController
//设置package包名称以及导入依赖的类
package controllers.api.auth
import javax.inject.{Inject, Singleton}
import akka.actor.ActorSystem
import com.github.sample.dto.AccountDTO
import jp.t2v.lab.play2.auth.LoginLogout
import play.api.libs.json.JsValue
import play.api.mvc._
import scala.concurrent.{ExecutionContext, Future}
@Singleton
class AuthenticateController @Inject() (actorSystem: ActorSystem) extends Controller with LoginLogout with AuthConfigImpl {
implicit val myExecutionContext: ExecutionContext = actorSystem.dispatcher
private def authenticate(dto: AccountDTO): Either[Result, MyUser] = {
AuthService.authenticate(dto.id, dto.password) match {
case None => Left(Unauthorized("authentication failed"))
case Some(user) => Right(user)
}
}
private def jsonDecode(request: Request[JsValue]): Either[Result, AccountDTO] = {
request.body.validate[AccountDTO].asOpt match {
case None => Left(BadRequest("bad request"))
case Some(v) => Right(v)
}
}
def login() = Action.async(BodyParsers.parse.json) { implicit request =>
val myUser = for{
account <- jsonDecode(request).right
myUser <- authenticate(account).right
} yield myUser
myUser match {
case Left(r) => Future{r}
case Right(user) => gotoLoginSucceeded(user.id).map(_.withCookies(Cookie("userId", user.id), Cookie("userRole", user.role.toString)))
}
}
def logout() = Action.async { implicit request =>
gotoLogoutSucceeded
}
}
示例4: Application
//设置package包名称以及导入依赖的类
package controllers
import jp.t2v.lab.play2.auth.LoginLogout
import models.Account
import play.api._
import play.api.data.Form
import play.api.mvc._
import views.html
import play.api.data.Form
import play.api.data.Forms._
import scala.concurrent.Future
import play.api.libs.concurrent.Execution.Implicits.defaultContext
class Application extends Controller with LoginLogout with AuthConfigImpl {
def index = Action {
Ok(views.html.index("Your new application is ready."))
}
def authenticate = Action.async { implicit request =>
loginForm.bindFromRequest.fold(
formWithErrors => Future.successful(BadRequest(html.login(formWithErrors))),
user => gotoLoginSucceeded(user.get.id)
)
}
}
示例5: SessionController
//设置package包名称以及导入依赖的类
package controllers
import authes.AuthConfigImpl
import com.github.tototoshi.play2.json4s.native.Json4s
import com.google.inject.Inject
import com.ponkotuy.queries.LoginEmail
import jp.t2v.lab.play2.auth.LoginLogout
import models.User
import org.json4s.DefaultFormats
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
import play.api.mvc.{Action, Controller}
import scalikejdbc._
import scala.concurrent.ExecutionContext
class SessionController @Inject()(_ec: ExecutionContext, json4s: Json4s)
extends Controller
with AuthConfigImpl
with LoginLogout {
import json4s._
import SessionController._
implicit val ec = _ec
implicit def formats = DefaultFormats
def login() = Action.async(json) { implicit req =>
val result = for {
auth <- req.body.extractOpt[LoginEmail]
user <- authenticate(auth)(AutoSession)
} yield user
result.fold(authenticationFailed(req)) { user => gotoLoginSucceeded(user.id) }
}
def logout() = Action.async { implicit req =>
gotoLogoutSucceeded
}
}
object SessionController {
val bcrypt = new BCryptPasswordEncoder(10)
def authenticate(le: LoginEmail)(implicit session: DBSession): Option[User] = {
User.findBy(sqls.eq(User.column.email, le.email)).filter { user =>
bcrypt.matches(le.password, user.password)
}
}
}
示例6: Sessions
//设置package包名称以及导入依赖的类
package controllers.rememberme
import jp.t2v.lab.play2.auth.LoginLogout
import model.Account
import play.api.data.Form
import play.api.data.Forms._
import play.api.mvc.{Action, Controller}
import views.html
import scala.language.postfixOps
import scala.concurrent.Future
import play.api.libs.concurrent.Execution.Implicits.defaultContext
class Sessions extends Controller with LoginLogout with AuthConfigImpl {
val loginForm = Form {
mapping("email" -> email, "password" -> text)(Account.authenticate)(_.map(u => (u.email, "")))
.verifying("Invalid email or password", result => result.isDefined)
}
val remembermeForm = Form {
"rememberme" -> boolean
}
def login = Action { implicit request =>
Ok(html.rememberme.login(loginForm, remembermeForm.fill(request.session.get("rememberme").exists("true" ==))))
}
def logout = Action.async { implicit request =>
gotoLogoutSucceeded.map(_.flashing(
"success" -> "You've been logged out"
))
}
def authenticate = Action.async { implicit request =>
val rememberme = remembermeForm.bindFromRequest()
loginForm.bindFromRequest.fold(
formWithErrors => Future.successful(BadRequest(html.rememberme.login(formWithErrors, rememberme))),
{ user =>
val req = request.copy(tags = request.tags + ("rememberme" -> rememberme.get.toString))
gotoLoginSucceeded(user.get.id)(req, defaultContext).map(_.withSession("rememberme" -> rememberme.get.toString))
}
)
}
}
示例7: Sessions
//设置package包名称以及导入依赖的类
package controllers.csrf
import jp.t2v.lab.play2.auth.LoginLogout
import model.Account
import play.api.data.Form
import play.api.data.Forms._
import play.api.mvc.{Action, Controller}
import views.html
import scala.concurrent.Future
import play.api.libs.concurrent.Execution.Implicits.defaultContext
class Sessions extends Controller with LoginLogout with AuthConfigImpl {
val loginForm = Form {
mapping("email" -> email, "password" -> text)(Account.authenticate)(_.map(u => (u.email, "")))
.verifying("Invalid email or password", result => result.isDefined)
}
def login = Action { implicit request =>
Ok(html.csrf.login(loginForm))
}
def logout = Action.async { implicit request =>
gotoLogoutSucceeded.map(_.flashing(
"success" -> "You've been logged out"
))
}
def authenticate = Action.async { implicit request =>
loginForm.bindFromRequest.fold(
formWithErrors => Future.successful(BadRequest(html.csrf.login(formWithErrors))),
user => gotoLoginSucceeded(user.get.id)
)
}
}
示例8: Sessions
//设置package包名称以及导入依赖的类
package controllers.stateless
import jp.t2v.lab.play2.auth.LoginLogout
import model.Account
import play.api.data.Form
import play.api.data.Forms._
import play.api.mvc.{Action, Controller}
import views.html
import scala.concurrent.Future
import play.api.libs.concurrent.Execution.Implicits.defaultContext
class Sessions extends Controller with LoginLogout with AuthConfigImpl {
val loginForm = Form {
mapping("email" -> email, "password" -> text)(Account.authenticate)(_.map(u => (u.email, "")))
.verifying("Invalid email or password", result => result.isDefined)
}
def login = Action { implicit request =>
Ok(html.stateless.login(loginForm))
}
def logout = Action.async { implicit request =>
gotoLogoutSucceeded.map(_.flashing(
"success" -> "You've been logged out"
))
}
def authenticate = Action.async { implicit request =>
loginForm.bindFromRequest.fold(
formWithErrors => Future.successful(BadRequest(html.stateless.login(formWithErrors))),
user => gotoLoginSucceeded(user.get.id)
)
}
}
示例9: Sessions
//设置package包名称以及导入依赖的类
package controllers.standard
import jp.t2v.lab.play2.auth.LoginLogout
import model.Account
import play.api.data.Form
import play.api.data.Forms._
import play.api.mvc.{Action, Controller}
import views.html
import scala.concurrent.Future
import play.api.libs.concurrent.Execution.Implicits.defaultContext
class Sessions extends Controller with LoginLogout with AuthConfigImpl {
val loginForm = Form {
mapping("email" -> email, "password" -> text)(Account.authenticate)(_.map(u => (u.email, "")))
.verifying("Invalid email or password", result => result.isDefined)
}
def login = Action { implicit request =>
Ok(html.standard.login(loginForm))
}
def logout = Action.async { implicit request =>
gotoLogoutSucceeded.map(_.flashing(
"success" -> "You've been logged out"
).removingFromSession("rememberme"))
}
def authenticate = Action.async { implicit request =>
loginForm.bindFromRequest.fold(
formWithErrors => Future.successful(BadRequest(html.standard.login(formWithErrors))),
user => gotoLoginSucceeded(user.get.id)
)
}
}
示例10: Sessions
//设置package包名称以及导入依赖的类
package controllers.builder
import jp.t2v.lab.play2.auth.LoginLogout
import model.Account
import play.api.data.Form
import play.api.data.Forms._
import play.api.mvc.{Action, Controller}
import views.html
import scala.concurrent.Future
import play.api.libs.concurrent.Execution.Implicits.defaultContext
class Sessions extends Controller with LoginLogout with AuthConfigImpl {
val loginForm = Form {
mapping("email" -> email, "password" -> text)(Account.authenticate)(_.map(u => (u.email, "")))
.verifying("Invalid email or password", result => result.isDefined)
}
def login = Action { implicit request =>
Ok(html.builder.login(loginForm))
}
def logout = Action.async { implicit request =>
gotoLogoutSucceeded.map(_.flashing(
"success" -> "You've been logged out"
))
}
def authenticate = Action.async { implicit request =>
loginForm.bindFromRequest.fold(
formWithErrors => Future.successful(BadRequest(html.builder.login(formWithErrors))),
user => gotoLoginSucceeded(user.get.id)
)
}
}
示例11: Sessions
//设置package包名称以及导入依赖的类
package controllers.ephemeral
import jp.t2v.lab.play2.auth.LoginLogout
import model.Account
import play.api.data.Form
import play.api.data.Forms._
import play.api.mvc.{Action, Controller}
import views.html
import scala.concurrent.Future
import play.api.libs.concurrent.Execution.Implicits.defaultContext
class Sessions extends Controller with LoginLogout with AuthConfigImpl {
val loginForm = Form {
mapping("email" -> email, "password" -> text)(Account.authenticate)(_.map(u => (u.email, "")))
.verifying("Invalid email or password", result => result.isDefined)
}
def login = Action { implicit request =>
Ok(html.ephemeral.login(loginForm))
}
def logout = Action.async { implicit request =>
gotoLogoutSucceeded.map(_.flashing(
"success" -> "You've been logged out"
))
}
def authenticate = Action.async { implicit request =>
loginForm.bindFromRequest.fold(
formWithErrors => Future.successful(BadRequest(html.ephemeral.login(formWithErrors))),
user => gotoLoginSucceeded(user.get.id)
)
}
}
示例12: SessionController
//设置package包名称以及导入依赖的类
package controllers
import com.google.inject.Inject
import forms.Login
import jp.t2v.lab.play2.auth.LoginLogout
import models.Account
import play.api.i18n.{I18nSupport, MessagesApi}
import play.api.mvc.{Action, Controller, RequestHeader, Result}
import utils.Tools
import scala.concurrent.{ExecutionContext, Future}
class SessionController @Inject()(val messagesApi: MessagesApi, implicit val ec: ExecutionContext) extends Controller with LoginLogout with AuthConfigImpl with I18nSupport {
def view() = Action {
Ok(views.html.login(Login.form))
}
def login() = Action.async { implicit req =>
Login.form.bindFromRequest().fold(
errors => Future.successful(BadRequest(errors.errors.mkString("\n"))),
authenticate
)
}
private def authenticate(login: Login)(implicit req: RequestHeader): Future[Result] = {
Account.where('name -> login.name).apply().headOption.flatMap { account =>
if(Tools.toHash(login.password, account.salt) sameElements account.hash) Some(gotoLoginSucceeded(account.id)) else None
}.getOrElse(Future.successful(Unauthorized("Authentication failed")))
}
}
示例13: AuthenticateController
//设置package包名称以及导入依赖的类
package controllers.api.auth
import javax.inject.{Inject, Singleton}
import akka.actor.ActorSystem
import com.github.uryyyyyyy.dto.AccountDTO
import jp.t2v.lab.play2.auth.LoginLogout
import play.api.libs.json.JsValue
import play.api.mvc._
import scala.concurrent.{ExecutionContext, Future}
@Singleton
class AuthenticateController @Inject() (actorSystem: ActorSystem) extends Controller with LoginLogout with AuthConfigImpl {
implicit val myExecutionContext: ExecutionContext = actorSystem.dispatcher
private def authenticate(dto: AccountDTO): Either[Result, MyUser] = {
AuthService.authenticate(dto.id, dto.password) match {
case None => Left(Unauthorized("authentication failed"))
case Some(user) => Right(user)
}
}
private def jsonDecode(request: Request[JsValue]): Either[Result, AccountDTO] = {
request.body.validate[AccountDTO].asOpt match {
case None => Left(BadRequest("bad request"))
case Some(v) => Right(v)
}
}
def login() = Action.async(BodyParsers.parse.json) { implicit request =>
val myUser = for{
account <- jsonDecode(request).right
myUser <- authenticate(account).right
} yield myUser
myUser match {
case Left(r) => Future{r}
case Right(user) => gotoLoginSucceeded(user.id).map(_.withCookies(Cookie("userId", user.id), Cookie("userRole", user.role.toString)))
}
}
def logout() = Action.async { implicit request =>
gotoLogoutSucceeded
}
}
示例14: LoginData
//设置package包名称以及导入依赖的类
package controllers.admin
import controllers.{ HasConfig, HasUserService, Security }
import javax.inject.Inject
import jp.t2v.lab.play2.auth.{ AuthElement, LoginLogout }
import play.api.Configuration
import play.api.data.Form
import play.api.data.Forms._
import play.api.i18n.{ I18nSupport, MessagesApi }
import play.api.mvc.{ Action, Controller }
import scala.concurrent.{ ExecutionContext, Future }
import services.user.UserService
case class LoginData(usernameOrPassword: String, password: String)
class LoginLogoutController @Inject() (
val config: Configuration,
val users: UserService,
val messagesApi: MessagesApi,
implicit val ctx: ExecutionContext
) extends Controller with AuthElement with HasConfig with HasUserService with Security with LoginLogout with I18nSupport {
private val MESSAGE = "message"
private val INVALID_LOGIN = "Invalid Username or Password"
val loginForm = Form(
mapping(
"username" -> nonEmptyText,
"password" -> nonEmptyText
)(LoginData.apply)(LoginData.unapply)
)
def showLoginForm(destination: Option[String]) = Action { implicit request =>
destination match {
case None => Ok(views.html.admin.login(loginForm))
case Some(dest) => Ok(views.html.admin.login(loginForm)).withSession("access_uri" -> dest)
}
}
def processLogin = Action.async { implicit request =>
loginForm.bindFromRequest.fold(
formWithErrors =>
Future(BadRequest(views.html.admin.login(formWithErrors))),
loginData =>
users.validateUser(loginData.usernameOrPassword, loginData.password).flatMap {
case Some(validUser) => gotoLoginSucceeded(validUser.username)
case None => Future(Redirect(routes.LoginLogoutController.showLoginForm()).flashing(MESSAGE -> INVALID_LOGIN))
}
)
}
def logout = Action.async { implicit request =>
gotoLogoutSucceeded
}
}