Skip to content

Commit e384fab

Browse files
author
Anonymous
committed
fontend authorization set user data in reducer
1 parent 5e6789b commit e384fab

File tree

12 files changed

+232
-33
lines changed

12 files changed

+232
-33
lines changed

client/package-lock.json

Lines changed: 103 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"@testing-library/jest-dom": "^5.14.1",
1111
"@testing-library/react": "^13.0.0",
1212
"@testing-library/user-event": "^13.2.1",
13+
"jsonwebtoken": "^9.0.2",
1314
"react": "^18.2.0",
1415
"react-dom": "^18.2.0",
1516
"react-hot-toast": "^2.4.1",

client/src/App.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import { RouterProvider, createBrowserRouter } from 'react-router-dom';
99
import CreateCategory from "./pages/Category/CreateCategory";
1010
import PostEdit from "./pages/PostEdit/PostEdit";
1111
import './App.css';
12+
import verifyCurrentUser from "./utilies/verifyCurrentUser";
13+
import { useEffect } from "react";
14+
import { useDispatch } from "react-redux";
15+
import { setCurrentUser } from "./services/reducer";
16+
17+
1218
const router = createBrowserRouter([
1319
{
1420
path: "/",
@@ -52,6 +58,12 @@ const router = createBrowserRouter([
5258
]);
5359

5460
function App() {
61+
const dispatch =useDispatch();
62+
const currentUser = verifyCurrentUser();
63+
useEffect(() => {
64+
dispatch(setCurrentUser(currentUser));
65+
}, [currentUser]);
66+
5567
return (
5668
<RouterProvider router={router} />
5769
// <Router>

client/src/components/TopBar/TopBar.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ const cookies = new Cookies();
1313
function TopBar() {
1414
const token = cookies.get('token');
1515
// getCurrentUser(token);
16-
const user = useSelector((state) => state);
16+
const user = useSelector((state) => state.user);
1717

18-
console.log(user)
18+
console.log(user.user.userId)
1919

2020
const [dropdownMenu, setDropdownMenu] = useState(false);
2121
const [canvasOpen, setCanvasOpen] = useState(false);

client/src/pages/Category/CreateCategory.jsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ export default function CreateCategory(){
4040
if(responseinfo.isSuccess){
4141
toast.success(responseinfo.data.message);
4242
}
43-
console.log(responseinfo)
4443

4544
return(
4645
<section className={classes.create_post_area}>

client/src/pages/Login/Login.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ export default function Login(){
3636
if (responseinfo.data.token) {
3737
defaultHeaders.append("Authorization", `Bearer ${responseinfo.data.token}`);
3838
}
39-
dispatch(setCurrentUser(responseinfo.data.token))
40-
// window.location.href = "/";
39+
dispatch(setCurrentUser(responseinfo.data.userData))
40+
window.location.href = "/";
4141
}
4242

4343
return(

client/src/services/authSlice.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@ export const authApi = createApi({
2121
body: credentials,
2222
}),
2323
invalidatesTags: ['Users'],
24-
onSuccess: (result, variables, context) => {
25-
console.log('res' + result)
26-
console.log('var' + variables)
27-
console.log('con' + context)
28-
// Dispatch the setUser action with the user data from the response
29-
context.dispatch(setCurrentUser(result.data.user));
30-
},
3124
}),
3225
logoutUser: builder.mutation({
3326
query: () => ({

client/src/services/store.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,4 @@ export const store = configureStore({
1515
getDefaultMiddleware().concat(authApi.middleware).concat(webApi.middleware)
1616
});
1717

18-
// Retrieve user data from cookies when the app loads
19-
// const cookies = await new Cookies();
20-
// const token = await cookies.get("token");
21-
22-
// if (token) {
23-
// console.log(token);
24-
// // Assuming your user data is stored as a JSON string in a cookie
25-
// store.dispatch(setCurrentUser(token)); // Dispatch setCurrentUser
26-
// }
2718
setupListeners(store.dispatch);

client/src/services/webSlice.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ export const webApi = createApi({
3636
query: (id) => ({url: `post?id=${id}`}),
3737
}),
3838
updatePost: builder.mutation({
39-
query: ({id, postObj}) => (
40-
console.log(id, postObj),
41-
{
39+
query: ({id, postObj}) => ({
4240
url: `post/${id}`,
4341
method: 'PUT',
4442
body: postObj,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// import jwt from 'jsonwebtoken';
2+
import Cookies from 'universal-cookie';
3+
4+
const verifyCurrentUser = () => {
5+
try{
6+
const cookies = new Cookies();
7+
const token = cookies.get('token');
8+
const base64Url = token.split('.')[1];
9+
const base64 = base64Url.replace('-', '+').replace('_', '/');
10+
return JSON.parse(window.atob(base64));
11+
}catch(error){
12+
return null;
13+
}
14+
}
15+
export default verifyCurrentUser;

0 commit comments

Comments
 (0)