diff --git a/resources/sql/queries.sql b/resources/sql/queries.sql index 4f866af..81316d8 100644 --- a/resources/sql/queries.sql +++ b/resources/sql/queries.sql @@ -43,6 +43,13 @@ SELECT * FROM users 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 SELECT * FROM users u, repositories r diff --git a/src/clj/commiteth/db/users.clj b/src/clj/commiteth/db/users.clj index 488a5db..20b4ff8 100644 --- a/src/clj/commiteth/db/users.clj +++ b/src/clj/commiteth/db/users.clj @@ -21,6 +21,11 @@ (jdbc/with-db-connection [con-db *db*] (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? [user-id] (jdbc/with-db-connection [con-db *db*] diff --git a/src/clj/commiteth/routes/home.clj b/src/clj/commiteth/routes/home.clj index 1230d13..1702369 100644 --- a/src/clj/commiteth/routes/home.clj +++ b/src/clj/commiteth/routes/home.clj @@ -23,7 +23,10 @@ :on-testnet? (env :on-testnet)})) (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 [] (layout/render "welcome.html")) diff --git a/src/clj/commiteth/routes/redirect.clj b/src/clj/commiteth/routes/redirect.clj index 6aaaf2e..654d5fa 100644 --- a/src/clj/commiteth/routes/redirect.clj +++ b/src/clj/commiteth/routes/redirect.clj @@ -35,28 +35,33 @@ (defroutes redirect-routes (GET "/callback" [code state] - (let [resp (github/post-for-token code state) - body (keywordize-keys (codec/form-decode (:body resp))) - scope (:scope body) - access-token (:access_token body)] - (log/info "access-token:" access-token) - (log/debug "github sign-in callback, response body:" body) - (if (:error body) - ;; Why does Mist browser sends two redirects at the same time? The latter results in 401 error. - (found (str (env :server-address) "/app")) - (let [admin-token? (str/includes? scope "repo") - token-key (if admin-token? :admin-token :token) - gh-user (github/get-user access-token) - new-user? (nil? (users/get-user (:id gh-user 0))) - user (assoc (get-or-create-user access-token) - token-key access-token)] - (when (and (hubspot-contact-create-enabled) - new-user?) - (try - (hubspot/create-hubspot-contact (:email user) - (:name user "") - (:login user)) - (catch Throwable t - (log/error "Failed to create hubspot contact" t)))) - (assoc (found (str (env :server-address) "/app")) - :session {:identity user})))))) + (let [resp (github/post-for-token code state) + body (keywordize-keys (codec/form-decode (:body resp))) + scope (:scope body) + access-token (:access_token body)] + (log/info "access-token:" access-token) + (log/debug "github sign-in callback, response body:" body) + (if (:error body) + ;; Why does Mist browser sends two redirects at the same time? The latter results in 401 error. + (found (str (env :server-address) "/app")) + (let [admin-token? (str/includes? scope "repo") + token-key (if admin-token? :admin-token :token) + gh-user (github/get-user access-token) + new-user? (nil? (users/get-user (:id gh-user 0))) + user (assoc (get-or-create-user access-token) + token-key access-token)] + (when (and (hubspot-contact-create-enabled) + new-user?) + (try + (hubspot/create-hubspot-contact (:email user) + (:name user "") + (:login user)) + (catch Throwable t + (log/error "Failed to create hubspot contact" t)))) + (assoc (found (str (env :server-address) "/app")) + :session {:identity user}))))) + (GET "/callback_dev" [] + (assoc (found (str (env :server-address) "/app")) + :session {:identity (users/get-user-by-login (env :dev-login))})) + + )