Scala Liftweb : Use Forgot Password Inbuilt Functionality to send Email with Unique Token

If you are implementing Forgot password functionality in your web application using Liftweb and you have such a scenario where you have to send redirect URL with unique token in Email , Liftweb with Scala provides a wonderful functionality to handle this .

1) Write a function to send URL with unique token in Email .

def sendLoginToken(user: User): Unit = {
    import net.liftweb.util.Mailer._
    val token = LoginToken.createForUserId(
    val msgTxt =
|Someone requested a link to change your password on the %s website.
|If you did not request this, you can safely ignore it. It will expire 48 hours from the time this message was sent.
|Follow the link below or copy and paste it into your internet browser.
""".format(siteName, token.url, sysUsername).stripMargin
      Subject("%s Password Help".format(siteName)),

This function will generate a Unique token url and save unique token in collection.
2) Override handleLoginToken function according to your requirement .

override def handleLoginToken: Box[LiftResponse] = {
        var respUrl = indexUrl.toString
        S.param("token").flatMap(LoginToken.findByStringId) match {
          case Full(at) if (at.expires.isExpired) ⇒ {
            S.error("Login token has expired")
          case Full(at) ⇒ logUserInFromToken( match {
            case Full(_) ⇒ respUrl = loginTokenAfterUrl.toString
            case _ ⇒ S.error("User not found")
          case _ ⇒  S.error("Login token has expired")


When user would click on URL , he would be redirected to password page . After redirecting , user session would be true and you can get current user . Now ask user for his new password and replace with old password .
Once URL is clicked by user , it would be expired and deleted from login token collection . It can not be used again .

About Ayush Mishra

Ayush is the Sr. Software Consultant @ Knoldus Software LLP. In his 5 years of experience he has become developer with proven experience in architecting and developing web applications. Ayush has a Masters in Computer Application from U.P. Technical University, Ayush is a strong-willed and self-motivated professional who takes deep care in adhering to quality norms within projects. He is capable of managing challenging projects with remarkable deadline sensitivity without compromising code quality. .
This entry was posted in Scala. Bookmark the permalink.

One Response to Scala Liftweb : Use Forgot Password Inbuilt Functionality to send Email with Unique Token

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s