This project uses Play 2.6's AuthenticatedBuilder and Action composition to create user ID / password authentication.
This project is inspired by Jorge Aliss's SecureSocial, and uses some code from play-silhouette-seed. Instead of supporting authentication via social providers, it only addresses user id / password authentication. Functionality includes sign up (registration), log in, log out, password reset for when the user forgets their password, the ability for authenticated users to change their password, and cleans up stale tokens and abandoned user signups.
Uses Quill and an in-memory H2 database.
Uses WebJars with Twitter Bootstrap and HtmlForm's HTML5 widgets for Bootstrap.
It would be a lot of work to turn this into a library. Instead, you could incorporate this code into a larger project as a subproject, and modify as required.
In addition to Play Framework's Action
handlers, this project adds
UserAwareAction
and SecuredAction handlers.
If you are familiar with SecureSocial, these work exactly the same as similarly named handlers in SecureSocial.
Here is an example of a Controller
containing all 3 types of Action
handlers:
package controllers
import javax.inject.Inject
import auth.Authentication
import play.api.i18n.{I18nSupport, MessagesApi}
import play.api.mvc._
class ApplicationController @Inject() (implicit
val messagesApi: MessagesApi,
webJarAssets: WebJarAssets,
authentication: Authentication
) extends Controller with I18nSupport {
import authentication._
def index() = Action { implicit request =>
Ok(views.html.index())
}
def securedAction = SecuredAction { implicit authenticatedRequest =>
val user: User = authenticatedRequest.user
Ok(s"${ user.fullName } is secure.")
}
def userAwareAction = UserAwareAction { implicit requestWithUser =>
val maybeUser: Option[User] = requestWithUser.user
Ok(s"${ maybeUser.map(_.fullName).getOrElse("No-one") } is aware of their lack of security.")
}
}
Set environment variables that establish email server settings, then run the program.
$ export SMTP_PASSWORD=myPassword
$ export SMTP_USER=santa@claus.com
$ export SMTP_FROM="Santa Claus <santa@claus.com>"
$ export SMTP_HOST=smtp.claus.com
$ export SMTP_PORT=465
$ export EMAIL_LOGO_URL="http://siteassets.scalacourses.com/images/ScalaCoursesHeadingLogo371x56.png"
$ export EMAIL_SIGNATURE="<p>Thank you,<br/></p>\n<p>The ScalaCourses mailbot</p>"
$ sbt run
This project is sponsored by Micronautics Research Corporation, the company that delivers online Scala and Play training via ScalaCourses.com. You can learn how this project works by taking the Introduction to Scala, Intermediate Scala and Introduction to Play courses.
This software is published under the Apache 2.0 License.