♻️ refactor(auth): replace redirect flow to popup window

This commit is contained in:
2026-01-09 09:27:44 +07:00
parent b2c21c5f01
commit cb436fe40c
3 changed files with 31 additions and 7 deletions

View File

@ -1,25 +1,33 @@
"use client"; "use client";
import { Spinner } from "@/shared/libs/shadcn/ui/spinner"; import { Spinner } from "@/shared/libs/shadcn/ui/spinner";
import { submitProviderCallback } from "@/shared/widgets/signin/actions/submitProviderCallback"; import { submitProviderCallback } from "@/features/authCallback/actions/submitProviderCallback";
import { useParams, useRouter, useSearchParams } from "next/navigation"; import { useParams, useRouter, useSearchParams } from "next/navigation";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
const AuthCallbackIndex = () => { const AuthCallbackIndex = () => {
const { name } = useParams(); const { name } = useParams();
const queries = useSearchParams().toString(); const queries = useSearchParams().toString();
const router = useRouter();
const [textDescription, setTextDescription] = useState( const [textDescription, setTextDescription] = useState(
"We are processing your authentication." "We are processing your authentication."
); );
const finishOAuthFlow = (type: string) => {
setTimeout(() => {
if (!window.opener) window.location.href = "/";
window.opener.postMessage({ type: type }, window.location.origin);
window.close();
}, 1000);
};
useEffect(() => { useEffect(() => {
(async () => { (async () => {
const response = await submitProviderCallback(name as string, queries); const response = await submitProviderCallback(name as string, queries);
if (response.success) { if (response.success) {
setTextDescription("Authentication successful! Redirecting..."); setTextDescription("Authentication successful! Redirecting...");
router.push("/"); finishOAuthFlow("oauth-success");
} else { } else {
setTextDescription("Authentication failed. Please try again."); setTextDescription("Authentication failed. Please try again.");
finishOAuthFlow("oauth-failed");
} }
})(); })();
}, []); }, []);

View File

@ -10,7 +10,7 @@ import {
import { Input } from "@/shared/libs/shadcn/ui/input"; import { Input } from "@/shared/libs/shadcn/ui/input";
import { Label } from "@/shared/libs/shadcn/ui/label"; import { Label } from "@/shared/libs/shadcn/ui/label";
import { Separator } from "@/shared/libs/shadcn/ui/separator"; import { Separator } from "@/shared/libs/shadcn/ui/separator";
import { useEffect, useState } from "react"; import { useCallback, useEffect, useState } from "react";
import { import {
getAllThirdPartyAuth, getAllThirdPartyAuth,
GetALlThirdPartyAuthCallback, GetALlThirdPartyAuthCallback,
@ -18,14 +18,13 @@ import {
import { Icon } from "@iconify/react"; import { Icon } from "@iconify/react";
import { Spinner } from "@/shared/libs/shadcn/ui/spinner"; import { Spinner } from "@/shared/libs/shadcn/ui/spinner";
import { getOauthEndpoint } from "../actions/getOauthEndpoint"; import { getOauthEndpoint } from "../actions/getOauthEndpoint";
import { useRouter } from "next/navigation";
const SignInCard = () => { const SignInCard = () => {
const router = useRouter();
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const [oAuthProviders, setOAuthProviders] = const [oAuthProviders, setOAuthProviders] =
useState<GetALlThirdPartyAuthCallback | null>(null); useState<GetALlThirdPartyAuthCallback | null>(null);
// Fetch available third-party auth providers on component mount
useEffect(() => { useEffect(() => {
(async () => { (async () => {
const res = await getAllThirdPartyAuth(); const res = await getAllThirdPartyAuth();
@ -33,6 +32,7 @@ const SignInCard = () => {
})(); })();
}, []); }, []);
// Open OAuth endpoint in a new popup window
const getOauthEndpointUrl = async ( const getOauthEndpointUrl = async (
providerReqEndpoint: string, providerReqEndpoint: string,
providerName: string providerName: string
@ -41,9 +41,25 @@ const SignInCard = () => {
endpointUrl: providerReqEndpoint, endpointUrl: providerReqEndpoint,
providerName: providerName, providerName: providerName,
}); });
router.push(res.data?.endpointUrl || "/");
setIsLoading(true);
window.open(res.data?.endpointUrl, "oauthPopup", "width=500,height=600");
}; };
// Handle the feedback from popup window for OAuth
const handleMessage = useCallback((event: MessageEvent) => {
if (event.origin !== window.location.origin) return;
if (event.data.type === "oauth-success") window.location.reload();
if (event.data.type === "oauth-failed") setIsLoading(false);
}, []);
useEffect(() => {
window.addEventListener("message", handleMessage);
return () => {
window.removeEventListener("message", handleMessage);
};
}, [handleMessage]);
return ( return (
<DialogContent showCloseButton={false}> <DialogContent showCloseButton={false}>
<DialogHeader> <DialogHeader>