Skip to content
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

MARA-9 소셜로그인 기능 구현 #16

Merged
merged 13 commits into from
Feb 10, 2024
8 changes: 8 additions & 0 deletions .github/workflows/workflow-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,13 @@ jobs:
- name: Install Dependencies
run: yarn install --frozen-lockfile

# 환경 변수 추가
- name: Set Environment Variables
run: |
echo "NEXT_PUBLIC_KAKAO_API_KEY=${{ secrets.NEXT_PUBLIC_KAKAO_API_KEY }}" >> .env
echo "NEXT_PUBLIC_KAKAO_REDIRECT_URI=${{ secrets.NEXT_PUBLIC_KAKAO_REDIRECT_URI }}" >> .env
echo "NEXT_PUBLIC_GOOGLE_API_KEY=${{ secrets.NEXT_PUBLIC_GOOGLE_API_KEY }}" >> .env
echo "NEXT_PUBLIC_GOOGLE_REDIRECT_URI=${{ secrets.NEXT_PUBLIC_GOOGLE_REDIRECT_URI }}" >> .env

- name: Build
run: yarn build
8 changes: 8 additions & 0 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ jobs:
- name: Install Dependencies
run: yarn install --frozen-lockfile

# 환경 변수 추가
- name: Set Environment Variables
run: |
echo "NEXT_PUBLIC_KAKAO_API_KEY=${{ secrets.NEXT_PUBLIC_KAKAO_API_KEY }}" >> .env
echo "NEXT_PUBLIC_KAKAO_REDIRECT_URI=${{ secrets.NEXT_PUBLIC_KAKAO_REDIRECT_URI }}" >> .env
echo "NEXT_PUBLIC_GOOGLE_API_KEY=${{ secrets.NEXT_PUBLIC_GOOGLE_API_KEY }}" >> .env
echo "NEXT_PUBLIC_GOOGLE_REDIRECT_URI=${{ secrets.NEXT_PUBLIC_GOOGLE_REDIRECT_URI }}" >> .env

- name: Build
run: yarn build

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ yarn-error.log*

# local env files
.env*.local
.env

# vercel
.vercel
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@commitlint/config-conventional": "^18.4.3",
"@emotion/react": "^11.11.3",
"@emotion/styled": "^11.11.0",
"@tanstack/react-query": "^5.18.1",
"@typescript-eslint/parser": "^6.19.0",
"axios": "^1.6.5",
"dayjs": "^1.11.10",
Expand Down
31 changes: 31 additions & 0 deletions src/api/login/getToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import axiosInstance from '../axiosInstance';

interface LoginResponseType {
data: { accessToken: string; refreshToken: string; email: string };
}

const fetchKaKao = async (code: string): Promise<LoginResponseType> =>
await axiosInstance.get(`/users/kakao-login?code=${code}`);

export const getKaKaoToken: (code: string) => Promise<void> = async (code) => {
fetchKaKao(code)
.then((response: LoginResponseType) => {
localStorage.setItem('token', response.data.accessToken);
})
.catch((error) => {
console.error(error);
});
};

const fetchGoogle = async (code: string): Promise<LoginResponseType> =>
await axiosInstance.get(`/users/google-login?code=${code}`);

export const getGoogleToken: (code: string) => Promise<void> = async (code) => {
fetchGoogle(code)
.then((response: LoginResponseType) => {
localStorage.setItem('token', response.data.accessToken);
})
.catch((error) => {
console.error(error);
});
};
8 changes: 8 additions & 0 deletions src/assets/images/img_login_google.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions src/assets/images/img_login_kakao.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 63 additions & 0 deletions src/assets/images/img_login_monsters.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions src/components/atoms/ExclamationAlertSpan.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react';
import { ExclamationIcon } from '@/assets/icons';

interface AlertMessageProps {
message: string;
className?: string;
}

const ExclamationAlertSpan: React.FC<AlertMessageProps> = ({
message,
className,
}) => {
return (
<span
className={`flex items-center gap-[4px] text-point4 body1-medium ${className}`}
>
<ExclamationIcon />
{message}
</span>
);
};

export default ExclamationAlertSpan;
1 change: 1 addition & 0 deletions src/components/atoms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export { default as SortButton } from './SortButton';
export { default as RadioButtonField } from './RadioButtonField';
export { default as Input } from './Input';
export { default as MiniButton } from './MiniButton';
export { default as ExclamationAlertSpan } from './ExclamationAlertSpan';
28 changes: 28 additions & 0 deletions src/components/templates/withLogin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React, { useEffect } from 'react';
import { useRouter } from 'next/router';

const withLogin = (InnerComponent: React.FC) => {
return () => {
const router = useRouter();
const token = localStorage.getItem('token');

const redirectToLogin: () => Promise<void> = async () => {
if (!token) {
alert('로그인이 필요합니다.');
try {
await router.push('/login');
} catch (error) {
console.error('로그인 체크 에러', error);
}
}
};

useEffect(() => {
void redirectToLogin();
}, [token]);

return token ? <InnerComponent /> : null;
};
};

export default withLogin;
1 change: 1 addition & 0 deletions src/hooks/queries/fridge/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as useGetIngredientList } from './useGetIngredientList';
9 changes: 9 additions & 0 deletions src/hooks/queries/fridge/useGetIngredientList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { IngredientType } from '@/types/fridge';
import { queryKeys } from '../queryKeys';
import { useBaseQuery } from '../useBaseQuery';

const useGetIngredientList = () => {
return useBaseQuery<IngredientType>(queryKeys.INGREDIENT(), '/ingrs');
};

export default useGetIngredientList;
5 changes: 5 additions & 0 deletions src/hooks/queries/queryKeys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const queryKeys = {
INGREDIENT: (id?: number) => ['ingredient', id] as const,
} as const;

export type QueryKeys = (typeof queryKeys)[keyof typeof queryKeys];
19 changes: 19 additions & 0 deletions src/hooks/queries/useBaseQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import axiosInstance from '@/api/axiosInstance';
import { useQuery } from '@tanstack/react-query';

export const fetchData = async <T>(url: string) => {
const response = await axiosInstance.get<{ data: T }>(url);
return response.data;
};

export const useBaseQuery = <T>(queryKey: any, url: string) => {
return useQuery({
queryKey,
queryFn: async () =>
await fetchData<T>(url)
.then((res) => res.data)
.catch((error) => {
console.error(error);
}),
});
};
Loading
Loading