2026-02-05 09:38:42 +09:00
|
|
|
|
import { createClient } from "@/utils/supabase/server";
|
2026-02-03 10:51:22 +09:00
|
|
|
|
import { NextResponse } from "next/server";
|
2026-02-04 09:35:42 +09:00
|
|
|
|
import { AUTH_ERROR_MESSAGES, AUTH_ROUTES } from "@/features/auth/constants";
|
2026-02-05 09:38:42 +09:00
|
|
|
|
import { getAuthErrorMessage } from "@/features/auth/errors";
|
2026-02-03 10:51:22 +09:00
|
|
|
|
|
|
|
|
|
|
/**
|
2026-02-05 09:38:42 +09:00
|
|
|
|
* OAuth/??? ?? ?? ??
|
2026-02-03 10:51:22 +09:00
|
|
|
|
*/
|
|
|
|
|
|
export async function GET(request: Request) {
|
|
|
|
|
|
const { searchParams, origin } = new URL(request.url);
|
|
|
|
|
|
|
|
|
|
|
|
const code = searchParams.get("code");
|
2026-02-04 09:35:42 +09:00
|
|
|
|
const next = searchParams.get("next") ?? AUTH_ROUTES.HOME;
|
|
|
|
|
|
const error = searchParams.get("error");
|
|
|
|
|
|
const error_code = searchParams.get("error_code");
|
|
|
|
|
|
const error_description = searchParams.get("error_description");
|
|
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
|
console.error("Auth callback error parameter:", {
|
|
|
|
|
|
error,
|
|
|
|
|
|
error_code,
|
|
|
|
|
|
error_description,
|
|
|
|
|
|
});
|
2026-02-03 10:51:22 +09:00
|
|
|
|
|
2026-02-04 12:28:15 +09:00
|
|
|
|
let message: string = AUTH_ERROR_MESSAGES.DEFAULT;
|
2026-02-04 09:35:42 +09:00
|
|
|
|
|
|
|
|
|
|
if (error === "access_denied") {
|
|
|
|
|
|
message = AUTH_ERROR_MESSAGES.OAUTH_ACCESS_DENIED;
|
|
|
|
|
|
} else if (error === "server_error") {
|
|
|
|
|
|
message = AUTH_ERROR_MESSAGES.OAUTH_SERVER_ERROR;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return NextResponse.redirect(
|
|
|
|
|
|
`${origin}${AUTH_ROUTES.LOGIN}?message=${encodeURIComponent(message)}`,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-03 10:51:22 +09:00
|
|
|
|
if (code) {
|
|
|
|
|
|
const supabase = await createClient();
|
2026-02-04 09:35:42 +09:00
|
|
|
|
const { error: exchangeError } =
|
|
|
|
|
|
await supabase.auth.exchangeCodeForSession(code);
|
2026-02-03 10:51:22 +09:00
|
|
|
|
|
2026-02-04 09:35:42 +09:00
|
|
|
|
if (!exchangeError) {
|
|
|
|
|
|
const forwardedHost = request.headers.get("x-forwarded-host");
|
2026-02-03 10:51:22 +09:00
|
|
|
|
const isLocalEnv = process.env.NODE_ENV === "development";
|
|
|
|
|
|
|
|
|
|
|
|
if (isLocalEnv) {
|
|
|
|
|
|
return NextResponse.redirect(`${origin}${next}`);
|
2026-02-05 09:38:42 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (forwardedHost) {
|
2026-02-03 10:51:22 +09:00
|
|
|
|
return NextResponse.redirect(`https://${forwardedHost}${next}`);
|
|
|
|
|
|
}
|
2026-02-05 09:38:42 +09:00
|
|
|
|
|
|
|
|
|
|
return NextResponse.redirect(`${origin}${next}`);
|
2026-02-03 10:51:22 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-04 09:35:42 +09:00
|
|
|
|
console.error("Auth exchange error:", exchangeError.message);
|
2026-02-05 09:38:42 +09:00
|
|
|
|
const message = getAuthErrorMessage(exchangeError);
|
|
|
|
|
|
return NextResponse.redirect(
|
|
|
|
|
|
`${origin}${AUTH_ROUTES.LOGIN}?message=${encodeURIComponent(message)}`,
|
|
|
|
|
|
);
|
2026-02-03 10:51:22 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const errorMessage = encodeURIComponent(
|
2026-02-04 09:35:42 +09:00
|
|
|
|
AUTH_ERROR_MESSAGES.INVALID_AUTH_LINK,
|
|
|
|
|
|
);
|
|
|
|
|
|
return NextResponse.redirect(
|
|
|
|
|
|
`${origin}${AUTH_ROUTES.LOGIN}?message=${errorMessage}`,
|
2026-02-03 10:51:22 +09:00
|
|
|
|
);
|
|
|
|
|
|
}
|