Skip to content

Instantly share code, notes, and snippets.

@freekh
Last active May 15, 2017 10:48
Show Gist options
  • Save freekh/9132313 to your computer and use it in GitHub Desktop.
Save freekh/9132313 to your computer and use it in GitHub Desktop.

Revisions

  1. freekh revised this gist Feb 21, 2014. 1 changed file with 1 addition and 15 deletions.
    16 changes: 1 addition & 15 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ object Application extends Controller {
    }

    def login = Action {
    Ok(views.html.login(loginForm))
    Ok(views.html.login(Form(loginFormMapping)))
    }

    def logout = Action {
    @@ -51,13 +51,6 @@ object Application extends Controller {
    futureResponse.map(_.status == 200)
    }

    val checkLoginFunc = checkLogin _

    // val loginForm: Form[Credential] = Form(
    // loginFormMapping.verifying("bad username/password combo", {
    // checkLogin _
    // }))

    def submit = Action.async(BodyParsers.parse.urlFormEncoded) { implicit request =>
    Form(loginFormMapping).bindFromRequest().fold(
    badForm => Future(BadRequest(views.html.login(badForm))),
    @@ -71,12 +64,5 @@ object Application extends Controller {
    }
    })
    }
    // val filledForm = loginForm.bindFromRequest()
    //
    // filledForm.fold(
    // badForm => BadRequest(views.html.login(badForm)),
    // credential => {
    // Redirect(routes.Application.index).withSession(UsernameSessionKey -> credential.username)
    // })

    }
  2. freekh created this gist Feb 21, 2014.
    82 changes: 82 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,82 @@
    package controllers

    import play.api._
    import play.api.mvc._
    import play.api.data.Form
    import play.api.data.Forms._
    import play.api.data.format.Formats._
    import models.Credential
    import views.html.defaultpages.unauthorized
    import scala.concurrent.Await
    import play.api.libs.ws.WS
    import scala.concurrent.Future
    import play.api.libs.ws.Response

    object Application extends Controller {

    val UsernameSessionKey = "username"

    def index = Action { request =>
    request.session.get(UsernameSessionKey) match {
    case Some(username) => Ok(views.html.index(username))
    case None => Redirect(routes.Application.login)
    }
    }

    def login = Action {
    Ok(views.html.login(loginForm))
    }

    def logout = Action {
    Redirect(routes.Application.login).withNewSession
    }

    val loginFormMapping = mapping(
    "username" -> nonEmptyText(4),
    "password" -> nonEmptyText(4)) {
    (username, password) => Credential(username, password)
    } {
    credential => Some(credential.username -> credential.password)
    }

    import play.api.libs.concurrent.Execution.Implicits._

    def checkLogin(c: Credential): Future[Boolean] = {

    val requestHolder = WS.url(models.github.baseUrl + "/user")
    .withAuth(c.username, c.password, com.ning.http.client.Realm.AuthScheme.BASIC)

    val futureResponse: Future[Response] = requestHolder.head()

    futureResponse.map(_.status == 200)
    }

    val checkLoginFunc = checkLogin _

    // val loginForm: Form[Credential] = Form(
    // loginFormMapping.verifying("bad username/password combo", {
    // checkLogin _
    // }))

    def submit = Action.async(BodyParsers.parse.urlFormEncoded) { implicit request =>
    Form(loginFormMapping).bindFromRequest().fold(
    badForm => Future(BadRequest(views.html.login(badForm))),
    credential => {
    checkLogin(credential).map { loginSuccessful =>
    {
    if (loginSuccessful) {
    Redirect(routes.Application.index).withSession(UsernameSessionKey -> credential.username)
    } else BadRequest(views.html.login(Form(loginFormMapping).withGlobalError("Username password combo wrong")))
    }
    }
    })
    }
    // val filledForm = loginForm.bindFromRequest()
    //
    // filledForm.fold(
    // badForm => BadRequest(views.html.login(badForm)),
    // credential => {
    // Redirect(routes.Application.index).withSession(UsernameSessionKey -> credential.username)
    // })

    }