From 934dbef44c714695b65e2c81b74eb65ef7962479 Mon Sep 17 00:00:00 2001 From: Dominik Picheta Date: Wed, 23 May 2018 14:00:49 +0100 Subject: [PATCH] Add recaptcha to reset password modal. --- nimforum.nimble | 8 ++++++-- src/forum.nim | 2 +- src/frontend/header.nim | 3 +-- src/frontend/login.nim | 4 ++-- src/frontend/resetpassword.nim | 11 ++++++++++- 5 files changed, 20 insertions(+), 8 deletions(-) diff --git a/nimforum.nimble b/nimforum.nimble index 29df381..2cf1271 100644 --- a/nimforum.nimble +++ b/nimforum.nimble @@ -41,14 +41,18 @@ task frontend, "Builds the necessary JS frontend (with CSS)": task minify, "Minifies the JS using Google's closure compiler": exec "closure-compiler public/js/forum.js --js_output_file public/js/forum.js.opt" -task testdb, "Creates a test DB": +task testdb, "Creates a test DB (with admin account!)": exec "nimble c src/setup_nimforum" exec "./src/setup_nimforum --test" -task devdb, "Creates a test DB": +task devdb, "Creates a test DB (with admin account!)": exec "nimble c src/setup_nimforum" exec "./src/setup_nimforum --dev" +task blankdb, "Creates a blank DB": + exec "nimble c src/setup_nimforum" + exec "./src/setup_nimforum --blank" + task test, "Runs tester": exec "nimble c -y src/forum.nim" exec "nimble c -y -r tests/browsertester" diff --git a/src/forum.nim b/src/forum.nim index 416436e..a197fcc 100644 --- a/src/forum.nim +++ b/src/forum.nim @@ -1290,7 +1290,7 @@ routes: if "g-recaptcha-response" notin formData: let err = PostError( errorFields: @[], - message: "Not logged in." + message: "Not logged in and no recaptcha." ) resp Http401, $(%err), "application/json" diff --git a/src/frontend/header.nim b/src/frontend/header.nim index d977771..fc16941 100644 --- a/src/frontend/header.nim +++ b/src/frontend/header.nim @@ -113,7 +113,6 @@ when defined(js): render(state.userMenu, user.get()) # Modals - render(state.loginModal) - if state.data.isSome(): + render(state.loginModal, state.data.get().recaptchaSiteKey) render(state.signupModal, state.data.get().recaptchaSiteKey) \ No newline at end of file diff --git a/src/frontend/login.nim b/src/frontend/login.nim index 2c413dd..f0779c7 100644 --- a/src/frontend/login.nim +++ b/src/frontend/login.nim @@ -53,7 +53,7 @@ when defined(js): if event.key == "Enter": onLogInClick(e, n, state) - proc render*(state: LoginModal): VNode = + proc render*(state: LoginModal, recaptchaSiteKey: Option[string]): VNode = result = buildHtml(tdiv()): tdiv(class=class({"active": state.shown}, "modal modal-sm"), id="login-modal"): @@ -93,4 +93,4 @@ when defined(js): (state.onSignUp(); state.shown = false)): text "Create account" - render(state.resetPasswordModal) \ No newline at end of file + render(state.resetPasswordModal, recaptchaSiteKey) \ No newline at end of file diff --git a/src/frontend/resetpassword.nim b/src/frontend/resetpassword.nim index 1313f20..c2f01a1 100644 --- a/src/frontend/resetpassword.nim +++ b/src/frontend/resetpassword.nim @@ -89,6 +89,8 @@ when defined(js): ajaxPost(uri, @[], cast[cstring](formData), (s: int, r: kstring) => onPost(s, r, state)) + ev.preventDefault() + proc onClose(ev: Event, n: VNode, state: ResetPasswordModal) = state.shown = false ev.preventDefault() @@ -106,7 +108,8 @@ when defined(js): if event.key == "Enter": onClick(e, n, state) - proc render*(state: ResetPasswordModal): VNode = + proc render*(state: ResetPasswordModal, + recaptchaSiteKey: Option[string]): VNode = result = buildHtml(): tdiv(class=class({"active": state.shown}, "modal"), id="resetpassword-modal"): @@ -132,6 +135,11 @@ when defined(js): true, placeholder="Username or email" ) + if recaptchaSiteKey.isSome: + tdiv(id="recaptcha"): + tdiv(class="g-recaptcha", + "data-sitekey"=recaptchaSiteKey.get()) + script(src="https://www.google.com/recaptcha/api.js") tdiv(class="modal-footer"): if state.sent: span(class="text-success"): @@ -142,5 +150,6 @@ when defined(js): {"loading": state.loading}, "btn btn-primary" ), + `type`="button", onClick=(ev: Event, n: VNode) => onClick(ev, n, state)): text "Reset password" \ No newline at end of file