signup from mobile
This commit is contained in:
parent
0842251ec9
commit
ed51d3c1a4
|
@ -1,12 +1,12 @@
|
|||
package controllers
|
||||
|
||||
import play.api.data._, Forms._
|
||||
import play.api.libs.json.Json
|
||||
import play.api.libs.json._
|
||||
import play.api.mvc._, Results._
|
||||
|
||||
import lila.app._
|
||||
import lila.common.LilaCookie
|
||||
import lila.user.UserRepo
|
||||
import lila.user.{ UserRepo, User => UserModel }
|
||||
import views._
|
||||
|
||||
object Auth extends LilaController {
|
||||
|
@ -14,7 +14,15 @@ object Auth extends LilaController {
|
|||
private def api = Env.security.api
|
||||
private def forms = Env.security.forms
|
||||
|
||||
private def authenticateUser(u: lila.user.User)(implicit ctx: lila.api.Context) = {
|
||||
private def mobileUserOk(u: UserModel): Fu[Result] =
|
||||
lila.game.GameRepo nowPlaying u map { povs =>
|
||||
Ok {
|
||||
Env.user.jsonView(u, extended = true) ++ Json.obj(
|
||||
"nowPlaying" -> JsArray(povs take 9 map Env.api.lobbyApi.nowPlaying))
|
||||
}
|
||||
}
|
||||
|
||||
private def authenticateUser(u: UserModel)(implicit ctx: lila.api.Context) = {
|
||||
implicit val req = ctx.req
|
||||
u.ipBan.fold(
|
||||
Env.security.firewall.blockIp(req.remoteAddress) inject BadRequest("blocked by firewall"),
|
||||
|
@ -23,13 +31,7 @@ object Auth extends LilaController {
|
|||
html = Redirect {
|
||||
get("referrer").filter(_.nonEmpty) orElse req.session.get(api.AccessUri) getOrElse routes.Lobby.home.url
|
||||
}.fuccess,
|
||||
api = _ => lila.game.GameRepo nowPlaying u map { povs =>
|
||||
Ok {
|
||||
import play.api.libs.json._
|
||||
Env.user.jsonView(u, extended = true) ++ Json.obj(
|
||||
"nowPlaying" -> JsArray(povs take 9 map Env.api.lobbyApi.nowPlaying))
|
||||
}
|
||||
}
|
||||
api = _ => mobileUserOk(u)
|
||||
) map {
|
||||
_ withCookies LilaCookie.withSession { session =>
|
||||
session + ("sessionId" -> sessionId) - api.AccessUri
|
||||
|
@ -67,25 +69,44 @@ object Auth extends LilaController {
|
|||
}
|
||||
|
||||
def signup = Open { implicit ctx =>
|
||||
forms.signupWithCaptcha map {
|
||||
forms.signup.websiteWithCaptcha map {
|
||||
case (form, captcha) => Ok(html.auth.signup(form, captcha))
|
||||
}
|
||||
}
|
||||
|
||||
private def doSignup(username: String, password: String)(res: UserModel => Fu[Result])(implicit ctx: lila.api.Context) =
|
||||
Firewall {
|
||||
implicit val req = ctx.req
|
||||
UserRepo.create(username, password, ctx.blindMode) flatMap { userOption =>
|
||||
val user = userOption err "No user could be created for %s".format(username)
|
||||
api.saveAuthentication(
|
||||
user.id,
|
||||
lila.api.Mobile.Api.requestVersion(ctx.req)
|
||||
) flatMap { sessionId =>
|
||||
res(user) map {
|
||||
_ withCookies LilaCookie.session("sessionId", sessionId)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def signupPost = OpenBody { implicit ctx =>
|
||||
implicit val req = ctx.body
|
||||
forms.signup.bindFromRequest.fold(
|
||||
err => forms.anyCaptcha map { captcha =>
|
||||
BadRequest(html.auth.signup(err, captcha))
|
||||
},
|
||||
data => Firewall {
|
||||
UserRepo.create(data.username, data.password, ctx.blindMode) flatMap { userOption =>
|
||||
val user = userOption err "No user could be created for %s".format(data.username)
|
||||
api.saveAuthentication(user.id, lila.api.Mobile.Api.requestVersion(ctx.req)) map { sessionId =>
|
||||
Redirect(routes.User.show(user.username)) withCookies LilaCookie.session("sessionId", sessionId)
|
||||
}
|
||||
negotiate(
|
||||
html = forms.signup.website.bindFromRequest.fold(
|
||||
err => forms.anyCaptcha map { captcha =>
|
||||
BadRequest(html.auth.signup(err, captcha))
|
||||
},
|
||||
data => doSignup(data.username, data.password) { user =>
|
||||
fuccess(Redirect(routes.User.show(user.username)))
|
||||
}
|
||||
}
|
||||
),
|
||||
api = _ => forms.signup.mobile.bindFromRequest.fold(
|
||||
err => fuccess(BadRequest(Json.obj(
|
||||
"error" -> err.errorsAsJson
|
||||
))),
|
||||
data => doSignup(data.username, data.password)(mobileUserOk)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
@ -22,8 +22,9 @@ final class DataForm(val captcher: akka.actor.ActorSelection) extends lila.hub.C
|
|||
|
||||
def emptyWithCaptcha = withCaptcha(empty)
|
||||
|
||||
val signup = Form(mapping(
|
||||
"username" -> nonEmptyText.verifying(
|
||||
object signup {
|
||||
|
||||
private val username = nonEmptyText.verifying(
|
||||
Constraints minLength 2,
|
||||
Constraints maxLength 20,
|
||||
Constraints.pattern(
|
||||
|
@ -32,17 +33,23 @@ final class DataForm(val captcher: akka.actor.ActorSelection) extends lila.hub.C
|
|||
Constraints.pattern(
|
||||
regex = """^[^\d].+$""".r,
|
||||
error = "The username must not start with a number")
|
||||
),
|
||||
"password" -> text(minLength = 4),
|
||||
"gameId" -> nonEmptyText,
|
||||
"move" -> nonEmptyText
|
||||
)(SignupData.apply)(_ => None)
|
||||
.verifying("This user already exists", d => !userExists(d).await)
|
||||
.verifying("This username is not acceptable", d => !usernameSucks(d.username.toLowerCase))
|
||||
.verifying(captchaFailMessage, validateCaptcha _)
|
||||
)
|
||||
).verifying("This user already exists", u => !$count.exists(u.toLowerCase).await)
|
||||
.verifying("This username is not acceptable", u => !usernameSucks(u.toLowerCase))
|
||||
|
||||
def signupWithCaptcha = withCaptcha(signup)
|
||||
val website = Form(mapping(
|
||||
"username" -> username,
|
||||
"password" -> text(minLength = 4),
|
||||
"gameId" -> nonEmptyText,
|
||||
"move" -> nonEmptyText
|
||||
)(SignupData.apply)(_ => None)
|
||||
.verifying(captchaFailMessage, validateCaptcha _))
|
||||
|
||||
val mobile = Form(mapping(
|
||||
"username" -> username,
|
||||
"password" -> text(minLength = 4))(MobileSignupData.apply)(_ => None))
|
||||
|
||||
def websiteWithCaptcha = withCaptcha(website)
|
||||
}
|
||||
|
||||
val passwordReset = Form(mapping(
|
||||
"email" -> Forms.email,
|
||||
|
@ -72,9 +79,6 @@ final class DataForm(val captcher: akka.actor.ActorSelection) extends lila.hub.C
|
|||
_.samePasswords
|
||||
))
|
||||
|
||||
private def userExists(data: SignupData) =
|
||||
$count.exists(data.username.toLowerCase)
|
||||
|
||||
private def usernameSucks(u: String) =
|
||||
(lameUsernames exists u.contains) ||
|
||||
(lamePrefixes exists u.startsWith) ||
|
||||
|
@ -125,6 +129,10 @@ object DataForm {
|
|||
gameId: String,
|
||||
move: String)
|
||||
|
||||
case class MobileSignupData(
|
||||
username: String,
|
||||
password: String)
|
||||
|
||||
case class PasswordReset(
|
||||
email: String,
|
||||
gameId: String,
|
||||
|
|
Loading…
Reference in a new issue