Skip to content

Commit

Permalink
hotfix: broken refresh_token flow (#11)
Browse files Browse the repository at this point in the history
* add: extra `input` padding

* fix: invalid `json` error payloads

* fix: broken `refresh_token` flow
  • Loading branch information
Jabolol authored Feb 23, 2024
1 parent 6be60f0 commit 465e15d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
28 changes: 24 additions & 4 deletions client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ export const headers = (_templates: TemplateStringsArray, token: string) => ({
"User-Agent": "okhttp/4.12.0",
});

const refresh = () => ({
"Accept": "*/*",
"User-Agent": `BeReal/8586 CFNetwork/1240.0.4 Darwin/20.6.0`,
"x-ios-bundle-identifier": "AlexisBarreyat.BeReal",
"Content-Type": "application/json",
});

export const validators: ApiValidators = {
search: z.object({
query: z.string(),
Expand Down Expand Up @@ -45,12 +52,20 @@ const generic = async (url: string, token: string, body?: unknown) => {
url,
{
method: body ? "POST" : "GET",
headers: headers`${token}`,
headers: {
...headers`${token}`,
...url.includes("refresh_token") ? refresh() : {},
},
body: body ? JSON.stringify(body) : undefined,
},
);
if (!result.ok) {
return { error: `(${result.status}) failed to fetch \`${result.url}\`` };
return {
error: {
message: `(${result.status}) failed to fetch \`${result.url}\``,
data: await result.text(),
},
};
}
return { data: await result.json() };
};
Expand Down Expand Up @@ -90,7 +105,7 @@ const map: ApiFunction = {
),
refresh: ({ refreshToken: refresh_token }, token) =>
generic(
`https://mobile.bereal.com/api/auth/refresh`,
`https://auth.bereal.team/token?grant_type=refresh_token`,
token,
{
grant_type: "refresh_token",
Expand Down Expand Up @@ -131,7 +146,12 @@ export const execute = async <T extends keyof APIMap>(
body: JSON.stringify({ key, data }),
});
if (!request.ok) {
return { error: `(${request.status}) failed to fetch \`${request.url}\`` };
return {
error: {
message: `(${request.status}) failed to fetch \`${request.url}\``,
data: await request.text(),
},
};
}
return await request.json();
};
2 changes: 1 addition & 1 deletion islands/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default function Search() {

return (
<div className="h-[70vh]">
<div className="mx-auto max-w-sm my-4 p-4 dark:bg-gray-800 border-2 border-gray-800 dark:border-white rounded-md opacity-50">
<div className="mx-6 max-w-sm my-4 p-4 dark:bg-gray-800 border-2 border-gray-800 dark:border-white rounded-md opacity-50">
<input
type="text"
disabled
Expand Down
2 changes: 1 addition & 1 deletion routes/api/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const handler = async (
const { key, data } = await req.json();
const [, token] = req.headers.get("authorization")!.split(" ");
const result = await perform(key, data, token);
return new Response(result.error ? result.error : JSON.stringify(result), {
return new Response(JSON.stringify(result), {
headers: { "content-type": "application/json" },
status: result.error ? 400 : 200,
});
Expand Down
2 changes: 1 addition & 1 deletion types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ export type SelfPost = NonNullable<FeedResp["userPosts"]>["posts"][number];

type MaybePromise<T> = T | Promise<T>;

type Out<T> = { data: T } | { error: string };
type Out<T> = { data: T } | { error: unknown };

type Pair<T, K> = {
in: T;
Expand Down

0 comments on commit 465e15d

Please sign in to comment.