From c6e0f64f452c7dffe7b12d5a45422785b4a7cb2b Mon Sep 17 00:00:00 2001 From: Griffin Smith Date: Sun, 19 Dec 2021 00:02:04 -0500 Subject: fix(grfn/bbbg): Properly handle already-existing user 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 Autosubmit: grfn Tested-by: BuildkiteCI --- users/grfn/bbbg/src/bbbg/db/user.clj | 9 +++++++++ users/grfn/bbbg/src/bbbg/handlers/home.clj | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) 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)})] -- cgit 1.4.1