about summary refs log tree commit diff
path: root/users/grfn/bbbg/src/bbbg/handlers/home.clj
diff options
context:
space:
mode:
authorGriffin Smith <grfn@gws.fyi>2021-12-19T04·37-0500
committerclbot <clbot@tvl.fyi>2021-12-19T04·43+0000
commit2bc742964163217982d43d74e4a06968de09d67b (patch)
tree20094a736d75e839c6689de8ae79cb50af3813c5 /users/grfn/bbbg/src/bbbg/handlers/home.clj
parent1205b42ee0436287fea654510db9323e8d59a395 (diff)
feat(grfn/bbbg): Allow Organizers to sign in via Discord r/3298
Allow users with the Organizers role to sign in via a Discord Oauth2
handshake, creating a user in the users table and adding the ID of that
user to the session.

Change-Id: I39d9e17433e71b07314b9eabb787fb9214289772
Reviewed-on: https://cl.tvl.fyi/c/depot/+/4409
Tested-by: BuildkiteCI
Reviewed-by: grfn <grfn@gws.fyi>
Autosubmit: grfn <grfn@gws.fyi>
Diffstat (limited to 'users/grfn/bbbg/src/bbbg/handlers/home.clj')
-rw-r--r--users/grfn/bbbg/src/bbbg/handlers/home.clj46
1 files changed, 39 insertions, 7 deletions
diff --git a/users/grfn/bbbg/src/bbbg/handlers/home.clj b/users/grfn/bbbg/src/bbbg/handlers/home.clj
index d5ba72878a..4807065745 100644
--- a/users/grfn/bbbg/src/bbbg/handlers/home.clj
+++ b/users/grfn/bbbg/src/bbbg/handlers/home.clj
@@ -1,17 +1,49 @@
 (ns bbbg.handlers.home
   (:require
+   [bbbg.db.user :as db.user]
+   [bbbg.discord.auth :as discord.auth]
    [bbbg.handlers.core :refer [page-response]]
-   [compojure.core :refer [GET routes]]))
+   [bbbg.user :as user]
+   [bbbg.views.flash :as flash]
+   [compojure.core :refer [GET routes]]
+   [ring.util.response :refer [redirect]]
+   [bbbg.discord :as discord]))
 
-(defn- home-page []
+(defn- home-page [{:keys [authenticated?]}]
   [:nav.home-nav
    [:ul
     [:li [:a {:href "/signup-forms"}
           "Event Signup Form"]]
-    [:li [:a {:href "/login"}
-          "Sign In"]]]])
+    (when-not authenticated?
+      [:li [:a {:href "/auth/discord"}
+            "Sign In"]])]])
 
-(defn home-routes [_env]
+(defn auth-failure []
+  [:div.auth-failure
+   [:p
+    "Sorry, only users with the Organizers role in discord can sign in"]
+   [:p
+    [:a {:href "/"} "Go Back"]]])
+
+(defn home-routes [{:keys [db] :as env}]
   (routes
-   (GET "/" []
-     (page-response (home-page)))))
+   (GET "/" request
+     (let [authenticated? (some? (get-in request [:session ::user/id]))]
+       (page-response (home-page {:authenticated? authenticated?}))))
+
+   (GET "/auth/success" request
+     (let [token (get-in request [:oauth2/access-tokens :discord])]
+       (if (discord.auth/check-discord-auth env token)
+         (let [discord-user (discord/me token)
+               user (db.user/create!
+                     db
+                     #::user{:username (:username discord-user)
+                             :discord-user-id (:id discord-user)})]
+           (-> (redirect "/")
+               (assoc-in [:session ::user/id] (::user/id user))
+               (flash/add-flash
+                {:flash/message "Successfully Signed In"
+                 :flash/type :success})))
+         (->
+          (page-response (auth-failure))
+          (assoc :status 401)))))))