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

[4주차 기본/심화/공유 과제] 회원가입 & 로그인 #5

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
25 changes: 25 additions & 0 deletions week4-assignment/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
.env
6 changes: 6 additions & 0 deletions week4-assignment/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"singleQuote": true,
"semi": true,
"trailingComma": "es5",
"tabWidth": 2
}
50 changes: 50 additions & 0 deletions week4-assignment/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# React + TypeScript + Vite

This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.

Currently, two official plugins are available:

- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh

## Expanding the ESLint configuration

If you are developing a production application, we recommend updating the configuration to enable type aware lint rules:

- Configure the top-level `parserOptions` property like this:

```js
export default tseslint.config({
languageOptions: {
// other options...
parserOptions: {
project: ['./tsconfig.node.json', './tsconfig.app.json'],
tsconfigRootDir: import.meta.dirname,
},
},
})
```

- Replace `tseslint.configs.recommended` to `tseslint.configs.recommendedTypeChecked` or `tseslint.configs.strictTypeChecked`
- Optionally add `...tseslint.configs.stylisticTypeChecked`
- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and update the config:

```js
// eslint.config.js
import react from 'eslint-plugin-react'

export default tseslint.config({
// Set the react version
settings: { react: { version: '18.3' } },
plugins: {
// Add the react plugin
react,
},
rules: {
// other rules...
// Enable its recommended rules
...react.configs.recommended.rules,
...react.configs['jsx-runtime'].rules,
},
})
```
28 changes: 28 additions & 0 deletions week4-assignment/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import js from '@eslint/js'
import globals from 'globals'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'
import tseslint from 'typescript-eslint'

export default tseslint.config(
{ ignores: ['dist'] },
{
extends: [js.configs.recommended, ...tseslint.configs.recommended],
files: ['**/*.{ts,tsx}'],
languageOptions: {
ecmaVersion: 2020,
globals: globals.browser,
},
plugins: {
'react-hooks': reactHooks,
'react-refresh': reactRefresh,
},
rules: {
...reactHooks.configs.recommended.rules,
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
},
)
13 changes: 13 additions & 0 deletions week4-assignment/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>회원가입 & 로그인</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
36 changes: 36 additions & 0 deletions week4-assignment/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "week4-assignment",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc -b && vite build",
"lint": "eslint .",
"preview": "vite preview"
},
"dependencies": {
"axios": "^1.7.7",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-icons": "^5.3.0",
"react-router-dom": "^6.28.0",
"styled-components": "^6.1.13"
},
"devDependencies": {
"@eslint/js": "^9.13.0",
"@types/axios": "^0.14.4",
"@types/react": "^18.3.12",
"@types/react-dom": "^18.3.1",
"@types/styled-components": "^5.1.34",
"@vitejs/plugin-react": "^4.3.3",
"eslint": "^9.13.0",
"eslint-plugin-react-hooks": "^5.0.0",
"eslint-plugin-react-refresh": "^0.4.14",
"globals": "^15.11.0",
"prettier": "^3.4.2",
"typescript": "~5.6.2",
"typescript-eslint": "^8.11.0",
"vite": "^5.4.10"
}
}
1 change: 1 addition & 0 deletions week4-assignment/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions week4-assignment/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { createBrowserRouter, RouterProvider } from 'react-router-dom';
import { ThemeProvider } from 'styled-components';
import GlobalStyle from './styles/GlobalStyle';
import theme from './styles/theme';

import LoginPage from './pages/LoginPage';
import SignupPage from './pages/SignupPage';
import MypagePage from './pages/MypagePage';

const router = createBrowserRouter([
{
path: "/",
element: <LoginPage />,
},
{
path: "/signup",
element: <SignupPage />,
},
{
path: "/mypage",
element: <MypagePage />,
},
]);

function App() {
return (
<ThemeProvider theme={theme}>
<GlobalStyle />
<RouterProvider router={router} />
</ThemeProvider>
);
}

export default App;
22 changes: 22 additions & 0 deletions week4-assignment/src/apis/authApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { AxiosResponse } from 'axios';
import axiosInstance from './axiosInstance';
import { SignupData, LoginData, LoginResponse } from '../types/types';

export const signupApi = async (signupData: SignupData) => {
const response = await axiosInstance.post('/user', {
username: signupData.username,
password: signupData.password,
hobby: signupData.hobby,
});
return response;
};

export const loginApi = async (
loginData: LoginData
): Promise<AxiosResponse<LoginResponse>> => {
const response = await axiosInstance.post('/login', {
username: loginData.username,
password: loginData.password,
});
return response;
};
10 changes: 10 additions & 0 deletions week4-assignment/src/apis/axiosInstance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import axios from 'axios';

const axiosInstance = axios.create({
baseURL: import.meta.env.VITE_API_BASE_URL,
headers: {
'Content-Type': 'application/json',
},
});

export default axiosInstance;
27 changes: 27 additions & 0 deletions week4-assignment/src/apis/hobbyApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { AxiosResponse } from 'axios';
import axiosInstance from './axiosInstance';
import { MyHobbyResponse, OtherHobbyResponse } from '../types/types';

export const fetchMyHobby = async (token: string) => {
const response: AxiosResponse<MyHobbyResponse> = await axiosInstance.get(
'/user/my-hobby',
{ headers: { token } }
);

if (response.status === 200) {
return response.data.result.hobby;
}
throw new Error('취미 정보를 가져오지 못했습니다.');
};

export const fetchOtherHobby = async (token: string, no: string) => {
const response: AxiosResponse<OtherHobbyResponse> = await axiosInstance.get(
`/user/${no}/hobby`,
{ headers: { token } }
);

if (response.status === 200) {
return `${no}번 사용자의 취미: ${response.data.result.hobby}`;
}
throw new Error('취미 정보를 가져오지 못했습니다.');
};
16 changes: 16 additions & 0 deletions week4-assignment/src/apis/infoApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { AxiosResponse } from 'axios';
import axiosInstance from './axiosInstance';
import { UpdateRequestData, InfoResponse } from '../types/types';

export const updateUserInfo = async (
token: string,
data: UpdateRequestData
): Promise<AxiosResponse<InfoResponse>> => {
const response: AxiosResponse<InfoResponse> = await axiosInstance.put(
'/user',
data,
{ headers: { token } }
);

return response;
};
75 changes: 75 additions & 0 deletions week4-assignment/src/components/Login/Login.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { useState } from 'react';
import {
Container,
Title,
InputWrapper,
Input,
Button,
BottomButton,
} from './LoginStyles';
import { useNavigate } from 'react-router-dom';

interface LoginData {
username: string;
password: string;
}

interface LoginProps {
onLogin: (loginData: LoginData) => void;
}

const Login = ({ onLogin }: LoginProps) => {
const navigate = useNavigate();
const [loginData, setLoginData] = useState<LoginData>({
username: '',
password: '',
});

const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const { name, value } = e.target;
setLoginData((prevData) => ({
...prevData,
[name]: value,
}));
};

const handleLogin = () => {
if (loginData.username && loginData.password) {
onLogin(loginData);
}
};

const handleSignup = () => {
navigate('/signup');
};

return (
<Container>
<Title>로그인</Title>
<InputWrapper>
<Input
type="text"
id="username"
name="username"
value={loginData.username}
onChange={handleChange}
placeholder="아이디"
/>
</InputWrapper>
<InputWrapper>
<Input
type="password"
id="password"
name="password"
value={loginData.password}
onChange={handleChange}
placeholder="비밀번호"
/>
</InputWrapper>
<Button onClick={handleLogin}>로그인</Button>
<BottomButton onClick={handleSignup}>회원가입</BottomButton>
</Container>
);
};

export default Login;
Loading