From 079ca3fb545f4e12fd70d7b40364ff4c656f8d4a Mon Sep 17 00:00:00 2001 From: SeungWoo Date: Mon, 27 May 2024 14:19:34 +0900 Subject: [PATCH 1/2] EDIT :: route --- src/app/auth/facebook/login/route.ts | 22 ++++++++++++++++++++-- src/app/auth/google/login/route.ts | 4 +++- src/app/auth/kakao/login/route.ts | 4 +++- src/app/auth/login/route.ts | 13 +++++++++++-- src/components/modal/SignupModal.tsx | 2 +- src/services/authSignup.ts | 5 ++++- src/utils/loginRedirect.ts | 5 ++++- 7 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/app/auth/facebook/login/route.ts b/src/app/auth/facebook/login/route.ts index efa00b7..2847028 100644 --- a/src/app/auth/facebook/login/route.ts +++ b/src/app/auth/facebook/login/route.ts @@ -1,7 +1,25 @@ +import { authLogin, getKakaoToken } from '@/services' +import { cookies } from 'next/headers' import { NextResponse } from 'next/server' export async function GET(request: Request) { const requestUrl = new URL(request.url) - console.log(`requestUrl is : ${requestUrl}`) - return NextResponse.redirect(`${requestUrl.origin}`) + const code = requestUrl.searchParams.get('code') + + console.log('haha : ' + requestUrl) + + // if (code) { + // return getKakaoToken(code).then(async (data) => { + // return await authLogin('kakao', data.access_token) + // .then((response) => + // NextResponse.redirect(`${process.env.NEXT_PUBLIC_ORIGIN_URL}`), + // ) + // .catch((error) => { + // cookies().set('Access_Token', data.access_token) + // return NextResponse.redirect(`${requestUrl.origin}/signup?kind=kakao`) + // }) + // }) + // } + + return NextResponse.redirect(requestUrl.origin) } diff --git a/src/app/auth/google/login/route.ts b/src/app/auth/google/login/route.ts index c7b3deb..4aa3464 100644 --- a/src/app/auth/google/login/route.ts +++ b/src/app/auth/google/login/route.ts @@ -24,7 +24,9 @@ export async function GET(request: Request) { .then((response) => response.json()) .then(async (data) => { return await authLogin('google', data.access_token) - .then(() => NextResponse.redirect(requestUrl.origin)) + .then(() => + NextResponse.redirect(`${process.env.NEXT_PUBLIC_ORIGIN_URL}`), + ) .catch((error) => { cookies().set('Access_Token', data.access_token) return NextResponse.redirect( diff --git a/src/app/auth/kakao/login/route.ts b/src/app/auth/kakao/login/route.ts index 934b71f..cfb89ef 100644 --- a/src/app/auth/kakao/login/route.ts +++ b/src/app/auth/kakao/login/route.ts @@ -9,7 +9,9 @@ export async function GET(request: Request) { if (code) { return getKakaoToken(code).then(async (data) => { return await authLogin('kakao', data.access_token) - .then((response) => NextResponse.redirect(requestUrl.origin)) + .then((response) => + NextResponse.redirect(`${process.env.NEXT_PUBLIC_ORIGIN_URL}`), + ) .catch((error) => { cookies().set('Access_Token', data.access_token) return NextResponse.redirect(`${requestUrl.origin}/signup?kind=kakao`) diff --git a/src/app/auth/login/route.ts b/src/app/auth/login/route.ts index e8a2deb..f14ca05 100644 --- a/src/app/auth/login/route.ts +++ b/src/app/auth/login/route.ts @@ -1,9 +1,18 @@ +import { authLogin } from '@/services' +import { AuthKindType } from '@/types' +import { cookies } from 'next/headers' import { NextResponse } from 'next/server' export async function GET(request: Request) { const requestUrl = new URL(request.url) const kind = requestUrl.searchParams.get('kind') - console.log(`login with ${kind}`) + const signed = requestUrl.searchParams.get('signed') + const access_token = cookies().get('Access_Token')?.value + + if (signed) { + await authLogin(kind as AuthKindType, access_token || '') + return NextResponse.redirect(requestUrl.origin) + } if (kind === 'google') { return NextResponse.redirect( @@ -12,7 +21,7 @@ export async function GET(request: Request) { } if (kind === 'facebook') { return NextResponse.redirect( - `https://accounts.google.com/o/oauth2/v2/auth?client_id=${process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID}&response_type=token&redirect_uri=${process.env.NEXT_PUBLIC_GOOGLE_REDIRECT_URI}&scope=https://www.googleapis.com/auth/userinfo.email+https://www.googleapis.com/auth/userinfo.profile`, + `https://www.facebook.com/v20.0/dialog/oauth?client_id=${process.env.NEXT_PUBLIC_FACEBOOK_CLIENT_ID}&redirect_uri=${process.env.NEXT_PUBLIC_FACEBOOK_REDIRECT_URI}&response_type=token`, ) } if (kind === 'kakao') { diff --git a/src/components/modal/SignupModal.tsx b/src/components/modal/SignupModal.tsx index 833c7ba..aa5a2ef 100644 --- a/src/components/modal/SignupModal.tsx +++ b/src/components/modal/SignupModal.tsx @@ -35,7 +35,7 @@ export const SignupModal = ({ kind }: { kind: string }) => { } const access_token = getCookie('Access_Token') await authSignup(kind as AuthKindType, access_token || '', signupJson).then( - () => loginRedirect(kind as AuthKindType), + () => loginRedirect(kind as AuthKindType, true), ) } diff --git a/src/services/authSignup.ts b/src/services/authSignup.ts index d0f47b9..6ce2c5b 100644 --- a/src/services/authSignup.ts +++ b/src/services/authSignup.ts @@ -14,6 +14,9 @@ export const authSignup = async ( }, data: data, }) - .then((response) => response.data) + .then((response) => { + console.log(response.headers) + return response.data + }) .catch((error) => console.log(error)) } diff --git a/src/utils/loginRedirect.ts b/src/utils/loginRedirect.ts index 0c6c65e..36ed178 100644 --- a/src/utils/loginRedirect.ts +++ b/src/utils/loginRedirect.ts @@ -3,6 +3,9 @@ import { AuthKindType } from '@/types' import { redirect } from 'next/navigation' -export async function loginRedirect(kind: AuthKindType) { +export async function loginRedirect(kind: AuthKindType, signed?: boolean) { + if (signed) { + return redirect(`/auth/login?kind=${kind}&signed=${signed}`) + } return redirect(`/auth/login?kind=${kind}`) } From f3e832b8f4033eb820b610048f6e0fa673201ed2 Mon Sep 17 00:00:00 2001 From: SeungWoo Date: Mon, 27 May 2024 14:57:44 +0900 Subject: [PATCH 2/2] EDIT :: rollback --- src/app/auth/google/login/route.ts | 4 +--- src/app/auth/kakao/login/route.ts | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/app/auth/google/login/route.ts b/src/app/auth/google/login/route.ts index 4aa3464..c7b3deb 100644 --- a/src/app/auth/google/login/route.ts +++ b/src/app/auth/google/login/route.ts @@ -24,9 +24,7 @@ export async function GET(request: Request) { .then((response) => response.json()) .then(async (data) => { return await authLogin('google', data.access_token) - .then(() => - NextResponse.redirect(`${process.env.NEXT_PUBLIC_ORIGIN_URL}`), - ) + .then(() => NextResponse.redirect(requestUrl.origin)) .catch((error) => { cookies().set('Access_Token', data.access_token) return NextResponse.redirect( diff --git a/src/app/auth/kakao/login/route.ts b/src/app/auth/kakao/login/route.ts index cfb89ef..934b71f 100644 --- a/src/app/auth/kakao/login/route.ts +++ b/src/app/auth/kakao/login/route.ts @@ -9,9 +9,7 @@ export async function GET(request: Request) { if (code) { return getKakaoToken(code).then(async (data) => { return await authLogin('kakao', data.access_token) - .then((response) => - NextResponse.redirect(`${process.env.NEXT_PUBLIC_ORIGIN_URL}`), - ) + .then((response) => NextResponse.redirect(requestUrl.origin)) .catch((error) => { cookies().set('Access_Token', data.access_token) return NextResponse.redirect(`${requestUrl.origin}/signup?kind=kakao`)