@@ -87,15 +87,16 @@ func SignInByEmail() web.HandlerFunc {
8787 // Only send code if user exists
8888 if userExists {
8989 err := bus .Dispatch (c , & cmd.SaveVerificationKey {
90- Key : action .VerificationCode ,
90+ Key : action .LinkKey ,
91+ Code : action .VerificationCode ,
9192 Duration : 15 * time .Minute ,
9293 Request : action ,
9394 })
9495 if err != nil {
9596 return c .Failure (err )
9697 }
9798
98- c .Enqueue (tasks .SendSignInEmail (action .Email , action .VerificationCode ))
99+ c .Enqueue (tasks .SendSignInEmail (action .Email , action .LinkKey , action . VerificationCode ))
99100 }
100101
101102 return c .Ok (web.Map {
@@ -129,15 +130,16 @@ func SignInByEmailWithName() web.HandlerFunc {
129130
130131 // Save verification with name
131132 err = bus .Dispatch (c , & cmd.SaveVerificationKey {
132- Key : action .VerificationCode ,
133+ Key : action .LinkKey ,
134+ Code : action .VerificationCode ,
133135 Duration : 15 * time .Minute ,
134136 Request : action ,
135137 })
136138 if err != nil {
137139 return c .Failure (err )
138140 }
139141
140- c .Enqueue (tasks .SendSignInEmail (action .Email , action .VerificationCode ))
142+ c .Enqueue (tasks .SendSignInEmail (action .Email , action .LinkKey , action . VerificationCode ))
141143
142144 return c .Ok (web.Map {})
143145 }
@@ -169,18 +171,20 @@ func VerifySignInCode() web.HandlerFunc {
169171
170172 result := verification .Result
171173
172- // Check if already verified (with grace period)
174+ // Code is single-use: reject if already verified
173175 if result .VerifiedAt != nil {
174- if time .Since (* result .VerifiedAt ) > 5 * time .Minute {
175- return c .Gone ()
176- }
177- } else {
178- // Check if expired
179- if time .Now ().After (result .ExpiresAt ) {
180- // Mark as verified to prevent reuse
181- _ = bus .Dispatch (c , & cmd.SetKeyAsVerified {Key : action .Code })
182- return c .Gone ()
183- }
176+ return c .BadRequest (web.Map {
177+ "code" : "Invalid or expired verification code" ,
178+ })
179+ }
180+
181+ // Check if expired
182+ if time .Now ().After (result .ExpiresAt ) {
183+ // Mark as verified to prevent reuse
184+ _ = bus .Dispatch (c , & cmd.SetKeyAsVerified {Key : result .Key })
185+ return c .BadRequest (web.Map {
186+ "code" : "Invalid or expired verification code" ,
187+ })
184188 }
185189
186190 // Check if user exists
@@ -207,7 +211,7 @@ func VerifySignInCode() web.HandlerFunc {
207211 }
208212
209213 // Mark code as verified
210- err = bus .Dispatch (c , & cmd.SetKeyAsVerified {Key : action . Code })
214+ err = bus .Dispatch (c , & cmd.SetKeyAsVerified {Key : result . Key })
211215 if err != nil {
212216 return c .Failure (err )
213217 }
@@ -226,7 +230,7 @@ func VerifySignInCode() web.HandlerFunc {
226230 }
227231
228232 // Mark code as verified
229- err = bus .Dispatch (c , & cmd.SetKeyAsVerified {Key : action . Code })
233+ err = bus .Dispatch (c , & cmd.SetKeyAsVerified {Key : result . Key })
230234 if err != nil {
231235 return c .Failure (err )
232236 }
@@ -254,7 +258,8 @@ func ResendSignInCode() web.HandlerFunc {
254258
255259 // Save new verification code
256260 err := bus .Dispatch (c , & cmd.SaveVerificationKey {
257- Key : action .VerificationCode ,
261+ Key : action .LinkKey ,
262+ Code : action .VerificationCode ,
258263 Duration : 15 * time .Minute ,
259264 Request : action ,
260265 })
@@ -263,7 +268,7 @@ func ResendSignInCode() web.HandlerFunc {
263268 }
264269
265270 // Send new email
266- c .Enqueue (tasks .SendSignInEmail (action .Email , action .VerificationCode ))
271+ c .Enqueue (tasks .SendSignInEmail (action .Email , action .LinkKey , action . VerificationCode ))
267272
268273 return c .Ok (web.Map {})
269274 }
0 commit comments