@@ -2,6 +2,7 @@ import { TRPCError } from '@trpc/server'
22import { formatInTimeZone } from 'date-fns-tz'
33
44import { getBaseUrl } from '~/utils/getBaseUrl'
5+ import { normaliseEmail } from '~/utils/zod'
56import { env } from '~/env.mjs'
67import { sendMail } from '~/lib/mail'
78import {
@@ -10,6 +11,7 @@ import {
1011} from '~/schemas/auth/email/sign-in'
1112import { publicProcedure , router } from '~/server/trpc'
1213import { defaultMeSelect } from '../../me/me.select'
14+ import { AccountProvider } from '../auth.constants'
1315import { VerificationError } from '../auth.error'
1416import { verifyToken } from '../auth.service'
1517import { createTokenHash , createVfnPrefix , createVfnToken } from '../auth.util'
@@ -81,14 +83,32 @@ export const emailSessionRouter = router({
8183
8284 const emailName = email . split ( '@' ) [ 0 ] ?? 'unknown'
8385
84- const user = await ctx . prisma . user . upsert ( {
85- where : { email } ,
86- update : { } ,
87- create : {
88- email,
89- name : emailName ,
90- } ,
91- select : defaultMeSelect ,
86+ const user = await ctx . prisma . $transaction ( async ( tx ) => {
87+ const user = await tx . user . upsert ( {
88+ where : { email } ,
89+ update : { } ,
90+ create : {
91+ email,
92+ name : emailName ,
93+ } ,
94+ select : defaultMeSelect ,
95+ } )
96+
97+ await tx . accounts . upsert ( {
98+ where : {
99+ provider_providerAccountId : {
100+ provider : AccountProvider . Email ,
101+ providerAccountId : normaliseEmail . parse ( email ) ,
102+ } ,
103+ } ,
104+ update : { } ,
105+ create : {
106+ provider : AccountProvider . Email ,
107+ providerAccountId : normaliseEmail . parse ( email ) ,
108+ userId : user . id ,
109+ } ,
110+ } )
111+ return user
92112 } )
93113
94114 ctx . session . userId = user . id
0 commit comments