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

[fix] Fix Apple login callbacks not redirecting back to the App for Expo #1262

Merged
merged 2 commits into from
Jan 25, 2025
Merged
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
60 changes: 39 additions & 21 deletions apps/nextjs/src/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,46 @@ function rewriteRequestUrlInDevelopment(req: NextRequest) {
return new NextRequest(newURL, req);
}

export const POST = async (_req: NextRequest) => {
async function handleExpoSigninCallback(req: NextRequest, redirectURL: string) {
cookies().delete(EXPO_COOKIE_NAME);

// Run original handler, then extract the session token from the response
// Send it back via a query param in the Expo deep link. The Expo app
// will then get that and set it in the session storage.
const authResponse = await handlers.POST(req);
const setCookie = authResponse.headers
.getSetCookie()
.find((cookie) => AUTH_COOKIE_PATTERN.test(cookie));
const match = setCookie?.match(AUTH_COOKIE_PATTERN)?.[1];

if (!match)
throw new Error(
"Unable to find session cookie: " +
JSON.stringify(authResponse.headers.getSetCookie()),
);

const url = new URL(redirectURL);
url.searchParams.set("session_token", match);

return NextResponse.redirect(url);
}

export const POST = async (
_req: NextRequest,
props: { params: { nextauth: string[] } },
) => {
// First step must be to correct the request URL.
const req = rewriteRequestUrlInDevelopment(_req);

const nextauthAction = props.params.nextauth[0];
const isExpoCallback = cookies().get(EXPO_COOKIE_NAME);

// callback handler required separately in the POST handler
// since Apple sends a POST request instead of a GET
if (nextauthAction === "callback" && !!isExpoCallback) {
return handleExpoSigninCallback(req, isExpoCallback.value);
}

return handlers.POST(req);
};

Expand All @@ -52,26 +89,7 @@ export const GET = async (
}

if (nextauthAction === "callback" && !!isExpoCallback) {
cookies().delete(EXPO_COOKIE_NAME);

// Run original handler, then extract the session token from the response
// Send it back via a query param in the Expo deep link. The Expo app
// will then get that and set it in the session storage.
const authResponse = await handlers.GET(req);
const setCookie = authResponse.headers
.getSetCookie()
.find((cookie) => AUTH_COOKIE_PATTERN.test(cookie));
const match = setCookie?.match(AUTH_COOKIE_PATTERN)?.[1];

if (!match)
throw new Error(
"Unable to find session cookie: " +
JSON.stringify(authResponse.headers.getSetCookie()),
);

const url = new URL(isExpoCallback.value);
url.searchParams.set("session_token", match);
return NextResponse.redirect(url);
return handleExpoSigninCallback(req, isExpoCallback.value);
}

// Every other request just calls the default handler
Expand Down
Loading