Skip to content

Commit

Permalink
Merge pull request #273 from depromeet/feature/#255
Browse files Browse the repository at this point in the history
로그인 api 로직 추가
  • Loading branch information
YOOJS1205 authored Feb 12, 2024
2 parents 38d9f55 + f8be9d8 commit f1505da
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
1 change: 0 additions & 1 deletion global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ interface AppleIDAuthSignInResponse {
};
};
}

export declare global {
interface Window {
ReactNativeWebView: {
Expand Down
18 changes: 15 additions & 3 deletions src/app/login/page.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
'use client';

import { useRouter } from 'next/navigation';
import { useEffect } from 'react';
import { useEffect, useState } from 'react';

import useAppleLogin from '@hooks/api/useAppleLogin';
import CTAButton from '@components/common/CTAButton';
import DdoeatLogo from 'public/assets/ddoeat_logo.svg';
import AppleLogo from 'public/assets/icon24/apple_logo_24.svg';
import KakaoLogo from 'public/assets/icon24/kakao_logo_24.svg';
import type { AppleSigninResponse } from 'src/types/apple';

const KAKAO_REST_API_KEY = process.env.NEXT_PUBLIC_KAKAO_REST_API_KEY;
const REDIRECT_URI =
Expand All @@ -15,10 +17,19 @@ const REDIRECT_URI =
: `${process.env.NEXT_PUBLIC_LOCAL_DOMAIN}/auth?type=kakao`;

export default function Page() {
const [code, setCode] = useState('');
const { refetch } = useAppleLogin({
code,
redirect_uri: `${process.env.NEXT_PUBLIC_SITE_DOMAIN}/login`,
});
useEffect(() => {
// Apple 로그인 성공 이벤트 리스너 등록
const handleAppleLoginSuccess = (e: unknown) => {
console.log(e); // 성공 응답 처리
const handleAppleLoginSuccess = (event: Event) => {
const customEvent = event as CustomEvent<AppleSigninResponse>;
console.log(customEvent.detail); // 성공 응답 처리
setCode(customEvent.detail.authorization.id_token);

refetch();
};

// Apple 로그인 실패 이벤트 리스너 등록
Expand All @@ -43,6 +54,7 @@ export default function Page() {
handleAppleLoginFail,
);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const { push } = useRouter();
Expand Down
35 changes: 35 additions & 0 deletions src/hooks/api/useAppleLogin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { useQuery, UseQueryResult } from '@tanstack/react-query';
import { AxiosError } from 'axios';

import { axiosRequest, ApiResponse } from '@api/api-config';

interface LoginRequest {
code: string;
redirect_uri: string;
}

interface LoginResponse {
accessToken: string;
refreshToken: string;
isFirst: boolean;
}

const appleLogin = ({
code,
redirect_uri,
}: LoginRequest): Promise<ApiResponse<LoginResponse>> => {
const path = `/api/v1/auth/login?provider=apple&code=${code}&redirect_uri=${redirect_uri}`;

return axiosRequest('get', path);
};

export default function useAppleLogin({
code,
redirect_uri,
}: LoginRequest): UseQueryResult<LoginResponse, AxiosError> {
return useQuery({
queryKey: ['apple-login'],
queryFn: () => appleLogin({ code, redirect_uri }),
enabled: false,
});
}
7 changes: 7 additions & 0 deletions src/types/apple.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface AppleSigninResponse {
authorization: {
code: string;
id_token: string;
state: string;
};
}

0 comments on commit f1505da

Please sign in to comment.