Skip to content

Commit

Permalink
Use com.biffweb/authentication-plugin in the example project
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobobryant committed Feb 18, 2023
1 parent 76accae commit 6a5ba0a
Show file tree
Hide file tree
Showing 9 changed files with 284 additions and 208 deletions.
16 changes: 13 additions & 3 deletions example/src/com/example.clj
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
(ns com.example
(:require [com.biffweb :as biff]
[com.example.email :as email]
[com.example.feat.app :as app]
[com.example.feat.auth :as auth]
[com.example.feat.home :as home]
[com.example.feat.worker :as worker]
[com.example.schema :refer [malli-opts]]
[com.example.schema :as schema]
[clojure.test :as test]
[clojure.tools.logging :as log]
[malli.core :as malc]
[malli.registry :as malr]
[nrepl.cmdline :as nrepl-cmd]))

(def features
[app/features
auth/features
(biff/authentication-plugin {})
home/features
schema/features
worker/features])

(def routes [["" {:middleware [biff/wrap-site-defaults]}
Expand All @@ -36,6 +39,12 @@
(generate-assets! sys)
(test/run-all-tests #"com.example.test.*"))

(def malli-opts
{:registry (malr/composite-registry
malc/default-registry
(apply biff/safe-merge
(keep :schema features)))})

(def components
[biff/use-config
biff/use-secrets
Expand All @@ -52,6 +61,7 @@
(defn start []
(let [ctx (biff/start-system
{:com.example/chat-clients (atom #{})
:biff/send-email #'email/send-email
:biff/features #'features
:biff/after-refresh `start
:biff/handler #'handler
Expand Down
90 changes: 90 additions & 0 deletions example/src/com/example/email.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
(ns com.example.email
(:require [camel-snake-kebab.core :as csk]
[camel-snake-kebab.extras :as cske]
[clj-http.client :as http]
[com.example.settings :as settings]
[clojure.tools.logging :as log]
[rum.core :as rum]))

(defn signin-link [{:keys [to url user-exists]}]
{:to to
:subject (if user-exists
(str "Sign in to " settings/app-name)
(str "Sign up for " settings/app-name))
:html-body (rum/render-static-markup
[:html
[:body
[:p "We received a request to sign in to " settings/app-name
" using this email address. Click this link to sign in:"]
[:p [:a {:href url :target "_blank"} "Click here to sign in."]]
[:p "This link will expire in one hour. "
"If you did not request this link, you can ignore this email."]]])
:text-body (str "We received a request to sign in to " settings/app-name
" using this email address. Click this link to sign in:\n"
"\n"
url "\n"
"\n"
"This link will expire in one hour. If you did not request this link, "
"you can ignore this email.")
:message-stream "outbound"})

(defn signin-code [{:keys [to code user-exists]}]
{:to to
:subject (if user-exists
(str "Sign in to " settings/app-name)
(str "Sign up for " settings/app-name))
:html-body (rum/render-static-markup
[:html
[:body
[:p "We received a request to sign in to " settings/app-name
" using this email address. Enter the following code to sign in:"]
[:p {:style {:font-size "2rem"}} code]
[:p
"This code will expire in three minutes. "
"If you did not request this code, you can ignore this email."]]])
:text-body (str "We received a request to sign in to " settings/app-name
" using this email address. Enter the following code to sign in:\n"
"\n"
code "\n"
"\n"
"This code will expire in three minutes. If you did not request this code, "
"you can ignore this email.")
:message-stream "outbound"})

(defn template [k opts]
((case k
:signin-link signin-link
:signin-code signin-code)
opts))

(defn send-postmark [{:keys [biff/secret postmark/from]} form-params]
(let [result (http/post "https://api.postmarkapp.com/email"
{:headers {"X-Postmark-Server-Token" (secret :postmark/api-key)}
:as :json
:content-type :json
:form-params (merge {:from from} (cske/transform-keys csk/->PascalCase form-params))
:throw-exceptions false})
success (< (:status result) 400)]
(when-not success
(log/error (:body result)))
success))

(defn send-console [ctx form-params]
(println "TO:" (:to form-params))
(println "SUBJECT:" (:subject form-params))
(println)
(println (:text-body form-params))
(println)
(println "To send emails instead of printing them to the console, add your"
"API keys for Postmark and Recaptcha to config.edn.")
true)

(defn send-email [{:keys [biff/secret recaptcha/site-key] :as ctx} opts]
(let [form-params (if-some [template-key (:template opts)]
(template template-key opts)
opts)]
(if (every? some? [(secret :postmark/api-key)
(secret :recaptcha/secret-key)
site-key])
(send-postmark ctx form-params)
(send-console ctx form-params))))
121 changes: 0 additions & 121 deletions example/src/com/example/feat/auth.clj

This file was deleted.

Loading

0 comments on commit 6a5ba0a

Please sign in to comment.