Skip to content

Commit 105e08f

Browse files
committed
localStorage를 추가하여 token&slug 사용, header의 토큰 값에 따른 api요청 분할
1 parent 4bd3695 commit 105e08f

File tree

9 files changed

+148
-19
lines changed

9 files changed

+148
-19
lines changed

src/App.jsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,29 @@ import Newarticle from './component/Newarticle';
1010
import Profile from './component/Profile';
1111
import ArticleDetail from './component/Article/ArticleDetail';
1212
import {useRecoilState} from 'recoil'
13-
import {authState} from './atoms/auth'
13+
import {authState,userState} from './atoms/auth'
14+
import { useEffect } from 'react';
15+
import { getLoginUser } from './remote/index';
1416

1517
function App() {
1618

1719
const [auth,setAuth] = useRecoilState(authState)
20+
const [user,setUserState] = useRecoilState(userState)
21+
22+
useEffect(()=>{
23+
if(localStorage.getItem('token')){
24+
getLoginUser()
25+
.then(res=>{
26+
setUserState(res.data.user)
27+
setAuth(true)
28+
}).catch(err=>{
29+
console.log(err)
30+
})
31+
32+
}else{
33+
setAuth(false)
34+
}
35+
},[])
1836

1937
return (
2038
<div>

src/component/Article/ArticleDetail.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ const ArticleDetail = () => {
1111

1212
const slug = useRecoilValue(slugState)
1313
const [data,setArticleData]=useState()
14+
1415

1516
useEffect(()=>{
16-
getSlugArticle(slug)
17+
getSlugArticle(localStorage.getItem('slug'))
1718
.then(res=>{
1819
setArticleData(res.data.article)
1920
})

src/component/ArticleList.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const ArticleList = ({data}) => {
99
const navigate = useNavigate()
1010
const [slug,setSlug] = useRecoilState(slugState)
1111
const spaceArticle = () =>{
12+
localStorage.setItem('slug',data.slug)
1213
setSlug(data.slug)
1314
navigate('/b')
1415
}

src/component/Home.jsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,30 @@ import Populartags from './Populartags'
44
import { useRecoilValue } from 'recoil'
55
import { authState, userState } from '../atoms/auth'
66
import { useEffect,useState } from 'react'
7-
import { getArticles } from '../remote/index'
7+
import { getGlobalArticles,getGlobalLoginArticles } from '../remote/index'
88

99
const Home = () => {
1010
let [articleData,setArticleData] = useState([])
1111
const auth = useRecoilValue(authState)
1212
const user = useRecoilValue(userState)
1313

1414
useEffect(()=>{
15-
getArticles({user})
15+
auth?(getGlobalLoginArticles()
1616
.then(res=>{
1717
setArticleData(res.data.articles)
1818
}).catch(err=>{
1919
console.log(err)
20-
})
21-
},[])
20+
})):
21+
(getGlobalArticles()
22+
.then(res=>{
23+
setArticleData(res.data.articles)
24+
}).catch(err=>{
25+
console.log(err)
26+
}))
27+
28+
},[auth])
29+
30+
2231
const tag = []
2332
for(let i=0; i<articleData.length; i++){
2433
for(let k=0; k<articleData[i].tagList.length; k++){

src/component/Profile.jsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import React, { useEffect,useState } from 'react'
22
import { useRecoilState,useRecoilValue } from 'recoil'
3-
import { profileState,userState } from '../atoms/auth'
4-
import { getProfile } from '../remote'
5-
import { getArticles } from '../remote/index'
3+
import { authState, profileState,userState } from '../atoms/auth'
4+
import { getArticles,getLoginArticles } from '../remote/index'
65
import ArticleList from './ArticleList'
76

87

@@ -11,14 +10,23 @@ const Profile = () => {
1110
const profile = useRecoilValue(profileState)
1211
const user = useRecoilValue(userState)
1312
let [articleData,setArticleData] = useState([])
13+
const auth = useRecoilValue(authState)
1414

1515
useEffect(()=>{
16-
getArticles(profile.username,{user})
16+
auth?(getLoginArticles(profile.username)
1717
.then(res=>{
1818
setArticleData(res.data.articles)
1919

2020
}).catch(err=>
21-
console.log(err))
21+
console.log(err)))
22+
:
23+
(getArticles(profile.username)
24+
.then(res=>{
25+
setArticleData(res.data.articles)
26+
27+
}).catch(err=>
28+
console.log(err)))
29+
2230

2331

2432
// get을 호출 파라미터로 들어가는게 위에서는 user(나자신이름), or 상대방 이미지 눌렀을때 이름을 전달하는것, api주소

src/component/Settings.jsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useNavigate } from 'react-router-dom'
33
import { useState } from 'react'
44
import { getLoginUser } from '../remote'
55
import { useRecoilState,useSetRecoilState } from 'recoil'
6-
import { authState, userState } from '../atoms/auth'
6+
import { authState, userState, profileState } from '../atoms/auth'
77
import { useRecoilValue } from 'recoil'
88
import { putLoginUser } from '../remote'
99

@@ -14,6 +14,7 @@ const Settings = () => {
1414

1515
const [user,setUser]= useRecoilState(userState)
1616
const setAuth = useSetRecoilState(authState)
17+
const [profile,setProfile] = useRecoilState(profileState)
1718

1819
const [username1,setUsername]=useState(user.username)
1920
const [useremail,setUserEmail] = useState(user.email)
@@ -22,6 +23,7 @@ const Settings = () => {
2223

2324

2425
const logOut = () =>{
26+
localStorage.removeItem('token')
2527
setAuth(false)
2628
navigate("/")
2729
}
@@ -34,10 +36,11 @@ const Settings = () => {
3436
// console.log(user)
3537

3638
putLoginUser({...user,username:username1,email:useremail,bio:biotext})
37-
.then(res=>
39+
.then(res=>{
3840
setUser(res.data.user)
39-
40-
41+
setProfile(res.data.user)
42+
navigate("/a")
43+
}
4144
).catch(err=>
4245
console.log(err)
4346
)

src/component/Signin.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { postLoginUser } from '../remote'
55
import { authState,userState } from '../atoms/auth'
66
import { useRecoilState,useSetRecoilState } from 'recoil'
77

8+
89
const Signin = () => {
910

1011
let navigate = useNavigate()
@@ -21,10 +22,11 @@ const Signin = () => {
2122

2223
const handleSubmit = (e)=>{
2324
e.preventDefault()
24-
25+
2526
postLoginUser({email,password})
2627
.then((res)=>{
2728
setUserState(res.data.user)
29+
localStorage.setItem("token",res.data.user.token)
2830
setAuth(true)
2931
navigate('/')
3032
}

src/component/Signup.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const Signup = () => {
2222
postRegisterUser({username,email,password})
2323
.then(res=>{
2424
setUserState(res.data.user)
25+
localStorage.setItem("token",res.data.user.token)
2526
setAuth(true)
2627
navigate('/')
2728

src/remote/index.jsx

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const postRegisterUser=(user)=>conduitAxios.post('/users',{user});
5050
image: string
5151
}}}
5252
*/
53-
const getLoginUser=()=>conduitAxios.get('/user');
53+
const getLoginUser=()=>conduitAxios.get('/user',{headers:{authorization:`Bearer ${localStorage.getItem('token')}`}});
5454

5555

5656
/**
@@ -92,7 +92,93 @@ const postRegisterUser=(user)=>conduitAxios.post('/users',{user});
9292

9393
//Article
9494

95+
/**
96+
@returns {{articles: [
97+
{
98+
slug: string;
99+
title: string;
100+
description: string;
101+
body: string;
102+
tagList: [
103+
string
104+
];
105+
createdAt: 2022-09-11T06:38:57.899Z;
106+
updatedAt: 2022-09-11T06:38:57.899Z;
107+
favorited: true;
108+
favoritesCount: 0;
109+
author: {
110+
username: string;
111+
bio: string;
112+
image: string;
113+
following: true;
114+
}
115+
}
116+
],
117+
articlesCount: 0;
118+
}}
119+
*/
120+
121+
const getGlobalArticles = () => conduitAxios.get('/articles?limit=30&offset=0')
122+
123+
95124
/**
125+
@returns {{articles: [
126+
{
127+
slug: string;
128+
title: string;
129+
description: string;
130+
body: string;
131+
tagList: [
132+
string
133+
];
134+
createdAt: 2022-09-11T06:38:57.899Z;
135+
updatedAt: 2022-09-11T06:38:57.899Z;
136+
favorited: true;
137+
favoritesCount: 0;
138+
author: {
139+
username: string;
140+
bio: string;
141+
image: string;
142+
following: true;
143+
}
144+
}
145+
],
146+
articlesCount: 0;
147+
}}
148+
*/
149+
150+
const getGlobalLoginArticles = () => conduitAxios.get(`/articles?limit=30&offset=0`,{headers:{authorization:`Bearer ${localStorage.getItem('token')}`}})
151+
152+
/**
153+
@param {string} author
154+
@returns {{articles: [
155+
{
156+
slug: string;
157+
title: string;
158+
description: string;
159+
body: string;
160+
tagList: [
161+
string
162+
];
163+
createdAt: 2022-09-11T06:38:57.899Z;
164+
updatedAt: 2022-09-11T06:38:57.899Z;
165+
favorited: true;
166+
favoritesCount: 0;
167+
author: {
168+
username: string;
169+
bio: string;
170+
image: string;
171+
following: true;
172+
}
173+
}
174+
],
175+
articlesCount: 0;
176+
}}
177+
*/
178+
179+
const getArticles = (author) => conduitAxios.get(`/articles?author=${author}&limit=30&offset=0`)
180+
181+
/**
96182
@param {string} author
97183
@returns {{articles: [
98184
{
@@ -119,7 +205,7 @@ const postRegisterUser=(user)=>conduitAxios.post('/users',{user});
119205
}}
120206
*/
121207

122-
const getArticles = ({user}) => conduitAxios.get(`/articles?limit=30&offset=0`,{headers:{authorization:`Bearer ${user.token}`}})
208+
const getLoginArticles = (author) => conduitAxios.get(`/articles?author=${author}&limit=30&offset=0`,{headers:{authorization:`Bearer ${localStorage.getItem('token')}`}})
123209

124210
/**
125211
@param {{article: {
@@ -232,4 +318,4 @@ const getComment = (slug) => conduitAxios.get(`/articles/${slug}/comments`)
232318

233319
const postComment =(slug,comment,{user}) => conduitAxios.post(`/articles/${slug}/comments`,{comment},{headers:{authorization:`Bearer ${user.token}`}})
234320

235-
export {postRegisterUser,postLoginUser,getLoginUser,putLoginUser,getProfile,getArticles,createArticle,getSlugArticle,getComment,postComment};
321+
export {postRegisterUser,postLoginUser,getLoginUser,putLoginUser,getProfile,getGlobalLoginArticles,getGlobalArticles,getArticles,getLoginArticles,createArticle,getSlugArticle,getComment,postComment};

0 commit comments

Comments
 (0)