Skip to content
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
9 changes: 7 additions & 2 deletions apps/client/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { headers } from 'next/headers';
import { redirect } from 'next/navigation';

import { MainPage } from '@/pageContainer';
Expand All @@ -24,8 +25,12 @@ export default async function Home({ searchParams }: { searchParams?: { isAdmin?
const isAdminRole = authInfo?.role === 'ADMIN' || authInfo?.role === 'ROOT';

if (isAdminRequested && isAdminRole) {
const isStage = process.env.NEXT_PUBLIC_API_BASE_URL?.includes('stage');
const adminUrl = isStage ? 'https://admin.stage.hellogsm.kr' : 'https://admin.hellogsm.kr';
const host = headers().get('host') ?? '';
const adminUrl = host.includes('localhost')
? 'http://localhost:3001'
: host.includes('stage')
? 'https://admin.stage.hellogsm.kr'
: 'https://admin.hellogsm.kr';
redirect(adminUrl);
}

Expand Down
14 changes: 8 additions & 6 deletions apps/client/src/pageContainer/CallbackPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ const CallbackPage = ({ code, provider }: { code: string; provider: string }) =>
const handleLoginSuccess = async () => {
await queryClient.invalidateQueries({ queryKey: memberQueryKeys.getMyMemberInfo() });

const currentOrigin = window.location.origin;
const isStage = currentOrigin.includes('stage');
const clientBaseUrl = isStage ? 'https://www.stage.hellogsm.kr' : 'https://www.hellogsm.kr';
const nextUrl = provider === 'admin' ? `${clientBaseUrl}/?isAdmin=true` : clientBaseUrl;
const nextUrl =
provider === 'admin'
? `${window.location.origin}/?isAdmin=true`
: window.location.origin;
router.replace(nextUrl);
};

Expand Down Expand Up @@ -59,10 +59,12 @@ const CallbackPage = ({ code, provider }: { code: string; provider: string }) =>
return;
}

const redirectUri = `${window.location.origin}/callback`;

if (provider === 'google' || provider === 'admin') {
googleLogin(code);
googleLogin({ code, redirectUri });
} else if (provider === 'kakao') {
kakaoLogin(code);
kakaoLogin({ code, redirectUri });
} else {
router.replace('/');
toast.error('로그인에 실패했습니다.');
Expand Down
10 changes: 8 additions & 2 deletions packages/api/src/hooks/auth/useOAuthLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ interface ReturnDataType {
message: string;
}

interface LoginPayload {
code: string;
redirectUri: string;
}

export const useOAuthLogin = (
provider: 'google' | 'kakao',
options?: UseMutationOptions<ReturnDataType, AxiosError, string>,
options?: UseMutationOptions<ReturnDataType, AxiosError, LoginPayload>,
) =>
useMutation({
mutationFn: (code: string) => post<ReturnDataType>(authUrl.postLogin(provider), { code }),
mutationFn: ({ code, redirectUri }: LoginPayload) =>
post<ReturnDataType>(authUrl.postLogin(provider), { code, redirectUri }),
...options,
});
26 changes: 9 additions & 17 deletions packages/ui/src/components/LoginButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ interface LoginButtonProps
isAdmin?: boolean;
}

const getClientOrigin = (origin: string): string => {
if (origin.includes('localhost')) return 'http://localhost:3000';
if (origin.includes('stage')) return 'https://www.stage.hellogsm.kr';
return 'https://www.hellogsm.kr';
};

const LoginButton = React.forwardRef<HTMLButtonElement, LoginButtonProps>(
({ className, variant, children, isAdmin = false, ...props }, ref) => {
const [redirectUri, setRedirectUri] = React.useState('');
Expand All @@ -47,24 +53,10 @@ const LoginButton = React.forwardRef<HTMLButtonElement, LoginButtonProps>(

React.useEffect(() => {
if (typeof window !== 'undefined') {
const currentOrigin = window.location.origin;

const stageOrigins = [
'http://localhost:3000',
'http://localhost:3001',
'https://www.stage.hellogsm.kr',
'https://admin.stage.hellogsm.kr',
];

const productionOrigins = ['https://www.hellogsm.kr', 'https://admin.hellogsm.kr'];

if (stageOrigins.includes(currentOrigin)) {
setRedirectUri('https://www.stage.hellogsm.kr/callback');
} else if (productionOrigins.includes(currentOrigin)) {
setRedirectUri('https://www.hellogsm.kr/callback');
}
const origin = isAdmin ? getClientOrigin(window.location.origin) : window.location.origin;
setRedirectUri(`${origin}/callback`);
}
}, []);
}, [isAdmin]);

React.useEffect(() => {
if (redirectUri) {
Expand Down
Loading