              name " has been reset to " pw ". Make sure to change it"
              " soon at https://4clojure.com/login/update - pick"
              " something you'll remember!")})
+      (session/session-put! :login-to "/login/update")
       (flash-msg "Your password has been reset! You should receive an email soon"
                  "/login"))
     (flash-error "We don't know anyone with that email address!"

  (:import java.net.URLEncoder
           org.apache.commons.mail.SimpleEmail))
                        
(def-page my-login-page [location]
  [:div.error (session/flash-get :error)]
  (form-to [:post "/login"]
    [:table
     [:tr

      [:td]
      [:td [:button {:type "submit"} "Log In"]]]
     [:tr
      [:td]
      [:td
       [:a {:href "/login/reset"} "Forgot your password?"]]]]))

(defn do-login [user pwd]
  (let [user (.toLowerCase user)
        {db-pwd :pwd} (from-mongo (fetch-one :users :where {:user user}))
        location (session/session-get :login-to)]
    (if (and db-pwd (.checkPassword (StrongPasswordEncryptor.) pwd db-pwd))
      (do (update! :users {:user user}
                   {:$set {:last-login (java.util.Date.)}}
                   :upsert false) ; never create new users accidentally
          (session/session-put! :user user)
          (session/session-delete-key! :login-to)
          (response/redirect (or location "/problems")))
      (flash-error "Error logging in." "/login"))))

(def-page update-password-page []
  (with-user [{:keys [user]}]
