@@ -11,6 +11,7 @@ import type {
11
11
LoginPayload ,
12
12
VerifyLoginPayloadParams ,
13
13
} from "thirdweb/auth" ;
14
+ import { isVercel } from "../../lib/vercel-utils" ;
14
15
15
16
export async function getLoginPayload (
16
17
params : GenerateLoginPayloadParams ,
@@ -45,80 +46,83 @@ export async function doLogin(
45
46
throw new Error ( "API_SERVER_SECRET is not set" ) ;
46
47
}
47
48
48
- if ( ! turnstileToken ) {
49
- return {
50
- error : "Missing Turnstile token." ,
51
- } ;
52
- }
49
+ // only validate the turnstile token if we are in a vercel environment
50
+ if ( isVercel ( ) ) {
51
+ if ( ! turnstileToken ) {
52
+ return {
53
+ error : "Missing Turnstile token." ,
54
+ } ;
55
+ }
53
56
54
- // get the request headers
55
- const requestHeaders = await headers ( ) ;
56
- if ( ! requestHeaders ) {
57
- return {
58
- error : "Failed to get request headers. Please try again." ,
59
- } ;
60
- }
61
- // CF header, fallback to req.ip, then X-Forwarded-For
62
- const [ ip , errors ] = ( ( ) => {
63
- let ip : string | null = null ;
64
- const errors : string [ ] = [ ] ;
65
- try {
66
- ip = requestHeaders . get ( "CF-Connecting-IP" ) || null ;
67
- } catch ( err ) {
68
- console . error ( "failed to get IP address from CF-Connecting-IP" , err ) ;
69
- errors . push ( "failed to get IP address from CF-Connecting-IP" ) ;
57
+ // get the request headers
58
+ const requestHeaders = await headers ( ) ;
59
+ if ( ! requestHeaders ) {
60
+ return {
61
+ error : "Failed to get request headers. Please try again." ,
62
+ } ;
70
63
}
71
- if ( ! ip ) {
64
+ // CF header, fallback to req.ip, then X-Forwarded-For
65
+ const [ ip , errors ] = ( ( ) => {
66
+ let ip : string | null = null ;
67
+ const errors : string [ ] = [ ] ;
72
68
try {
73
- ip = ipAddress ( requestHeaders ) || null ;
69
+ ip = requestHeaders . get ( "CF-Connecting-IP" ) || null ;
74
70
} catch ( err ) {
75
- console . error (
76
- "failed to get IP address from ipAddress() function" ,
77
- err ,
78
- ) ;
79
- errors . push ( "failed to get IP address from ipAddress() function" ) ;
71
+ console . error ( "failed to get IP address from CF-Connecting-IP" , err ) ;
72
+ errors . push ( "failed to get IP address from CF-Connecting-IP" ) ;
80
73
}
81
- }
82
- if ( ! ip ) {
83
- try {
84
- ip = requestHeaders . get ( "X-Forwarded-For" ) ;
85
- } catch ( err ) {
86
- console . error ( "failed to get IP address from X-Forwarded-For" , err ) ;
87
- errors . push ( "failed to get IP address from X-Forwarded-For" ) ;
74
+ if ( ! ip ) {
75
+ try {
76
+ ip = ipAddress ( requestHeaders ) || null ;
77
+ } catch ( err ) {
78
+ console . error (
79
+ "failed to get IP address from ipAddress() function" ,
80
+ err ,
81
+ ) ;
82
+ errors . push ( "failed to get IP address from ipAddress() function" ) ;
83
+ }
88
84
}
89
- }
90
- return [ ip , errors ] ;
91
- } ) ( ) ;
85
+ if ( ! ip ) {
86
+ try {
87
+ ip = requestHeaders . get ( "X-Forwarded-For" ) ;
88
+ } catch ( err ) {
89
+ console . error ( "failed to get IP address from X-Forwarded-For" , err ) ;
90
+ errors . push ( "failed to get IP address from X-Forwarded-For" ) ;
91
+ }
92
+ }
93
+ return [ ip , errors ] ;
94
+ } ) ( ) ;
92
95
93
- if ( ! ip ) {
94
- return {
95
- error : "Could not get IP address. Please try again." ,
96
- context : errors ,
97
- } ;
98
- }
96
+ if ( ! ip ) {
97
+ return {
98
+ error : "Could not get IP address. Please try again." ,
99
+ context : errors ,
100
+ } ;
101
+ }
99
102
100
- // https://developers.cloudflare.com/turnstile/get-started/server-side-validation/
101
- // Validate the token by calling the "/siteverify" API endpoint.
102
- const result = await fetch (
103
- "https://challenges.cloudflare.com/turnstile/v0/siteverify" ,
104
- {
105
- body : JSON . stringify ( {
106
- secret : process . env . TURNSTILE_SECRET_KEY ,
107
- response : turnstileToken ,
108
- remoteip : ip ,
109
- } ) ,
110
- method : "POST" ,
111
- headers : {
112
- "Content-Type" : "application/json" ,
103
+ // https://developers.cloudflare.com/turnstile/get-started/server-side-validation/
104
+ // Validate the token by calling the "/siteverify" API endpoint.
105
+ const result = await fetch (
106
+ "https://challenges.cloudflare.com/turnstile/v0/siteverify" ,
107
+ {
108
+ body : JSON . stringify ( {
109
+ secret : process . env . TURNSTILE_SECRET_KEY ,
110
+ response : turnstileToken ,
111
+ remoteip : ip ,
112
+ } ) ,
113
+ method : "POST" ,
114
+ headers : {
115
+ "Content-Type" : "application/json" ,
116
+ } ,
113
117
} ,
114
- } ,
115
- ) ;
118
+ ) ;
116
119
117
- const outcome = await result . json ( ) ;
118
- if ( ! outcome . success ) {
119
- return {
120
- error : "Could not validate captcha." ,
121
- } ;
120
+ const outcome = await result . json ( ) ;
121
+ if ( ! outcome . success ) {
122
+ return {
123
+ error : "Could not validate captcha." ,
124
+ } ;
125
+ }
122
126
}
123
127
124
128
const cookieStore = await cookies ( ) ;
0 commit comments