mslinn / play-authenticated

Play Framework user Id / password Authentication seed project in the style of SecureSocial with Quill

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Play Framework User Id / Password Authentication Seed Project

License Build Status GitHub version

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.

Using Authentication-Aware Actions

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.")
  }
}

Scaladoc

Here

To Run

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

Sponsor

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.

License

This software is published under the Apache 2.0 License.

About

Play Framework user Id / password Authentication seed project in the style of SecureSocial with Quill


Languages

Language:Scala 78.0%Language:HTML 17.7%Language:CSS 3.4%Language:JavaScript 0.9%