Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hotfix: broken refresh_token flow #11

Merged
merged 3 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading