about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGriffin Smith <grfn@gws.fyi>2021-12-19T05·02-0500
committerclbot <clbot@tvl.fyi>2021-12-19T05·06+0000
commitc6e0f64f452c7dffe7b12d5a45422785b4a7cb2b (patch)
treef8a5a843a0f86dc6d698c19e34575a2045d97322
parent2bc742964163217982d43d74e4a06968de09d67b (diff)
fix(grfn/bbbg): Properly handle already-existing user r/3299
If a user already exists with the discord ID we just authenticated, just
return it rather than trying to create another user.

Change-Id: I582c4c8c55f6fb399295b3e691b0bc59e77151f4
Reviewed-on: https://cl.tvl.fyi/c/depot/+/4410
Reviewed-by: grfn <grfn@gws.fyi>
Autosubmit: grfn <grfn@gws.fyi>
Tested-by: BuildkiteCI
-rw-r--r--users/grfn/bbbg/src/bbbg/db/user.clj9
-rw-r--r--users/grfn/bbbg/src/bbbg/handlers/home.clj2
2 files changed, 10 insertions, 1 deletions
diff --git a/users/grfn/bbbg/src/bbbg/db/user.clj b/users/grfn/bbbg/src/bbbg/db/user.clj
index 7db73e378d91..700105ef6350 100644
--- a/users/grfn/bbbg/src/bbbg/db/user.clj
+++ b/users/grfn/bbbg/src/bbbg/db/user.clj
@@ -8,3 +8,12 @@
               (select-keys attrs [::user/id
                                   ::user/username
                                   ::user/discord-user-id])))
+
+(defn find-or-create! [db attrs]
+  (or
+   (db/fetch db {:select [:*]
+                 :from [:public.user]
+                 :where [:=
+                         :discord-user-id
+                         (::user/discord-user-id attrs)]})
+   (create! db attrs)))
diff --git a/users/grfn/bbbg/src/bbbg/handlers/home.clj b/users/grfn/bbbg/src/bbbg/handlers/home.clj
index 480706574569..4b46675e4c34 100644
--- a/users/grfn/bbbg/src/bbbg/handlers/home.clj
+++ b/users/grfn/bbbg/src/bbbg/handlers/home.clj
@@ -35,7 +35,7 @@
      (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!
+               user (db.user/find-or-create!
                      db
                      #::user{:username (:username discord-user)
                              :discord-user-id (:id discord-user)})]