SecureSocial 3.0-M3 Custom Login view with play 2.

2019-08-09 05:52发布

I'm trying to build a custom login view using the latest version of SecureSocial 3.0-M3 and I've struggeling for few days with this.

As suggested by fotis on the secureSocial Google Group, I created a MyViewTemplates class that extends ViewTemplates and overrides the required methods:

package controllers
import play.api.mvc.{RequestHeader, Request}
import play.twirl.api.Html

import securesocial.core.{BasicProfile, RuntimeEnvironment}
import play.api.data.Form
import play.api.i18n.Lang
import play.api.mvc.Controller
import play.twirl.api._
import securesocial.controllers.ChangeInfo
import securesocial.controllers.RegistrationInfo
import securesocial.controllers.ViewTemplates
import securesocial.core.RuntimeEnvironment
import play.api.mvc.Action
import play.api.Logger
import views.html.index

/**
 * If no license is here then you can whatever you like!
 * and of course I am not liable
 *
 * Created by fotis on 21/08/14.
 */
class MyViewTemplates(env:RuntimeEnvironment[_]) extends ViewTemplates{

  implicit val implicitEnv = env

  override def getLoginPage(form: Form[(String, String)],msg: Option[String] = None)(implicit request: RequestHeader, lang: Lang): Html = {
    Logger.debug("Redirecting to custom view")
    views.html.login(form, msg)(request, lang, env)

  }

  override def getSignUpPage(form: Form[RegistrationInfo], token: String)(implicit request: RequestHeader, lang: Lang): Html = {
    securesocial.views.html.Registration.signUp(form, token)(request, lang, env)
  }

  override def getStartSignUpPage(form: Form[String])(implicit request: RequestHeader, lang: Lang): Html = {
    securesocial.views.html.Registration.startSignUp(form)(request, lang, env)
  }

  override def getStartResetPasswordPage(form: Form[String])(implicit request: RequestHeader, lang: Lang): Html = {
    securesocial.views.html.Registration.startResetPassword(form)(request, lang, env)
  }

  override def getResetPasswordPage(form: Form[(String, String)], token: String)(implicit request: RequestHeader, lang: Lang): Html = {
    securesocial.views.html.Registration.resetPasswordPage(form, token)(request, lang, env)
  }

  override def getPasswordChangePage(form: Form[ChangeInfo])(implicit request: RequestHeader, lang: Lang): Html = {
    securesocial.views.html.passwordChange(form)(request, lang, env)
  }

  override def getNotAuthorizedPage(implicit request: RequestHeader, lang: Lang): Html = {
    securesocial.views.html.notAuthorized()(request, lang, env)
  }


}

I have added a login page:

@(loginForm: play.api.data.Form[(String,String)], errorMsg: Option[String] = None)(implicit request: play.api.mvc.RequestHeader, lang: Lang, env:securesocial.core.RuntimeEnvironment[_])
@import securesocial.core.providers.UsernamePasswordProvider.UsernamePassword
@main(Messages("securesocial.login.title")) {
    <div class="page-header">
        <h1>@Messages("securesocial.login.title")</h1>
    </div>

    @errorMsg.map { msg =>
        <div class="alert alert-error">
            @Messages(msg)
        </div>
    }

    @request.flash.get("success").map { msg =>
        <div class="alert alert-info">
            @msg
        </div>
    }

    @request.flash.get("error").map { msg =>
        <div class="alert alert-error">
            @msg
        </div>
    }


    @defining( env.providers.values.filter( _.id != UsernamePassword) ) { externalProviders =>


        @env.providers.get(UsernamePassword).map { up =>
            <div class="clearfix">
                @if( externalProviders.size > 0 ) {
                    <p>@Messages("securesocial.login.useEmailAndPassword")</p>
                } else {
                    <p>@Messages("securesocial.login.useEmailAndPasswordOnly")</p>
                }

                @securesocial.views.html.provider("userpass", Some(loginForm))
            </div>
        }
    }
}

But this page never gets called. It's always the default SecurSocial login page that is displayed.

What am I missing? Does anyone has a complete code sample with this version of SecureSocial and customized login view?

Many thanks for your help!

1条回答
Summer. ? 凉城
2楼-- · 2019-08-09 06:19

I had to override viewTemplates in the custom RuntimeEnvironment:

override lazy val viewTemplates: ViewTemplates = MyViewTemplates
查看更多
登录 后发表回答