@@ -42,37 +42,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
42
42
return ;
43
43
}
44
44
45
- // There is an existingUser if the username matches
46
- // OR if the email matches AND either the email is verified
47
- // or both username and password are set
48
- const existingUser = await prisma . user . findFirst ( {
49
- where : {
50
- OR : [
51
- { username } ,
52
- {
53
- AND : [
54
- { email : userEmail } ,
55
- {
56
- OR : [
57
- { emailVerified : { not : null } } ,
58
- {
59
- AND : [ { password : { not : null } } , { username : { not : null } } ] ,
60
- } ,
61
- ] ,
62
- } ,
63
- ] ,
64
- } ,
65
- ] ,
66
- } ,
67
- } ) ;
68
-
69
- if ( existingUser ) {
70
- const message : string =
71
- existingUser . email !== userEmail ? "Username already taken" : "Email address is already registered" ;
72
-
73
- return res . status ( 409 ) . json ( { message } ) ;
74
- }
75
-
76
45
let foundToken : { id : number ; teamId : number | null ; expires : Date } | null = null ;
77
46
if ( token ) {
78
47
foundToken = await prisma . verificationToken . findFirst ( {
@@ -100,6 +69,36 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
100
69
return res . status ( 409 ) . json ( { message : "Username already taken" } ) ;
101
70
}
102
71
}
72
+ } else {
73
+ // There is an existingUser if the username matches
74
+ // OR if the email matches AND either the email is verified
75
+ // or both username and password are set
76
+ const existingUser = await prisma . user . findFirst ( {
77
+ where : {
78
+ OR : [
79
+ { username } ,
80
+ {
81
+ AND : [
82
+ { email : userEmail } ,
83
+ {
84
+ OR : [
85
+ { emailVerified : { not : null } } ,
86
+ {
87
+ AND : [ { password : { not : null } } , { username : { not : null } } ] ,
88
+ } ,
89
+ ] ,
90
+ } ,
91
+ ] ,
92
+ } ,
93
+ ] ,
94
+ } ,
95
+ } ) ;
96
+ if ( existingUser ) {
97
+ const message : string =
98
+ existingUser . email !== userEmail ? "Username already taken" : "Email address is already registered" ;
99
+
100
+ return res . status ( 409 ) . json ( { message } ) ;
101
+ }
103
102
}
104
103
105
104
const hashedPassword = await hashPassword ( password ) ;
0 commit comments