-
Notifications
You must be signed in to change notification settings - Fork 1
[Feature/#161] - 구글 로그인 추가 #163
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,226 @@ | ||
| import { useMutation } from "@tanstack/react-query"; | ||
| import { GetServerSidePropsContext, GetServerSidePropsResult } from "next"; | ||
| import { useRouter } from "next/router"; | ||
| import { JSX, useEffect } from "react"; | ||
| import Link from "next/link"; | ||
| import { postGoogleAuthorizationCode } from "@/domains/auth/api"; | ||
|
|
||
| type GoogleCallbackPageProps = { | ||
| code: string; | ||
| state: string; | ||
| }; | ||
|
|
||
| export default function KakaoCallbackPage({ | ||
| code, | ||
| state | ||
| }: GoogleCallbackPageProps): JSX.Element | null { | ||
| const router = useRouter(); | ||
|
|
||
| const authMutation = useMutation({ | ||
| mutationFn: ({ | ||
| code, | ||
| redirectUri | ||
| }: { | ||
| code: string; | ||
| redirectUri: string; | ||
| }) => postGoogleAuthorizationCode(code, redirectUri), | ||
|
|
||
| onSuccess: ({ data }) => { | ||
| if (!data.profile_completed) { | ||
| router.replace(`/login/profile?state=${state || "/"}`); | ||
| return; | ||
| } | ||
|
|
||
| const redirectTo = state || "/"; | ||
| router.replace(redirectTo); | ||
| }, | ||
|
|
||
| onError: (error) => { | ||
| console.error("로그인 실패:", error); | ||
| }, | ||
| retry: 1 | ||
| }); | ||
|
|
||
| useEffect(() => { | ||
| if ( | ||
| !router.isReady || | ||
| authMutation.isPending || | ||
| authMutation.error || | ||
| authMutation.isSuccess | ||
| ) | ||
| return; | ||
|
|
||
| const redirectUri = `${process.env.NEXT_PUBLIC_BASE_URL}/login/google/callback`; | ||
|
|
||
| authMutation.mutate({ | ||
| code: code as string, | ||
| redirectUri | ||
| }); | ||
| }, [router.isReady, code, state, authMutation]); | ||
|
|
||
| // 로딩 상태 | ||
| if (authMutation.isPending) { | ||
| return ( | ||
| <div className="min-h-screen bg-gray-50 flex items-center justify-center"> | ||
| <div className="max-w-md w-full text-center px-4"> | ||
| <div className="bg-white rounded-lg shadow-lg p-8"> | ||
| <div className="mx-auto w-16 h-16 bg-blue-100 rounded-full flex items-center justify-center mb-6"> | ||
| <svg | ||
| className="animate-spin w-8 h-8 text-blue-600" | ||
| xmlns="http://www.w3.org/2000/svg" | ||
| fill="none" | ||
| viewBox="0 0 24 24" | ||
| > | ||
| <circle | ||
| className="opacity-25" | ||
| cx="12" | ||
| cy="12" | ||
| r="10" | ||
| stroke="currentColor" | ||
| strokeWidth="4" | ||
| ></circle> | ||
| <path | ||
| className="opacity-75" | ||
| fill="currentColor" | ||
| d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z" | ||
| ></path> | ||
| </svg> | ||
| </div> | ||
| <h2 className="text-2xl font-bold text-gray-900 mb-2"> | ||
| 로그인 처리 중... | ||
| </h2> | ||
| <p className="text-gray-600">구글 로그인을 완료하고 있습니다</p> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| // 성공 상태 | ||
| if (authMutation.isSuccess) { | ||
| return ( | ||
| <div className="min-h-screen bg-gray-50 flex items-center justify-center"> | ||
| <div className="max-w-md w-full text-center px-4"> | ||
| <div className="bg-white rounded-lg shadow-lg p-8"> | ||
| <div className="mx-auto w-16 h-16 bg-green-100 rounded-full flex items-center justify-center mb-6"> | ||
| <svg | ||
| className="w-8 h-8 text-green-600" | ||
| fill="none" | ||
| stroke="currentColor" | ||
| viewBox="0 0 24 24" | ||
| xmlns="http://www.w3.org/2000/svg" | ||
| > | ||
| <path | ||
| strokeLinecap="round" | ||
| strokeLinejoin="round" | ||
| strokeWidth={2} | ||
| d="M5 13l4 4L19 7" | ||
| /> | ||
| </svg> | ||
| </div> | ||
| <h2 className="text-2xl font-bold text-gray-900 mb-2"> | ||
| 로그인 완료! | ||
| </h2> | ||
| <p className="text-gray-600 mb-6"> | ||
| 로그인이 성공적으로 완료되었습니다 | ||
| </p> | ||
| <div className="space-y-4"> | ||
| <div className="text-sm text-gray-500"> | ||
| <span>페이지가 곧 이동됩니다...</span> | ||
| </div> | ||
| <Link | ||
| href={state || "/"} | ||
| className="inline-block w-full bg-blue-600 text-white py-3 px-6 rounded-lg font-medium hover:bg-blue-700 transition-colors" | ||
| > | ||
| 지금 이동하기 | ||
| </Link> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| // 에러 상태 | ||
| if (authMutation.isError) { | ||
| return ( | ||
| <div className="min-h-screen bg-gray-50 flex items-center justify-center"> | ||
| <div className="max-w-md w-full text-center px-4"> | ||
| <div className="bg-white rounded-lg shadow-lg p-8"> | ||
| <div className="mx-auto w-16 h-16 bg-red-100 rounded-full flex items-center justify-center mb-6"> | ||
| <svg | ||
| className="w-8 h-8 text-red-600" | ||
| fill="none" | ||
| stroke="currentColor" | ||
| viewBox="0 0 24 24" | ||
| xmlns="http://www.w3.org/2000/svg" | ||
| > | ||
| <path | ||
| strokeLinecap="round" | ||
| strokeLinejoin="round" | ||
| strokeWidth={2} | ||
| d="M6 18L18 6M6 6l12 12" | ||
| /> | ||
| </svg> | ||
| </div> | ||
| <h2 className="text-2xl font-bold text-gray-900 mb-2"> | ||
| 로그인 실패 | ||
| </h2> | ||
| <p className="text-gray-600 mb-6"> | ||
| 로그인 처리 중 문제가 발생했습니다. 다시 시도해주세요. | ||
| </p> | ||
| <div className="space-y-3"> | ||
| <button | ||
| onClick={() => { | ||
| const redirectUri = `${process.env.NEXT_PUBLIC_BASE_URL}/login/google/callback`; | ||
| authMutation.mutate({ code, redirectUri }); | ||
| }} | ||
| className="w-full bg-blue-600 text-white py-3 px-6 rounded-lg font-medium hover:bg-blue-700 transition-colors disabled:opacity-50" | ||
| disabled={authMutation.isPending} | ||
| > | ||
| 다시 시도하기 | ||
| </button> | ||
| <Link | ||
| href="/login" | ||
| className="inline-block w-full bg-gray-100 text-gray-700 py-3 px-6 rounded-lg font-medium hover:bg-gray-200 transition-colors" | ||
| > | ||
| 로그인 페이지로 돌아가기 | ||
| </Link> | ||
| <Link | ||
| href="/" | ||
| className="inline-block text-blue-600 hover:text-blue-800 text-sm" | ||
| > | ||
| 홈으로 이동 | ||
| </Link> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| export const getServerSideProps = ( | ||
| context: GetServerSidePropsContext | ||
| ): GetServerSidePropsResult<GoogleCallbackPageProps> => { | ||
| const { code, state } = context.query; | ||
|
|
||
| // Authorization code가 없는 경우 | ||
| if (!code) { | ||
| return { | ||
| redirect: { | ||
| destination: "/500", | ||
| permanent: false | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| props: { | ||
| code: code as string, | ||
| state: (state as string) || "/" | ||
| } | ||
| }; | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
KakaoLoginResponse타입을 구글 로그인 응답에도 사용하고 있습니다. 서버에서 동일한 형식의 응답을 반환하더라도, 타입 이름이 특정 소셜 로그인(카카오)을 가리키고 있어 혼란을 줄 수 있습니다. 향후 유지보수성을 위해SocialLoginResponse와 같이 좀 더 일반적인 이름으로 변경하는 것을 고려해보세요.