【问题标题】:How to update securesocial username after user is created创建用户后如何更新securesocial用户名
【发布时间】:2013-08-08 12:23:27
【问题描述】:

我正在尝试使用在 Play Framework 应用中实现的 Secure Social 插件编写一个 editUser 页面。更改用户名后,我无法保持登录状态。更改用户名后,当我为 editUser 表单按下提交时,会出现此问题。它进入登录页面并显示“您需要登录才能访问该页面”。所需的行为是重定向到 editUser 页面而无需重新登录。在数据库中,一切都已成功更新。这样就可以了,只是不再登录。

下面是我的“用户”控制器的控制器方法,用于用户更新的 POST。

如果有人能帮助我解决这个问题,将不胜感激。谢谢。

// The form uses the following case class
case class AccountInfo(userName: String, firstName: String, lastName: String, email: Option[String]) 

def update(username: String) = SecuredAction { implicit request =>
    this.editAccountForm.bindFromRequest.fold (
      hasErrors = { info =>
        Redirect(routes.Users.edit()).flashing(Flash(editAccountForm.data) +
          ("error" -> Messages("validation.errors")))
      },
      success = { info =>
        DB.withSession { implicit s: Session =>
          val uid = User.currentUser(request.user.id.id,providerId).get.uid
          User.update(uid, info, providerId)
        }
        val message = Messages("user.update.success")

        Redirect(routes.Users.edit()).flashing("success" -> message)
          .withCookies(request.cookies.get("id").get)
      }
    )
  }

【问题讨论】:

    标签: scala playframework securesocial


    【解决方案1】:

    通过更改用户名,您将更改用于识别用户的值(用户名 + 提供者 ID)。发生的情况是,在下一个请求时,SecureSocial 正在寻找旧用户名,由于它在数据库中找不到它,它只会将你踢出。

    除了更新数据库之外,您还应该更新为当前会话存储的 Authenticator。比如:

    SecureSocial.authenticatorFromRequest(request).map { authenticator =>
        val newId = request.user.id.copy( id = userName )
        Authenticator.save(authenticator.copy( userId = newId))
    }
    

    这应该可以正常工作。此外,您不需要将 id cookie 添加到重定向中。 SecureSocial 会为您做到这一点。

    【讨论】:

    • 感谢您的回复豪尔赫。我试过这个。我更新了属性以反映新的securesocial更新中的变化。它没有用。我将代码块放在 Redirect(routes.Users.edit()).flash("success" -> message) 之前。知道发生了什么吗?谢谢!
    猜你喜欢
    • 1970-01-01
    • 2019-01-15
    • 2022-08-05
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-11
    相关资源
    最近更新 更多