Add :dev-login for quick dev tasks when OAuth/GitHub app are not

required
This commit is contained in:
Vitaliy Vlasov 2018-04-04 14:23:24 +03:00 committed by Martin Klepsch
parent 4437e13657
commit 9008f675f3
4 changed files with 46 additions and 26 deletions

View File

@ -43,6 +43,13 @@ SELECT *
FROM users FROM users
WHERE id = :id; WHERE id = :id;
-- :name get-user-by-login :? :1
-- :doc retrieve a user given GitHub login.
SELECT *
FROM users
WHERE login = :login;
-- :name get-repo-owner :? :1 -- :name get-repo-owner :? :1
SELECT * SELECT *
FROM users u, repositories r FROM users u, repositories r

View File

@ -21,6 +21,11 @@
(jdbc/with-db-connection [con-db *db*] (jdbc/with-db-connection [con-db *db*]
(db/get-user con-db {:id user-id}))) (db/get-user con-db {:id user-id})))
(defn get-user-by-login
[login]
(jdbc/with-db-connection [con-db *db*]
(db/get-user-by-login con-db {:login login})))
(defn exists? (defn exists?
[user-id] [user-id]
(jdbc/with-db-connection [con-db *db*] (jdbc/with-db-connection [con-db *db*]

View File

@ -23,7 +23,10 @@
:on-testnet? (env :on-testnet)})) :on-testnet? (env :on-testnet)}))
(defn landing-page [] (defn landing-page []
(layout/render "index.html" {:authorize-url (github/signup-authorize-url)})) (layout/render "index.html"
{:authorize-url (if (env :dev-login)
(str (env :server-address) "/callback_dev")
(github/signup-authorize-url))}))
(defn welcome-page [] (defn welcome-page []
(layout/render "welcome.html")) (layout/render "welcome.html"))

View File

@ -35,28 +35,33 @@
(defroutes redirect-routes (defroutes redirect-routes
(GET "/callback" [code state] (GET "/callback" [code state]
(let [resp (github/post-for-token code state) (let [resp (github/post-for-token code state)
body (keywordize-keys (codec/form-decode (:body resp))) body (keywordize-keys (codec/form-decode (:body resp)))
scope (:scope body) scope (:scope body)
access-token (:access_token body)] access-token (:access_token body)]
(log/info "access-token:" access-token) (log/info "access-token:" access-token)
(log/debug "github sign-in callback, response body:" body) (log/debug "github sign-in callback, response body:" body)
(if (:error body) (if (:error body)
;; Why does Mist browser sends two redirects at the same time? The latter results in 401 error. ;; Why does Mist browser sends two redirects at the same time? The latter results in 401 error.
(found (str (env :server-address) "/app")) (found (str (env :server-address) "/app"))
(let [admin-token? (str/includes? scope "repo") (let [admin-token? (str/includes? scope "repo")
token-key (if admin-token? :admin-token :token) token-key (if admin-token? :admin-token :token)
gh-user (github/get-user access-token) gh-user (github/get-user access-token)
new-user? (nil? (users/get-user (:id gh-user 0))) new-user? (nil? (users/get-user (:id gh-user 0)))
user (assoc (get-or-create-user access-token) user (assoc (get-or-create-user access-token)
token-key access-token)] token-key access-token)]
(when (and (hubspot-contact-create-enabled) (when (and (hubspot-contact-create-enabled)
new-user?) new-user?)
(try (try
(hubspot/create-hubspot-contact (:email user) (hubspot/create-hubspot-contact (:email user)
(:name user "") (:name user "")
(:login user)) (:login user))
(catch Throwable t (catch Throwable t
(log/error "Failed to create hubspot contact" t)))) (log/error "Failed to create hubspot contact" t))))
(assoc (found (str (env :server-address) "/app")) (assoc (found (str (env :server-address) "/app"))
:session {:identity user})))))) :session {:identity user})))))
(GET "/callback_dev" []
(assoc (found (str (env :server-address) "/app"))
:session {:identity (users/get-user-by-login (env :dev-login))}))
)