@@ -45,17 +45,30 @@ export async function doLogin(payload: VerifyLoginPayloadParams) {
45
45
throw new Error ( "API_SERVER_SECRET is not set" ) ;
46
46
}
47
47
48
+ const cookieStore = cookies ( ) ;
49
+
48
50
// forward the request to the API server
49
51
const res = await fetch ( `${ THIRDWEB_API_HOST } /v2/siwe/login` , {
50
52
method : "POST" ,
51
53
headers : {
52
54
"Content-Type" : "application/json" ,
53
55
"x-service-api-key" : THIRDWEB_API_SECRET ,
54
56
} ,
55
- body : JSON . stringify ( payload ) ,
57
+ // set the createAccount flag to true to create a new account if it does not exist
58
+ body : JSON . stringify ( { ...payload , createAccount : true } ) ,
56
59
} ) ;
57
60
61
+ // if the request failed, log the error and throw an error
58
62
if ( ! res . ok ) {
63
+ try {
64
+ // clear the cookies to prevent any weird issues
65
+ cookieStore . delete (
66
+ COOKIE_PREFIX_TOKEN + getAddress ( payload . payload . address ) ,
67
+ ) ;
68
+ cookieStore . delete ( COOKIE_ACTIVE_ACCOUNT ) ;
69
+ } catch {
70
+ // ignore any errors on this
71
+ }
59
72
try {
60
73
const response = await res . text ( ) ;
61
74
// try to log the rich error message
@@ -86,36 +99,6 @@ export async function doLogin(payload: VerifyLoginPayloadParams) {
86
99
throw new Error ( "Failed to login - invalid json" ) ;
87
100
}
88
101
89
- // check if we have a thirdweb account for the token
90
- const userRes = await fetch ( `${ THIRDWEB_API_HOST } /v1/account/me` , {
91
- method : "GET" ,
92
- headers : {
93
- "Content-Type" : "application/json" ,
94
- Authorization : `Bearer ${ jwt } ` ,
95
- } ,
96
- } ) ;
97
- // if we do not have a user, create one
98
- // TODO: this should only need to check for 404, but because of secretKey auth it can also be 400 error
99
- if ( userRes . status === 404 || userRes . status === 400 ) {
100
- const newUserRes = await fetch ( `${ THIRDWEB_API_HOST } /v1/account/create` , {
101
- method : "POST" ,
102
- headers : {
103
- Authorization : `Bearer ${ jwt } ` ,
104
- } ,
105
- } ) ;
106
-
107
- if ( newUserRes . status !== 200 ) {
108
- console . error (
109
- "Failed to create new user" ,
110
- newUserRes . status ,
111
- newUserRes . statusText ,
112
- ) ;
113
- throw new Error ( "Failed to create new user" ) ;
114
- }
115
- }
116
-
117
- const cookieStore = cookies ( ) ;
118
-
119
102
// set the token cookie
120
103
cookieStore . set (
121
104
COOKIE_PREFIX_TOKEN + getAddress ( payload . payload . address ) ,
0 commit comments