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

[전수빈] week15 #545

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
47 changes: 47 additions & 0 deletions lib/api/auth.ts/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { ApiMapper } from "@/lib/apiMapper";
import request from "@/lib/axios";

interface UserData {
email: string;
password: string;
}

interface SaveTokenProps {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Props라는 이름은 properties를 축약한 것으로 react에서 html properties스펙에 맞게 지은 이름입니다.
이 타입은 html props가 아니니까 굳이 props라는 이름을 지을 필요가 없습니다. 차라리 PersistedTokens 같은 이름이 좋을 것 같아요

accessToken: string;
refreshToken: string;
}

export const signin = async (data: UserData) => {
try {
const response = await request.post(`${ApiMapper.auth.post.SIGN_IN}`, data);

if (response.status === 200) {
const { data } = response;
saveToken(data.data);
return true;
}
throw new Error();
} catch (e) {
return false;
Copy link
Collaborator

Choose a reason for hiding this comment

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

멘토링 시간때 에러핸들링 관련해서 말씀드렸던것처럼 이 함수에서는 차라리 error를 catch하지 않는 것이 좋습니다.

}
};

export const signup = async (data: UserData) => {
try {
const response = await request.post(`${ApiMapper.auth.post.SIGN_UP}`, data);

if (response.status === 200) {
const { data } = response;
saveToken(data.data);
Copy link
Collaborator

Choose a reason for hiding this comment

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

인증 관련 파일 내부에서 token관리를 하고 계시네요 관심사에 맞게 잘 하고 계십니다. 👍

return true;
}
throw new Error();
} catch (e) {
return false;
}
};

const saveToken = ({ accessToken, refreshToken }: SaveTokenProps) => {
localStorage.setItem("accessToken", accessToken);
localStorage.setItem("refreshToken", refreshToken);
};
11 changes: 3 additions & 8 deletions pages/signin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import GradientButton from "@/components/button/GradientButton";
import EmailInput from "@/components/input/EmailInput";
import PasswordInput from "@/components/input/PasswordInput";
import UserLayout from "@/components/user/UserLayout";
import { ApiMapper } from "@/lib/apiMapper";
import request from "@/lib/axios";
import { signin } from "@/lib/api/auth.ts/auth";
import { useRouter } from "next/router";
import { useEffect } from "react";
import { useForm } from "react-hook-form";
Expand Down Expand Up @@ -31,13 +30,9 @@ const Signin = () => {

const handleSignin = async (data: any) => {
try {
const result = await request.post(`${ApiMapper.auth.post.SIGN_IN}`, data);

if (result.status === 200) {
const { data } = result;
localStorage.setItem("accessToken", data.data.accessToken);
const result = await signin(data);
if (result) {
router.push("/folder");
return;
}
throw new Error();
} catch (e) {
Expand Down
7 changes: 3 additions & 4 deletions pages/signup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useRouter } from "next/router";
import { useEffect } from "react";
import { useForm } from "react-hook-form";
import { userData } from "./signin";
import { signup } from "@/lib/api/auth.ts/auth";

interface SignupData extends userData {
passwordConfirm: string;
Expand Down Expand Up @@ -92,11 +93,9 @@ const Signup = () => {
}

try {
const result = await request.post(`${ApiMapper.auth.post.SIGN_UP}`, data);
const result = await signup(data);

if (result.status === 200) {
const { data } = result;
localStorage.setItem("accessToken", data.data.accessToken);
if (result) {
router.push("/folder");
Copy link
Collaborator

Choose a reason for hiding this comment

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

localStorage관련 코드가 사라져서 훨씬 깔끔하네요.
redirect도 auth 관련 코드 내부에서 해주면 어떨까요?

signUp(data, {redirectTo: '/folder'});

return;
}
Expand Down