@@ -26,47 +26,50 @@ export const verifyOtpAction = publicAction
26
26
type : 'email' ,
27
27
} ) ;
28
28
29
- if ( ! error ) {
30
- if (
31
- data . user ?. id &&
32
- data . user . email_confirmed_at &&
33
- new Date ( data . user . email_confirmed_at ) . getTime ( ) <
34
- new Date ( ) . getTime ( ) + 1000 * 60 * 1 // 1 minute
35
- ) {
36
- const result = await ctx . authClient
37
- . from ( 'accounts' )
38
- . select ( 'id' )
39
- . eq ( 'user_id' , data . user . id )
40
- . single ( ) ;
29
+ if ( error ) {
30
+ return {
31
+ error : error . message ,
32
+ ok : false ,
33
+ } ;
34
+ }
41
35
42
- if ( result . error ) {
43
- return {
44
- error : result . error . message ,
45
- ok : false ,
46
- } ;
47
- }
36
+ // Check if this is a fresh email confirmation (within last 5 minute)
37
+ const isFirstTimeSignIn =
38
+ data . user ?. last_sign_in_at &&
39
+ data . user . email_confirmed_at &&
40
+ new Date ( data . user . email_confirmed_at ) . getTime ( ) + 5 * 60 * 1000 > // within five minutes
41
+ new Date ( data . user . last_sign_in_at ) . getTime ( ) ;
48
42
49
- await sendEmail ( {
50
- accountId : result . data . id ,
51
- subject : 'Welcome to DS Pro' ,
52
- template : {
53
- key : 'welcome' ,
54
- props : {
55
- staticPathUrl : `${ config . pageUrl } /static/email` ,
56
- } ,
57
- } ,
58
- scheduledAt : 'in 5 minutes' ,
59
- } ) ;
43
+ if ( data . user && isFirstTimeSignIn ) {
44
+ const result = await ctx . authClient
45
+ . from ( 'accounts' )
46
+ . select ( 'id' )
47
+ . eq ( 'user_id' , data . user . id )
48
+ . single ( ) ;
60
49
61
- await scheduleOnboardingEmails ( result . data . id ) ;
50
+ if ( result . error ) {
62
51
return {
63
- ok : true ,
52
+ error : 'Error with the user account' ,
53
+ ok : false ,
64
54
} ;
65
55
}
66
- }
67
56
68
- return {
69
- error : error ?. message ?? 'Error verifying OTP' ,
70
- ok : false ,
71
- } ;
57
+ await sendEmail ( {
58
+ accountId : result . data . id ,
59
+ subject : 'Welcome to DS Pro' ,
60
+ template : {
61
+ key : 'welcome' ,
62
+ props : {
63
+ staticPathUrl : `${ config . pageUrl } /static/email` ,
64
+ } ,
65
+ } ,
66
+ scheduledAt : 'in 5 minutes' ,
67
+ } ) ;
68
+
69
+ await scheduleOnboardingEmails ( result . data . id ) ;
70
+
71
+ return {
72
+ ok : true ,
73
+ } ;
74
+ }
72
75
} ) ;
0 commit comments