Skip to content

Instantly share code, notes, and snippets.

@daguitosama
Last active July 19, 2023 17:25
Show Gist options
  • Save daguitosama/b6001d406ac94e48aee18fb45f9cc094 to your computer and use it in GitHub Desktop.
Save daguitosama/b6001d406ac94e48aee18fb45f9cc094 to your computer and use it in GitHub Desktop.
Dago Wix SDK Client Implementation for use only on a server environment with Remix
export function create_wix_member_api({ client_id, env }: { client_id: string; env: Env }) {
var oauth = OAuthStrategy({
clientId: client_id
});
var client = createClient({
modules: {},
auth: oauth
});
return {
is_authenticated(session: Session<SessionData>): boolean {
const member_tokens_string = session.get("member_tokens");
if (!member_tokens_string) {
return false;
}
const member_tokens = JSON.parse(member_tokens_string) as Tokens;
console.log("()wix_member_api.is_authenticated tokens: ");
console.log(JSON.stringify(member_tokens, null, 2));
// todo make sure the tokens are valid a this point
// Ronny this is the place where it breaks
client.auth.setTokens(member_tokens);
return client.auth.loggedIn();
},
set_tokens(tokens: Tokens) {
client.auth.setTokens(tokens);
},
async get_login_url_oauth_data(): Promise<{
login_url: string;
oauth_data: OauthData;
}> {
var oauth_data = client.auth.generateOAuthData(env.AUTH_REDIRECT_URI);
var { authUrl } = await client.auth.getAuthUrl(oauth_data);
return {
login_url: authUrl,
oauth_data
};
},
async get_member_tokens({
code,
state,
oauthState
}: {
code: string;
state: string;
oauthState: OauthData;
}): Promise<Tokens | null> {
var result: Awaited<ReturnType<typeof client.auth.getMemberTokens>> | null = null;
try {
result = await client.auth.getMemberTokens(code, state, oauthState);
} catch (error) {
console.log("wix_member_api.get_member_tokens Error: ");
console.error(error);
}
return result;
},
/**
* Make sure to only call this method with a
* properly authenticated client
* @returns
*/
async get_orders(): Promise<any[]> {
const res = await client.fetch(`/stores/v2/orders/query`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
query: {
paging: {
limit: 20
}
}
})
});
const data = await res.json();
return data as any[];
}
};
}
@daguitosama
Copy link
Author

daguitosama commented Jul 19, 2023

The session is a cookie store object, with an api pretty much similar to URLSearchParams and the environment is a runtime Cloudflare object coming down the pipe with env variables and other special sauces.
The point when it brakes is in line 22 client.auth.setTokens(member_tokens); but i think can be preventing by properly validating the tokens. The curious thing is that the client.auth.setTokens accepts any type, but it does not make any parsing or validation on his own.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment