Skip to content

Instantly share code, notes, and snippets.

/auth.clj Secret

Created June 14, 2017 00:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/2428913e718c68e6bc64cf4c261c12e9 to your computer and use it in GitHub Desktop.
Save anonymous/2428913e718c68e6bc64cf4c261c12e9 to your computer and use it in GitHub Desktop.
(defn handle-registration-error [e]
(if (and
(instance? java.sql.SQLException e)
(-> e (.getNextException)
(.getMessage)
(.startsWith "ERROR: duplicate key value")))
(response/precondition-failed
{:result :error
:message "user with the selected ID already exists"})
(do
(log/error e)
(response/internal-server-error
{:result :error
:message "server error occurred while adding the user"}))))
(defn register! [{:keys [session]} user]
(if (registration-errors user)
(response/precondition-failed {:result :error})
((try
(db/create-user!
(-> user
(dissoc :pass-confirm)
(update :pass hashers/encrypt)))
(-> {:result :ok}
(response/ok)
(assoc :session (assoc session :identity (:id user))))
(catch Exception e
(handle-registration-error e))))))
titaniumbrella.routes.services.auth> (register! {} {:id 1 :first_name "Ping" :last_name "Pong" :email "wamblam84@gmail.com" :pass "notmypassword" :pass-confirm "notmypassword"})
{:status 412, :headers {}, :body {:result :error}}
INSERT INTO users
(id, first_name, last_name, email, pass)
VALUES (:id, :first_name, :last_name, :email, :pass)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment