Skip to content

Instantly share code, notes, and snippets.

@dadair-ca
Created August 19, 2015 20:19
Show Gist options
  • Save dadair-ca/ba9b80122cbf044bd0f6 to your computer and use it in GitHub Desktop.
Save dadair-ca/ba9b80122cbf044bd0f6 to your computer and use it in GitHub Desktop.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; handlers
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn- login
"Verifies that the user credentials are valid and returns a token
that can be used for future requests to secure resources."
[{{username :username} :body}]
(let [auth (authenticate username)]
(response/ok auth)))
(defn- register
"Creates a user record in the database and sends an activation email
to the user."
[request]
(let [username (get-in request [:body :username])
;; FIXME: these two strings should probably come from some
;; resource file that establishes email templates (da 2015-08-17)
subject "An account has been created for you on ...!"
message "Click here to activate your account."]
#_(email email-service username subject message)
(create-user<! db username)
(response/ok (authenticate username))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; schema
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def ^:private LoginRequest {:body {:username s/Str, :password s/Str}, s/Any s/Any})
(def ^:private RegisterRequest {:body {:username s/Str, :password s/Str}, s/Any s/Any})
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; endpoint
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn accounts-endpoint [{{uri :uri} :db}]
(let [db {:connection-uri uri}]
(routes
(POST "/login" [] (-> login
(wrap-credentials-pass? db)
(wrap-validates? LoginRequest)))
(POST "/users" [] (-> register
(wrap-taken? db)
(wrap-validates? RegisterRequest))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment