snapframework / snap

Top-level package for the official Snap Framework libraries, includes the snaplets API as well as infrastructure for sessions, auth, and templates.
http://snapframework.com/
BSD 3-Clause "New" or "Revised" License
455 stars 68 forks source link

Accept "on" value for remember me checkbox input value #97

Closed nurpax closed 10 years ago

nurpax commented 10 years ago

Tested by adding a remember checkbox into the project default template login. I verified that a "remember_token" gets saved to a non-null value in the users.json after login.

I've copy&pasted my patch for adding "remember me" functionality into the comments of this pull request.

Accept a value of "on" for login screen's "remember me" input value.
This is what gets sent as default by the browser when an
<input type="checkbox"> is checked.

Previously only "1" was accepted which meant checkboxes would need to
explicitly specify a value="1" in their definition.

Fixes #96
nurpax commented 10 years ago

Here's a patch I used to modify the default project template to add "remember me" checkbox into the login screen. However, I'm not including it in a PR because it doesn't work ideally. It shows the "Remember me?" checkbox for both the login screen and the "add new user" screen. I wanted to leave it out in the "create new user" form, but seems like Heist doesn't allow conditionally leaving out portions of a template without backing it up with some Haskell code. I nowadays define most of my templates client-side w/ Handlebars.js, so I suck at Heist templating.

nurpax@nurpax:~/dev/snap$ git diff
diff --git a/project_template/default/snaplets/heist/templates/userform.tpl b/project_templat
index 0f8a8fc..c0bfcb3 100644
--- a/project_template/default/snaplets/heist/templates/userform.tpl
+++ b/project_template/default/snaplets/heist/templates/userform.tpl
@@ -8,6 +8,14 @@
     </tr>
     <tr>
       <td></td>
+      <td>
+        <label>
+          <input type="checkbox" id="remember" name="remember">&nbsp;Remember me
+        </label>
+      <td>
+    </tr>
+    <tr>
+      <td></td>
       <td><input type="submit" value="${submitText}" /></td>
     </tr>
   </table>
diff --git a/project_template/default/src/Site.hs b/project_template/default/src/Site.hs
index cefdc45..61da460 100644
--- a/project_template/default/src/Site.hs
+++ b/project_template/default/src/Site.hs
@@ -38,7 +38,7 @@ handleLogin authError = heistLocal (I.bindSplices errs) $ render "login"
 -- | Handle login submit
 handleLoginSubmit :: Handler App (AuthManager App) ()
 handleLoginSubmit =
-    loginUser "login" "password" Nothing
+    loginUser "login" "password" (Just "remember")
               (\_ -> handleLogin err) (redirect "/")
   where
     err = Just "Unknown user or password"
nurpax commented 10 years ago

BTW if you skip the template patch in comments, this pull request should be fine to merge.