Skip to content

Commit 9c58c08

Browse files
committed
simplify siwe auth login flow (#4808)
1 parent 587804d commit 9c58c08

File tree

1 file changed

+14
-31
lines changed

1 file changed

+14
-31
lines changed

apps/dashboard/src/app/login/auth-actions.ts

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,30 @@ export async function doLogin(payload: VerifyLoginPayloadParams) {
4545
throw new Error("API_SERVER_SECRET is not set");
4646
}
4747

48+
const cookieStore = cookies();
49+
4850
// forward the request to the API server
4951
const res = await fetch(`${THIRDWEB_API_HOST}/v2/siwe/login`, {
5052
method: "POST",
5153
headers: {
5254
"Content-Type": "application/json",
5355
"x-service-api-key": THIRDWEB_API_SECRET,
5456
},
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 }),
5659
});
5760

61+
// if the request failed, log the error and throw an error
5862
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+
}
5972
try {
6073
const response = await res.text();
6174
// try to log the rich error message
@@ -86,36 +99,6 @@ export async function doLogin(payload: VerifyLoginPayloadParams) {
8699
throw new Error("Failed to login - invalid json");
87100
}
88101

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-
119102
// set the token cookie
120103
cookieStore.set(
121104
COOKIE_PREFIX_TOKEN + getAddress(payload.payload.address),

0 commit comments

Comments
 (0)