-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
1,069 additions
and
727 deletions.
There are no files selected for viewing
This file contains 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,2 @@ | ||
NEXT_PUBLIC_SERVER_URL=<NEXT_PUBLIC_SERVER_URL> | ||
NEXT_PUBLIC_APP_ENV=<NEXT_PUBLIC_APP_ENV> |
This file contains 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 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 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 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,40 @@ | ||
import { KAKAO } from '@/app/_constants/kakao' | ||
|
||
interface KakaoRequestBody { | ||
grant_type: string | ||
client_id: string | ||
redirect_uri: string | ||
code: string | ||
} | ||
|
||
export async function GET(request: Request) { | ||
const { searchParams } = new URL(request.url) | ||
const code = searchParams.get('code') | ||
|
||
if (code === null) { | ||
return Response.json({ | ||
error: 'code is required', | ||
}) | ||
} | ||
|
||
const kakaoRequestBody: KakaoRequestBody = { | ||
grant_type: 'authorization_code', | ||
client_id: KAKAO.REST_API_KEY, | ||
redirect_uri: KAKAO.REDIRECT_URI, | ||
code, | ||
} | ||
|
||
const data = await fetch(`https://kauth.kakao.com/oauth/token`, { | ||
method: 'POST', | ||
headers: { | ||
Authorization: `469f80f467f98201541aadbfa83e8456`, | ||
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8', | ||
}, | ||
|
||
body: Object.keys(kakaoRequestBody) | ||
.map((k) => encodeURIComponent(k) + '=' + encodeURI(kakaoRequestBody[k as keyof KakaoRequestBody])) | ||
.join('&'), | ||
}).then(async (res) => await res.json()) | ||
|
||
return Response.json(data) | ||
} |
This file contains 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,45 @@ | ||
'use client' | ||
|
||
import { useSearchParams, useRouter } from 'next/navigation' | ||
|
||
import { useEffect } from 'react' | ||
|
||
import { useQuery } from '@tanstack/react-query' | ||
|
||
import { ROUTE } from '@/app/_constants/route' | ||
import { getTokenByAuthorizationCode } from '@/app/_service/auth' | ||
|
||
import { QUERY_KEY } from './queries' | ||
|
||
export default function KaKaoCallbackPage() { | ||
const router = useRouter() | ||
|
||
const searchParams = useSearchParams() | ||
const code = searchParams.get('code') | ||
|
||
const { isSuccess, data } = useQuery({ | ||
queryKey: QUERY_KEY.KAKAO_TOKEN(code as string), | ||
queryFn: async () => | ||
await getTokenByAuthorizationCode({ | ||
code: code as string, | ||
}), | ||
enabled: code !== null, | ||
}) | ||
|
||
useEffect(() => { | ||
if (isSuccess) { | ||
router.push(ROUTE.HOME) | ||
} | ||
}, [isSuccess]) | ||
|
||
if (!isSuccess) { | ||
return null | ||
} | ||
|
||
return ( | ||
<div> | ||
<span>{code}</span> | ||
<span>{data.data.access_token}</span> | ||
</div> | ||
) | ||
} |
This file contains 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,3 @@ | ||
export const QUERY_KEY = { | ||
KAKAO_TOKEN: (code: string) => ['kakaoToken', code], | ||
} |
This file contains 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 |
---|---|---|
@@ -1,3 +1,15 @@ | ||
import { KAKAO } from '../_constants/kakao' | ||
|
||
export default function Home() { | ||
return <main>Hello, World!</main> | ||
return ( | ||
<main> | ||
<span>Hello, World!</span> | ||
<a | ||
className='rounded-md bg-yellow-500 p-4 text-white' | ||
href={`https://kauth.kakao.com/oauth/authorize?response_type=code&client_id=${KAKAO.REST_API_KEY}&redirect_uri=${KAKAO.REDIRECT_URI}`} | ||
> | ||
카카오 로그인 | ||
</a> | ||
</main> | ||
) | ||
} |
Empty file.
This file contains 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,21 @@ | ||
'use client' | ||
|
||
import { PropsWithChildren, useState } from 'react' | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query' | ||
|
||
export default function Providers({ children }: PropsWithChildren) { | ||
const [queryClient] = useState( | ||
() => | ||
new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
// With SSR, we usually want to set some default staleTime | ||
// above 0 to avoid refetching immediately on the client | ||
staleTime: 60 * 1000, | ||
}, | ||
}, | ||
}) | ||
) | ||
|
||
return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
} |
Empty file.
This file contains 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,16 @@ | ||
export const SERVER_URL = process.env.NEXT_PUBLIC_SERVER_URL | ||
|
||
export type AppEnv = 'production' | 'development' | ||
|
||
const getAppEnv = (): AppEnv => (process.env.NEXT_PUBLIC_APP_ENV as Exclude<AppEnv, 'development'>) || 'development' | ||
|
||
export const getApiEndpoint = () => { | ||
switch (getAppEnv()) { | ||
case 'production': | ||
return SERVER_URL | ||
case 'development': | ||
return SERVER_URL | ||
default: | ||
return SERVER_URL | ||
} | ||
} |
This file contains 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,5 @@ | ||
export const KAKAO = { | ||
// REST_API_KEY: '8cb6bb4a47d00e99bb5a038f9b6467fe', | ||
REST_API_KEY: 'df0059498e461a8b0c76d9b4d537dbaa', | ||
REDIRECT_URI: 'http://localhost:3000/oauth/callback/kakao', | ||
} |
This file contains 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,10 @@ | ||
export const ROUTE = { | ||
HOME: '/', | ||
LOGIN: '/login', | ||
SIGNUP: '/signup', | ||
OAUTH: { | ||
KAKAO: { | ||
CALLBACK: '/oauth/callback/kakao', | ||
}, | ||
}, | ||
} |
Empty file.
This file contains 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,11 @@ | ||
export interface GetTokenFromKakaoParams { | ||
code: string | ||
} | ||
|
||
export interface GetTokenFromKakaoResponse { | ||
access_token: string | ||
expires_in: number | ||
refresh_token: string | ||
refresh_token_expires_in: number | ||
token_type: string | ||
} |
This file contains 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,6 @@ | ||
import axios from 'axios' | ||
import { GetTokenFromKakaoParams, GetTokenFromKakaoResponse } from './auth.types' | ||
|
||
export const getTokenByAuthorizationCode = ({ code }: GetTokenFromKakaoParams) => { | ||
return axios.get<GetTokenFromKakaoResponse>(`/oauth/callback/kakao/api?code=${code}`) | ||
} |
This file contains 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,33 @@ | ||
import axios, { type AxiosRequestConfig } from 'axios' | ||
|
||
import { getApiEndpoint } from './api.utils' | ||
import type { ServerResponse } from './api.types' | ||
|
||
const createApi = () => { | ||
const _api = axios.create({ | ||
baseURL: getApiEndpoint(), | ||
withCredentials: true, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}) | ||
|
||
return _api | ||
} | ||
|
||
export const axiosInstance = createApi() | ||
|
||
const api = { | ||
get: <T>(url: string, config?: AxiosRequestConfig) => | ||
axiosInstance.get<ServerResponse<T>>(url, config).then((res) => res.data), | ||
post: <T>(url: string, payload?: object, config?: AxiosRequestConfig) => | ||
axiosInstance.post<ServerResponse<T>>(url, payload, config).then((res) => res.data), | ||
put: <T>(url: string, payload?: object, config?: AxiosRequestConfig) => | ||
axiosInstance.put<ServerResponse<T>>(url, payload, config).then((res) => res.data), | ||
patch: <T>(url: string, payload?: object, config?: AxiosRequestConfig) => | ||
axiosInstance.patch<ServerResponse<T>>(url, payload, config).then((res) => res.data), | ||
delete: <T>(url: string, config?: AxiosRequestConfig) => | ||
axiosInstance.delete<ServerResponse<T>>(url, config).then((res) => res.data), | ||
} | ||
|
||
export default api |
This file contains 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,11 @@ | ||
export interface ServerResponse<T> { | ||
statusCode: number | ||
responseMessage: string | ||
data: T | ||
} | ||
|
||
export interface ServerError { | ||
responseMessage: string | ||
data: boolean | ||
statusCode: number | ||
} |
This file contains 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,16 @@ | ||
export const SERVER_URL = process.env.NEXT_PUBLIC_SERVER_URL as string | ||
|
||
export type AppEnv = 'production' | 'development' | ||
|
||
const getAppEnv = (): AppEnv => (process.env.NEXT_PUBLIC_APP_ENV as Exclude<AppEnv, 'development'>) || 'development' | ||
|
||
export const getApiEndpoint = () => { | ||
switch (getAppEnv()) { | ||
case 'production': | ||
return SERVER_URL | ||
case 'development': | ||
return SERVER_URL | ||
default: | ||
return SERVER_URL | ||
} | ||
} |
This file contains 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 @@ | ||
export async function GET(request: Request) {} |
Binary file not shown.
This file contains 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
Oops, something went wrong.