Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into feat/#113
  • Loading branch information
100Gyeon committed Jan 19, 2022
2 parents 1f4d6c9 + 1a94a88 commit 01bce2c
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 36 deletions.
13 changes: 11 additions & 2 deletions src/application/hooks/useLoginUser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { api } from '@api/index';
import { LoginUser } from '@api/types/user';
import { errorState } from '@stores/error';
import { authState, loginUserState } from '@stores/login-user';
import { useRecoilState } from 'recoil';
Expand All @@ -18,13 +19,20 @@ export function useLoginUser() {
setLoginUser({ id: -1, accessToken: '', username: '', userID: '', profileImage: '' });
};

const saveLoginUser = (loginUser: LoginUser) => {
setLoginUser(loginUser);
setIsAuthenticated(true);
console.log(loginUser);
localStorage.setItem('token', loginUser.accessToken);
};

const initLoginUser = async () => {
try {
const token = localStorage.getItem('token');
console.log(token);
if (!token) throw '토큰이 없습니다';
const user = await api.loginUserService.getUserInfo(token);
setLoginUser(user);
setIsAuthenticated(true);
saveLoginUser(user);
} catch (error) {
setError(error);
}
Expand All @@ -35,6 +43,7 @@ export function useLoginUser() {
setAccessToken,
removeAccessToken,
initLoginUser,
saveLoginUser,
isAuthenticated,
isLoading: !error && !isAuthenticated,
error: error,
Expand Down
4 changes: 2 additions & 2 deletions src/infrastructure/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { loginUserMock } from '../mock/login-user';
import { neososeoFormDataMock } from '../mock/neososeo-form';
import { userDataMock } from '../mock/user';
import { teamDataRemote } from '../remote/team';
Expand All @@ -8,6 +7,7 @@ import { NeogaService } from './neoga';
import { NeososeoFormService } from './neososeo-form';
import { TeamService } from './team';
import { UserService } from './user';
import { loginUserRemote } from '@infrastructure/remote/login-user';

export const api: APIService = getAPIMethod();

Expand All @@ -18,7 +18,7 @@ function getAPIMethod(): APIService {
function provideMockAPIService(): APIService {
const teamService = teamDataRemote();
const userService = userDataMock();
const loginUserService = loginUserMock();
const loginUserService = loginUserRemote();
const neogaService = NeogaDataRemote();
const neososeoFormService = neososeoFormDataMock();

Expand Down
2 changes: 1 addition & 1 deletion src/infrastructure/api/login-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface LoginUserService {

export const postLogin = async (
kakaoToken: string,
): Promise<{ user?: object; accesstoken: string; refreshtoken: string }> => {
): Promise<{ user?: any; accesstoken: string; refreshtoken?: string }> => {
try {
const response = await publicAPI.post({
url: `/auth/login`,
Expand Down
2 changes: 1 addition & 1 deletion src/infrastructure/api/types/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface LoginUser {
accessToken: string;
username: string;
userID: string;
profileImage: string;
profileImage: string | null;
}

export interface User {
Expand Down
17 changes: 17 additions & 0 deletions src/infrastructure/remote/login-user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { LoginUserService } from '@api/login-user';
import { publicAPI } from './base';

export function loginUserRemote(): LoginUserService {
const getUserInfo = async (token: string) => {
const response = await publicAPI.get({ url: '/user', headers: { accesstoken: token } });
if (response.status !== 200) throw '유저 조회 실패';
return {
id: response.data.id,
username: response.data.name,
userID: response.data.profileId,
profileImage: response.data.image,
accessToken: token,
};
};
return { getUserInfo };
}
43 changes: 23 additions & 20 deletions src/infrastructure/remote/team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ export function teamDataRemote(): TeamService {
const response = await privateAPI.get({ url: `/team` });
if (response.status === 200)
return {
profileListData: response.data.map((team: any) => ({
id: team.id,
profileImage: team.image ?? imgEmptyProfile,
profileName: team.name,
})),
profileListData: response.data
? response.data.map((team: any) => ({
id: team.id,
profileImage: team.image ?? imgEmptyProfile,
profileName: team.name,
}))
: [],
};
else throw '서버 통신 실패';
};
Expand Down Expand Up @@ -96,23 +98,24 @@ export function teamDataRemote(): TeamService {

const getMyTeamIssue = async () => {
const response = await privateAPI.get({ url: `/team/issue` });
console.log(response);
if (response.status === 200)
return {
issueListData: response.data.map((team: any) => ({
issueNumber: team.id,
issueMembers: team.feedback.map((member: any) => ({
id: member.userId,
profileName: member.name,
profileImage: member.image,
})),
category: team.categoryName,
createdAt: team.dates,
content: team.content,
teamID: team.teamId,
issueCardImage: team.teamImage,
teamName: team.teamname,
memberName: team.username,
})),
issueListData: response.data
? response.data.map((team: any) => ({
issueNumber: team.id,
issueMembers: team.feedback.map((member: any) => ({
id: member.userId,
profileName: member.name,
profileImage: member.image,
})),
category: team.categoryName,
createdAt: team.dates,
content: team.content,
teamName: team.teamname,
memberName: team.username,
}))
: [],
};
else throw '서버 통신 실패';
};
Expand Down
14 changes: 10 additions & 4 deletions src/presentation/components/JoinForm/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import React, { useEffect, useState } from 'react';
import {
StJoinForm,
Expand Down Expand Up @@ -28,7 +27,7 @@ function JoinForm() {
const [image, setImage] = useState<File | null>(null);
const [inputId, setInputId] = useState('');
const [inputName, setInputName] = useState('');
const { setAccessToken } = useLoginUser();
const { saveLoginUser } = useLoginUser();
const navigate = useNavigate();

useEffect(() => {
Expand Down Expand Up @@ -59,8 +58,15 @@ function JoinForm() {
form.append('accesstoken', accessToken);
form.append('refreshtoken', refreshToken);

const getData = await postJoin(form);
setAccessToken(getData.accesstoken);
const response = await postJoin(form);

saveLoginUser({
id: response.user,
accessToken: response.accesstoken,
username: response.user.name,
userID: response.user.profileId,
profileImage: response.user.image,
});
navigate('/joinComplete');
} catch (error) {
console.error(error); //나중에 또 처리합시다.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function ExpandListButton(props: ExpandListButtonProps) {
const { onClick, isExpanded } = props;
return (
<StExpandListButton onClick={onClick}>
<span>더보기</span>
<span>{isExpanded ? '접기' : '더보기'}</span>
{isExpanded ? <IcArrowViewMoreClose /> : <IcArrowViewMore />}
</StExpandListButton>
);
Expand Down
3 changes: 2 additions & 1 deletion src/presentation/components/common/HomeHeader/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ function HomeHeader() {

useEffect(() => {
if (!location) return;
setCurrentTab(location.pathname.split('/')[2]);
const selectedTab = location.pathname.split('/')[2];
setCurrentTab(selectedTab ?? 'neoga');
}, [location]);

/*
Expand Down
2 changes: 1 addition & 1 deletion src/presentation/pages/Home/Team/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function HomeTeam() {
)}
<StDivisionLine />
<h1>나와 관련된 이슈 확인</h1>
{issueListData ? (
{issueListData && issueListData.length ? (
<IssueCardList
issueListData={issueListData}
onIssueClick={(teamID, issueNumber) => {
Expand Down
8 changes: 5 additions & 3 deletions src/presentation/pages/OAuthRedirectHandler/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ import { useLoginUser } from '@hooks/useLoginUser';

const OAuthRedirectHandler = () => {
const navigate = useNavigate();
const { setAccessToken } = useLoginUser();
const { saveLoginUser } = useLoginUser();
const setKakaoAccessToken = useSetRecoilState(kakaoAccessTokenState);
const setKakaoRefreshToken = useSetRecoilState(kakaoRefreshTokenState);

useEffect(() => {
const code = new URL(window.location.href).searchParams.get('code') ?? ''; //인가코드
postLogin(code).then((response) => {
if (response.user) {
setAccessToken(response.accesstoken);
const { id, profileId, name, image } = response.user;
const accessToken = response.accesstoken;
saveLoginUser({ id, accessToken, username: name, userID: profileId, profileImage: image });
navigate('/home');
} else {
setKakaoAccessToken(response.accesstoken);
setKakaoRefreshToken(response.refreshtoken);
setKakaoRefreshToken(response.refreshtoken ?? '');
navigate('/join');
}
});
Expand Down

0 comments on commit 01bce2c

Please sign in to comment.