Skip to content

Commit

Permalink
fix(*): update token issue when user still logged in
Browse files Browse the repository at this point in the history
  • Loading branch information
HoseaCodes committed May 8, 2024
1 parent ea58a05 commit 8b8b30d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
21 changes: 14 additions & 7 deletions src/API/UserAPI.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const initialState = {
skills: [],
phone: "",
socialMedia: [],
websites: []
websites: [],
};

function UserAPI(token) {
Expand All @@ -25,7 +25,7 @@ function UserAPI(token) {
const [user, setUser] = useState(initialState);
const [authenticated, isAuthenticated] = useState(false);
const [cart, setCart] = useState([]);
const [cookies] = useCookies(["cookie-name"]);
const [cookies, setCookie, removeCookie] = useCookies(["cookie-name"]);

useEffect(() => {
if (cookies.accesstoken) setIsLoggedIn(true);
Expand All @@ -34,25 +34,32 @@ function UserAPI(token) {
const accesstoken = token ? token : cookies.accesstoken;
try {
const res = await axios.get("/api/user/info", {
headers: { Authorization: accesstoken }
headers: { Authorization: accesstoken },
});
console.log(res, "huh");
setIsLoggedIn(true);
isAuthenticated(true);
setUser(res.data.users);
res.data.users.role === 1 ? setIsAdmin(true) : setIsAdmin(false);
} catch (err) {
await axios.post("/api/user/logout");
localStorage.removeItem("firstLogin");
localStorage.removeItem("isLoggedIn");
removeCookie("accesstoken");
setUser(initialState);
setIsLoggedIn(false);
isAuthenticated(false);
alert(err.response.data.msg);
}
};
getUser();
}
}, [token, cookies]);

const addCart = async product => {
const addCart = async (product) => {
if (!isLoggedIn) return alert("Please login to continue buying");

const check = cart.every(item => {
const check = cart.every((item) => {
return item._id !== product._id;
});

Expand All @@ -63,7 +70,7 @@ function UserAPI(token) {
"/api/user/addcart",
{ cart: [...cart, { ...product, quantity: 1 }] },
{
headers: { Authorization: token }
headers: { Authorization: token },
}
);
} else {
Expand All @@ -78,7 +85,7 @@ function UserAPI(token) {
user: [user, setUser],
cart: [cart, setCart],
addCart: addCart,
authenticated: [authenticated, isAuthenticated]
authenticated: [authenticated, isAuthenticated],
};
}

Expand Down
6 changes: 5 additions & 1 deletion src/Pages/Articles/CreateArticle.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function CreatArticle() {
const [markdown, setMarkdown] = useState(articleTempltes[3].markdown);
const initialState = {
article_id: uuidv4(),
title: "",
title: "Demo",
subtitle: "",
description: "Description",
markdown: markdown,
Expand Down Expand Up @@ -150,6 +150,10 @@ function CreatArticle() {
categories: [selectedCategory],
id: user.id,
});
setArticle({
...article,
["slug"]: article.title.toLowerCase().replace(/ /g, "-"),
});
await axios.post("/api/articles", { ...article, images, ...user });
setImages(false);
setArticle(initialState);
Expand Down

0 comments on commit 8b8b30d

Please sign in to comment.