Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (import base16)
- (import ./redis :as redis)
- (import ./pq :as pq)
- #(import ./pgjobq :as pgjobq)
- (import ./mailer :as mailer)
- (defn secure-code
- []
- (->
- (os/cryptorand 16)
- (base16/encode)))
- (def- SIGNUP-EXPIRY (string (* 2 60 60 24) ))
- (defn- account-with-email-exists?
- [email]
- (truthy? (pq/val "select 1 from accounts where primaryemail ilike $1" email)))
- (defn begin-signup
- "
- Create an account signup code, registering it in
- the redis database.
- If the account already exists then returns nil.
- Otherwise returns a secure code for completing account creation.
- "
- [email]
- (if (account-with-email-exists? email)
- nil
- (do
- (def code (secure-code))
- (def key (string "signup/" code))
- (redis/multi
- ["SET" key (marshal @{:email email})]
- ["EXPIRE" key SIGNUP-EXPIRY])
- code)))
- (defn confirm-signup
- "
- Confirm an account signup.
- Returns one of:
- :expired-signup
- :account-exists
- id
- "
- [code]
- (label _
- (def key (string "signup/" code))
- (def info (-?>
- (redis/command "GET" key) (unmarshal)))
- (unless info
- (return _ :expired-signup))
- (def id
- (pq/txn []
- (when (account-with-email-exists? (info :email))
- (return _ :account-exists))
- (pq/val "insert into accounts(primaryemail) values($1) returning id;" (info :email))))
- (redis/command "DEL" key)
- id))
- # repl helpers
- # (redis/connect "../dev-state/run/redis.sock")
- # (pq/connect "host=localhost dbname=postgres")
- # (begin-signup "ac@acha.ninja")
- # (confirm-signup _)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement