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 <grfn@gws.fyi> Autosubmit: grfn <grfn@gws.fyi> Tested-by: BuildkiteCI
This commit is contained in:
		
							parent
							
								
									2bc7429641
								
							
						
					
					
						commit
						c6e0f64f45
					
				
					 2 changed files with 10 additions and 1 deletions
				
			
		| 
						 | 
				
			
			@ -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)))
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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)})]
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue