Skip to content

Commit

Permalink
Merge pull request #59 from Soongsil-Developers/addLocalStorage
Browse files Browse the repository at this point in the history
[#57][#58] localStorage를 추가하여 token&slug 사용, header의 토큰 값에 따른 api요청 분할
  • Loading branch information
hoyyChoi authored Sep 15, 2022
2 parents 648a8e7 + 105e08f commit 4adf420
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 19 deletions.
20 changes: 19 additions & 1 deletion src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,29 @@ import Newarticle from './component/Newarticle';
import Profile from './component/Profile';
import ArticleDetail from './component/Article/ArticleDetail';
import {useRecoilState} from 'recoil'
import {authState} from './atoms/auth'
import {authState,userState} from './atoms/auth'
import { useEffect } from 'react';
import { getLoginUser } from './remote/index';

function App() {

const [auth,setAuth] = useRecoilState(authState)
const [user,setUserState] = useRecoilState(userState)

useEffect(()=>{
if(localStorage.getItem('token')){
getLoginUser()
.then(res=>{
setUserState(res.data.user)
setAuth(true)
}).catch(err=>{
console.log(err)
})

}else{
setAuth(false)
}
},[])

return (
<div>
Expand Down
3 changes: 2 additions & 1 deletion src/component/Article/ArticleDetail.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ const ArticleDetail = () => {

const slug = useRecoilValue(slugState)
const [data,setArticleData]=useState()


useEffect(()=>{
getSlugArticle(slug)
getSlugArticle(localStorage.getItem('slug'))
.then(res=>{
setArticleData(res.data.article)
})
Expand Down
1 change: 1 addition & 0 deletions src/component/ArticleList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const ArticleList = ({data}) => {
const navigate = useNavigate()
const [slug,setSlug] = useRecoilState(slugState)
const spaceArticle = () =>{
localStorage.setItem('slug',data.slug)
setSlug(data.slug)
navigate('/b')
}
Expand Down
17 changes: 13 additions & 4 deletions src/component/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,30 @@ import Populartags from './Populartags'
import { useRecoilValue } from 'recoil'
import { authState, userState } from '../atoms/auth'
import { useEffect,useState } from 'react'
import { getArticles } from '../remote/index'
import { getGlobalArticles,getGlobalLoginArticles } from '../remote/index'

const Home = () => {
let [articleData,setArticleData] = useState([])
const auth = useRecoilValue(authState)
const user = useRecoilValue(userState)

useEffect(()=>{
getArticles({user})
auth?(getGlobalLoginArticles()
.then(res=>{
setArticleData(res.data.articles)
}).catch(err=>{
console.log(err)
})
},[])
})):
(getGlobalArticles()
.then(res=>{
setArticleData(res.data.articles)
}).catch(err=>{
console.log(err)
}))

},[auth])


const tag = []
for(let i=0; i<articleData.length; i++){
for(let k=0; k<articleData[i].tagList.length; k++){
Expand Down
18 changes: 13 additions & 5 deletions src/component/Profile.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React, { useEffect,useState } from 'react'
import { useRecoilState,useRecoilValue } from 'recoil'
import { profileState,userState } from '../atoms/auth'
import { getProfile } from '../remote'
import { getArticles } from '../remote/index'
import { authState, profileState,userState } from '../atoms/auth'
import { getArticles,getLoginArticles } from '../remote/index'
import ArticleList from './ArticleList'


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

useEffect(()=>{
getArticles(profile.username,{user})
auth?(getLoginArticles(profile.username)
.then(res=>{
setArticleData(res.data.articles)

}).catch(err=>
console.log(err))
console.log(err)))
:
(getArticles(profile.username)
.then(res=>{
setArticleData(res.data.articles)

}).catch(err=>
console.log(err)))



// get을 호출 파라미터로 들어가는게 위에서는 user(나자신이름), or 상대방 이미지 눌렀을때 이름을 전달하는것, api주소
Expand Down
11 changes: 7 additions & 4 deletions src/component/Settings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useNavigate } from 'react-router-dom'
import { useState } from 'react'
import { getLoginUser } from '../remote'
import { useRecoilState,useSetRecoilState } from 'recoil'
import { authState, userState } from '../atoms/auth'
import { authState, userState, profileState } from '../atoms/auth'
import { useRecoilValue } from 'recoil'
import { putLoginUser } from '../remote'

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

const [user,setUser]= useRecoilState(userState)
const setAuth = useSetRecoilState(authState)
const [profile,setProfile] = useRecoilState(profileState)

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


const logOut = () =>{
localStorage.removeItem('token')
setAuth(false)
navigate("/")
}
Expand All @@ -34,10 +36,11 @@ const Settings = () => {
// console.log(user)

putLoginUser({...user,username:username1,email:useremail,bio:biotext})
.then(res=>
.then(res=>{
setUser(res.data.user)


setProfile(res.data.user)
navigate("/a")
}
).catch(err=>
console.log(err)
)
Expand Down
4 changes: 3 additions & 1 deletion src/component/Signin.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { postLoginUser } from '../remote'
import { authState,userState } from '../atoms/auth'
import { useRecoilState,useSetRecoilState } from 'recoil'


const Signin = () => {

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

const handleSubmit = (e)=>{
e.preventDefault()

postLoginUser({email,password})
.then((res)=>{
setUserState(res.data.user)
localStorage.setItem("token",res.data.user.token)
setAuth(true)
navigate('/')
}
Expand Down
1 change: 1 addition & 0 deletions src/component/Signup.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const Signup = () => {
postRegisterUser({username,email,password})
.then(res=>{
setUserState(res.data.user)
localStorage.setItem("token",res.data.user.token)
setAuth(true)
navigate('/')

Expand Down
92 changes: 89 additions & 3 deletions src/remote/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const postRegisterUser=(user)=>conduitAxios.post('/users',{user});
image: string
}}}
*/
const getLoginUser=()=>conduitAxios.get('/user');
const getLoginUser=()=>conduitAxios.get('/user',{headers:{authorization:`Bearer ${localStorage.getItem('token')}`}});


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

//Article

/**
@returns {{articles: [
{
slug: string;
title: string;
description: string;
body: string;
tagList: [
string
];
createdAt: 2022-09-11T06:38:57.899Z;
updatedAt: 2022-09-11T06:38:57.899Z;
favorited: true;
favoritesCount: 0;
author: {
username: string;
bio: string;
image: string;
following: true;
}
}
],
articlesCount: 0;
}}
*/

const getGlobalArticles = () => conduitAxios.get('/articles?limit=30&offset=0')


/**
@returns {{articles: [
{
slug: string;
title: string;
description: string;
body: string;
tagList: [
string
];
createdAt: 2022-09-11T06:38:57.899Z;
updatedAt: 2022-09-11T06:38:57.899Z;
favorited: true;
favoritesCount: 0;
author: {
username: string;
bio: string;
image: string;
following: true;
}
}
],
articlesCount: 0;
}}
*/

const getGlobalLoginArticles = () => conduitAxios.get(`/articles?limit=30&offset=0`,{headers:{authorization:`Bearer ${localStorage.getItem('token')}`}})

/**
@param {string} author
@returns {{articles: [
{
slug: string;
title: string;
description: string;
body: string;
tagList: [
string
];
createdAt: 2022-09-11T06:38:57.899Z;
updatedAt: 2022-09-11T06:38:57.899Z;
favorited: true;
favoritesCount: 0;
author: {
username: string;
bio: string;
image: string;
following: true;
}
}
],
articlesCount: 0;
}}
*/

const getArticles = (author) => conduitAxios.get(`/articles?author=${author}&limit=30&offset=0`)

/**
@param {string} author
@returns {{articles: [
{
Expand All @@ -119,7 +205,7 @@ const postRegisterUser=(user)=>conduitAxios.post('/users',{user});
}}
*/

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

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

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

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

0 comments on commit 4adf420

Please sign in to comment.