Skip to content

Commit

Permalink
Feat : 로그인 통신
Browse files Browse the repository at this point in the history
- 사용자의 이메일, 비밀번호를 입력받고 토큰을 발급받는다.
related to #31
  • Loading branch information
future9061 committed Feb 4, 2024
1 parent 7a8bf37 commit 897be90
Show file tree
Hide file tree
Showing 17 changed files with 91 additions and 112 deletions.
22 changes: 0 additions & 22 deletions frontend/build/asset-manifest.json

This file was deleted.

Binary file removed frontend/build/favicon.ico
Binary file not shown.
1 change: 0 additions & 1 deletion frontend/build/index.html

This file was deleted.

Binary file removed frontend/build/logo192.png
Binary file not shown.
Binary file removed frontend/build/logo512.png
Binary file not shown.
54 changes: 0 additions & 54 deletions frontend/build/manifest.json

This file was deleted.

3 changes: 0 additions & 3 deletions frontend/build/robots.txt

This file was deleted.

12 changes: 11 additions & 1 deletion frontend/src/apis/BaseUrl.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
import axios from 'axios';

export default axios.create({ baseURL: 'https://api.doit-toeic.xyz' });
export default axios.create({
baseURL: 'https://api.doit-toeic.xyz',
headers: {
'Content-Type': 'application/json',
},
});

export const httpClientForCredentials = axios.create({
baseURL: 'https://api.doit-toeic.xyz',
withCredentials: true,
});
19 changes: 0 additions & 19 deletions frontend/src/apis/FetchRegister.ts

This file was deleted.

50 changes: 50 additions & 0 deletions frontend/src/apis/auth/FetchLogIn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { httpClientForCredentials } from '../BaseUrl';
import { LoginData } from '../../types/LoginData';
import { AxiosError, AxiosResponse } from 'axios';

export const onLogInSuccess = async (response: AxiosResponse) => {
const { accessToken, expiresIn } = await response.data.data;

httpClientForCredentials.defaults.headers.common['Authorization'] =
`Bearer ${accessToken}`;

const currentTime = new Date(Date.now());
const tokenExpirationTime = new Date(Date.now() + expiresIn * 1000);
const refreshTime = tokenExpirationTime.getTime() - currentTime.getTime();
console.log('accessToken', response.data.message);

setTimeout(() => {
FetchRefreshToken();
}, refreshTime);
};

export const FetchRefreshToken = async () => {
try {
const res = await httpClientForCredentials.get('/auth/refresh');

if (res.status === 200) {
onLogInSuccess(res);
console.log('refresh', res.data.message);
}
} catch (err) {
const axiosError = err as AxiosError;
if (axiosError.response?.status === 401) {
alert('로그인 해주세요');
}

throw new Error();
}
};

export const FetchLogIn = async (data: LoginData) => {
try {
const response = await httpClientForCredentials.post('/auth/login', data);

if (response.status === 200) {
onLogInSuccess(response);
}
} catch (err) {
console.log('로그인 에러 발생', err);
throw new Error();
}
};
16 changes: 16 additions & 0 deletions frontend/src/apis/auth/FetchRegister.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { RegisterData } from '../../types/RegisterData';
import url from '../BaseUrl';

export const FetchRegister = async (register: RegisterData) => {
const data = { email: register.email, password: register.password };

try {
const res = await url.post('/auth/register', data);
if (res.status === 201) {
alert('회원가입이 완료되었습니다.이메일을 확인해주세요!!');
}
} catch (error) {
alert('회원가입이 실패하였습니다.');
throw new Error();
}
};
2 changes: 1 addition & 1 deletion frontend/src/components/common/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { ButtonCSS } from '../../style/components/common/ButtonCSS';
import { useNavigate } from 'react-router-dom';
import { ButtonProps } from '../../types/ButtonPtops';
import { ButtonProps } from '../../types/ButtonProps';

function Button({
children,
Expand Down
16 changes: 9 additions & 7 deletions frontend/src/components/login/LoginForm.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
import React from 'react';
import { LoginFormCSS } from '../../style/components/login/LoginFormCSS';
import { SubmitHandler, useForm } from 'react-hook-form';
import { FormData } from '../../types/LoginData';
import { LoginData } from '../../types/LoginData';
import SubmitBtn from '../common/SubmitBtn';
import { FetchLogIn } from '../../apis/auth/FetchLogIn';

function LoginForm() {
const {
register,
handleSubmit,
formState: { errors },
} = useForm<FormData>();
} = useForm<LoginData>();

const onSubmit: SubmitHandler<FormData> = (data) => {
console.log(data);
const onsubmit: SubmitHandler<LoginData> = (data) => {
FetchLogIn(data);
};

return (
<>
<LoginFormCSS onSubmit={handleSubmit(onSubmit)}>
<LoginFormCSS onSubmit={handleSubmit(onsubmit)}>
<fieldset>
<label>ID</label>
<input
id="id"
spellCheck={false}
{...register('username', { required: true })}
{...register('email', { required: true })}
/>
</fieldset>
{errors.username && <span>아이디를 작성해주세요!</span>}
{errors.email && <span>아이디를 작성해주세요!</span>}

<fieldset>
<label>PW</label>
<input
id="password"
type="password"
spellCheck={false}
{...register('password', { required: true })}
/>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/register/RegisterForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SubmitHandler, useForm } from 'react-hook-form';
import { RegisterData } from '../../types/RegisterData';
import Agreement from './Agreement';
import SubmitBtn from '../common/SubmitBtn';
import { FetchRegister } from '../../apis/FetchRegister';
import { FetchRegister } from '../../apis/auth/FetchRegister';
import { useNavigate } from 'react-router-dom';

function RegisterForm() {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/style/components/common/ButtonCSS.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import styled from 'styled-components';
import { ButtonProps } from '../../../types/ButtonPtops';
import { ButtonProps } from '../../../types/ButtonProps';
import { media } from '../../mediaQuery';

export const ButtonCSS = styled.button<ButtonProps>`
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions frontend/src/types/LoginData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type FormData = {
username: string;
export type LoginData = {
email: string;
password: string;
};

0 comments on commit 897be90

Please sign in to comment.