All Projects → ezzahraoui → play-silhouette-reactivemongo-seed

ezzahraoui / play-silhouette-reactivemongo-seed

Licence: Apache-2.0 license
Seed Application for PlayFramework, Silhouette and ReactiveMongo

Programming Languages

scala
5932 projects
shell
77523 projects
HTML
75241 projects
CSS
56736 projects

Projects that are alternatives of or similar to play-silhouette-reactivemongo-seed

play-silhouette-rest-mongo
Seed project for Play Framework 2.6 with Silhouette 5.0, exposing a REST API for sign-up and signin. ReactiveMongo + MongoDB used as a backing store.
Stars: ✭ 19 (-13.64%)
Mutual labels:  play-framework, silhouette, reactivemongo
play-docker-aws-tutorial
101 tutorial: How to deploy Play application to Amazon Lightsail (AWS) using Docker
Stars: ✭ 25 (+13.64%)
Mutual labels:  play-framework
Play Pdf
A PDF module for the Play framework
Stars: ✭ 108 (+390.91%)
Mutual labels:  play-framework
Scala Play React Seed
❄️ Java Play 2.7.x + React seed project with full-fledged build process
Stars: ✭ 180 (+718.18%)
Mutual labels:  play-framework
Play Guard
Play2 module for rate limiting, based on token bucket algorithm
Stars: ✭ 123 (+459.09%)
Mutual labels:  play-framework
React Play
Render React components in the Play Framework with JDK8's JavaScript engine
Stars: ✭ 216 (+881.82%)
Mutual labels:  play-framework
Scala Play Angular Seed
🍀 Scala Play 2.7.x + Angular 8 with Angular CLI seed project with full-fledged build process
Stars: ✭ 85 (+286.36%)
Mutual labels:  play-framework
play2-scala-pdf
A PDF module for Play Framework 2 (Scala)
Stars: ✭ 25 (+13.64%)
Mutual labels:  play-framework
Play Json Derived Codecs
Stars: ✭ 179 (+713.64%)
Mutual labels:  play-framework
Pac4j
Security engine for Java (authentication, authorization, multi frameworks): OAuth, CAS, SAML, OpenID Connect, LDAP, JWT...
Stars: ✭ 2,097 (+9431.82%)
Mutual labels:  play-framework
Api First Hand
API-First bootstrapping tool for building RESTful web services from a Swagger/OpenAPI spec
Stars: ✭ 138 (+527.27%)
Mutual labels:  play-framework
Play Redis
Play framework 2 cache plugin as an adapter to redis-server
Stars: ✭ 152 (+590.91%)
Mutual labels:  play-framework
Play Mailer
Play mailer plugin
Stars: ✭ 243 (+1004.55%)
Mutual labels:  play-framework
Kebs
Scala library to eliminate boilerplate
Stars: ✭ 113 (+413.64%)
Mutual labels:  play-framework
sprest
Sprest is a collection of libaries to make building REST services simpler using Spray.
Stars: ✭ 47 (+113.64%)
Mutual labels:  reactivemongo
Play2 Html5tags
HTML5 form tags module for Play Framework
Stars: ✭ 101 (+359.09%)
Mutual labels:  play-framework
Validation
validation api extracted from play
Stars: ✭ 194 (+781.82%)
Mutual labels:  play-framework
play-ebean
Play Ebean module
Stars: ✭ 29 (+31.82%)
Mutual labels:  play-framework
intelliroutes
Support for Play Routes in IntelliJ IDEA
Stars: ✭ 21 (-4.55%)
Mutual labels:  play-framework
sbt-sass
A fork of the sbt-sass repository which seems to be abandoned.
Stars: ✭ 32 (+45.45%)
Mutual labels:  play-framework

Silhouette Authentication with ReactiveMongo and Play 2

This application is based on Silhouette seed Template:

https://github.com/mohiva/play-silhouette-seed

And Silhouette ReactiveMongo persistence layer:

https://github.com/mohiva/play-silhouette-persistence-reactivemongo

The original template uses an array on memory to authenticate users, to use a MongoDB database instead, follow the step by step example.

Edit build.sbt and add the following dependencies

libraryDependencies ++= Seq(
  "org.reactivemongo" %% "play2-reactivemongo" % "0.12.1",
  "com.mohiva" %% "play-silhouette-persistence-reactivemongo" % "4.0.1"
)

Add ReactiveMongo configuration to application.conf

play.modules.enabled += "play.modules.reactivemongo.ReactiveMongoModule"
mongodb.uri = "mongodb://localhost:27017/test"

Add the implementation of the different delegables on SilhouetteModule.scala

/**
 * Provides the implementation of the delegable OAuth1 auth info DAO.
 *
 * @param reactiveMongoApi The ReactiveMongo API.
 * @param config The Play configuration.
 * @return The implementation of the delegable OAuth1 auth info DAO.
 */
@Provides
def provideOAuth1InfoDAO(reactiveMongoApi: ReactiveMongoApi, config: Configuration): DelegableAuthInfoDAO[OAuth1Info] = {
  implicit lazy val format = Json.format[OAuth1Info]
  new MongoAuthInfoDAO[OAuth1Info](reactiveMongoApi, config)
}

/**
 * Provides the implementation of the delegable OAuth2 auth info DAO.
 *
 * @param reactiveMongoApi The ReactiveMongo API.
 * @param config The Play configuration.
 * @return The implementation of the delegable OAuth2 auth info DAO.
 */
@Provides
def provideOAuth2InfoDAO(reactiveMongoApi: ReactiveMongoApi, config: Configuration): DelegableAuthInfoDAO[OAuth2Info] = {
  implicit lazy val format = Json.format[OAuth2Info]
  new MongoAuthInfoDAO[OAuth2Info](reactiveMongoApi, config)
}

/**
 * Provides the implementation of the delegable OpenID auth info DAO.
 *
 * @param reactiveMongoApi The ReactiveMongo API.
 * @param config The Play configuration.
 * @return The implementation of the delegable OpenID auth info DAO.
 */
@Provides
def provideOpenIDInfoDAO(reactiveMongoApi: ReactiveMongoApi, config: Configuration): DelegableAuthInfoDAO[OpenIDInfo] = {
  implicit lazy val format = Json.format[OpenIDInfo]
  new MongoAuthInfoDAO[OpenIDInfo](reactiveMongoApi, config)
}

/**
 * Provides the implementation of the delegable password auth info DAO.
 *
 * @param reactiveMongoApi The ReactiveMongo API.
 * @param config The Play configuration.
 * @return The implementation of the delegable password auth info DAO.
 */
@Provides
def providePasswordInfoDAO(reactiveMongoApi: ReactiveMongoApi, config: Configuration): DelegableAuthInfoDAO[PasswordInfo] = {
  implicit lazy val format = Json.format[PasswordInfo]
  new MongoAuthInfoDAO[PasswordInfo](reactiveMongoApi, config)
}

Inject ReactiveMongoApi on UserDAOImpl.scala and AuthTokenDAOImpl.scala

class AuthTokenDAOImpl @Inject() (val reactiveMongoApi: ReactiveMongoApi) extends AuthTokenDAO

class UserDAOImpl @Inject() (val reactiveMongoApi: ReactiveMongoApi) extends UserDAO

Specify the collection name on both files

def collection: Future[JSONCollection] = reactiveMongoApi.database.map(_.collection("silhouette.user"))

def collection: Future[JSONCollection] = reactiveMongoApi.database.map(_.collection("silhouette.token"))

Rename password collections if needed on silhouette.conf

persistence.reactivemongo.collection.OAuth1Info = "silhouette.password"
persistence.reactivemongo.collection.OAuth2Info = "silhouette.password"
persistence.reactivemongo.collection.OpenIDInfo = "silhouette.password"
persistence.reactivemongo.collection.PasswordInfo = "silhouette.password"

And finally rewrite functions on the both classes, for example I will show UserDaoImpl functions

/**
 * Finds a user by its login info.
 *
 * @param loginInfo The login info of the user to find.
 * @return The found user or None if no user for the given login info could be found.
 */
def find(loginInfo: LoginInfo): Future[Option[User]] = {
  val query = Json.obj("loginInfo" -> loginInfo)
  collection.flatMap(_.find(query).one[User])
}

/**
 * Finds a user by its user ID.
 *
 * @param userID The ID of the user to find.
 * @return The found user or None if no user for the given ID could be found.
 */
def find(userID: UUID): Future[Option[User]] = {
  val query = Json.obj("userID" -> userID)
  collection.flatMap(_.find(query).one[User])
}

/**
 * Saves a user.
 *
 * @param user The user to save.
 * @return The saved user.
 */
def save(user: User): Future[User] = {
  collection.flatMap(_.insert(user))
  Future.successful(user)
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].