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

Next Auth 카카오 로그인 #63

Merged
merged 19 commits into from
May 20, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 150 additions & 0 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 2 additions & 0 deletions @types/environment.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ namespace NodeJS {
NEXT_PUBLIC_MIXPANEL_ID: string;
NEXT_PUBLIC_HOTJAR_ID: string;
NEXT_PUBLIC_HOTJAR_SV: string;
KAKAO_CLIENT_ID: string;
KAKAO_CLIENT_SECRET: string;
}
}
2 changes: 1 addition & 1 deletion next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const nextConfig = {
appDir: false,
forceSwcTransforms: true,
},
pageExtensions: ['page.tsx'],
pageExtensions: ['page.tsx', 'page.ts'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

swcMinify: true,
compiler: {
emotion: true,
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@
"@sentry/nextjs": "^7.49.0",
"@tanstack/react-query": "^4.29.3",
"axios": "^1.3.5",
"dotenv": "^16.0.3",
"emotion-normalize": "^11.0.1",
"framer-motion": "^10.12.2",
"mixpanel-browser": "^2.46.0",
"next": "13.3.0",
"next-auth": "^4.22.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-hotjar": "^6.1.0"
Expand Down Expand Up @@ -114,4 +116,4 @@
"vitest": "^0.30.1",
"webpack": "^5.79.0"
}
}
}
63 changes: 63 additions & 0 deletions src/components/kakaoLoginButton/KakaoLogin.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { signIn, signOut, useSession } from 'next-auth/react';
import { cleanup, fireEvent, render, screen } from '@testing-library/react';
import { afterEach, beforeEach, describe, expect, type Mock, test, vi } from 'vitest';

import KakaoLoginButton from './KakaoLoginButton';

describe('components/kakaoLoginButton/KakaoLoginButton', () => {
vi.mock('next-auth/react', () => ({
signIn: vi.fn(),
signOut: vi.fn(),
useSession: vi.fn(),
}));

beforeEach(() => {
// useSession 함수의 반환 값을 설정
(useSession as Mock<unknown[]>).mockReturnValue({ data: null });
});

afterEach(cleanup);

test('정의되어 있어야 한다', () => {
expect(KakaoLoginButton).toBeDefined();
});

test('유저가 로그인하지 않은 경우에는 카카오로 로그인 하기 버튼을 렌더링한다', () => {
render(<KakaoLoginButton />);
expect(screen.getByRole('button')).toHaveTextContent('카카오로 로그인 하기');
});

test('유저가 로그인한 경우에는 유저를 환영하는 문구와 "로그아웃 하기" 버튼을 렌더링한다', () => {
(useSession as Mock<unknown[]>).mockReturnValue({
data: {
user: {
name: '오연',
},
},
});
render(<KakaoLoginButton />);
expect(screen.getByText('오연님 환영합니다.')).toBeInTheDocument();
expect(screen.getByRole('button')).toHaveTextContent('로그아웃 하기');
});

test('"로그아웃 하기" 버튼을 클릭하면 signOut 함수를 호출한다', () => {
(useSession as Mock<unknown[]>).mockReturnValue({
data: {
user: {
name: '오연',
},
},
});
render(<KakaoLoginButton />);
const logoutButton = screen.getByRole('button', { name: '로그아웃 하기' });
fireEvent.click(logoutButton);
expect(signOut).toHaveBeenCalledTimes(1);
});

test('"카카오로 로그인 하기" 버튼을 클릭하면 signIn 함수를 호출한다', () => {
render(<KakaoLoginButton />);
const loginButton = screen.getByRole('button', { name: '카카오로 로그인 하기' });
fireEvent.click(loginButton);
expect(signIn).toHaveBeenCalledWith('kakao');
});
});
21 changes: 21 additions & 0 deletions src/components/kakaoLoginButton/KakaoLoginButton.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { SessionProvider } from 'next-auth/react';
import { type Meta, type StoryObj } from '@storybook/react';

import KakaoLoginButton from './KakaoLoginButton';

const meta: Meta<typeof KakaoLoginButton> = {
title: 'KakaoLoginButton',
component: KakaoLoginButton,
};

export default meta;

type Story = StoryObj<typeof KakaoLoginButton>;

export const Primary: Story = {
render: () => (
<SessionProvider>
<KakaoLoginButton />
</SessionProvider>
),
};
26 changes: 26 additions & 0 deletions src/components/kakaoLoginButton/KakaoLoginButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react';
import { signIn, signOut, useSession } from 'next-auth/react';

const KakaoLoginButton = () => {
const { data: session } = useSession();

if (session) {
// TODO : 추후 서버로 session.user.name 과 session.user.email 정보를 보내줄 수 있음
return (
<>
<div>{session.user?.name}님 환영합니다.</div>
<button type="button" onClick={() => signOut()}>
로그아웃 하기
</button>
</>
);
}

return (
<button type="button" onClick={() => signIn('kakao')}>
카카오로 로그인 하기
</button>
);
};

export default KakaoLoginButton;
Loading