diff --git a/client/src/App.tsx b/client/src/App.tsx
index 76fcd23..7619eed 100755
--- a/client/src/App.tsx
+++ b/client/src/App.tsx
@@ -28,16 +28,11 @@ import { logOut } from './reducers/auth';
const App = () => {
useEffect(() => {
- // check for token in LS when app first runs
if (localStorage.token) {
- // if there is a token set axios headers for all requests
setAuthToken(localStorage.token);
}
- // try to fetch a user, if no token or invalid token we
- // will get a 401 response from our API
store.dispatch(loadUser());
- // log user out from all tabs if they log out in one tab
window.addEventListener("storage", () => {
if (!localStorage.token) store.dispatch(logOut);
});
@@ -80,17 +75,6 @@ const App = () => {
);
- /*
- return (
-
-
-
- } />
-
-
-
- );
- */
};
export default App;
diff --git a/client/src/components/profile-forms/ProfileForm.tsx b/client/src/components/profile-forms/ProfileForm.tsx
index c06581f..b92a700 100755
--- a/client/src/components/profile-forms/ProfileForm.tsx
+++ b/client/src/components/profile-forms/ProfileForm.tsx
@@ -82,7 +82,6 @@ const ProfileForm = () => {
const editing = profile ? true : false;
e.preventDefault();
await dispatch(createProfile(formData, editing)).then(() => {
- console.log(editing)
if (!editing) navigate("/dashboard");
});
};
diff --git a/client/src/store.ts b/client/src/store.ts
index ce6efbd..01a2418 100755
--- a/client/src/store.ts
+++ b/client/src/store.ts
@@ -25,13 +25,11 @@ store.subscribe(() => {
// if the token changes set the value in localStorage and axios headers
if (previousState.auth.token !== currentState.auth.token) {
const token = currentState.auth.token;
- if (typeof token === 'string') setAuthToken(token);
- else throw new Error("token not string")
+ setAuthToken(token);
}
});
-// Infer the `RootState` and `AppDispatch` types from the store itself
+
export type RootState = ReturnType
-// Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState}
export type AppDispatch = typeof store.dispatch
export default store;
diff --git a/client/src/utils/setAuthToken.ts b/client/src/utils/setAuthToken.ts
index db0c0bc..e7c63d4 100755
--- a/client/src/utils/setAuthToken.ts
+++ b/client/src/utils/setAuthToken.ts
@@ -2,7 +2,7 @@ import api from './api';
// store our JWT in LS and set axios headers if we do have a token
-const setAuthToken = (token: string) => {
+const setAuthToken = (token: any) => {
if (token) {
api.defaults.headers.common['x-auth-token'] = token;
localStorage.setItem('token', token);
diff --git a/middleware/auth.ts b/middleware/auth.ts
index a5ef3eb..152122e 100755
--- a/middleware/auth.ts
+++ b/middleware/auth.ts
@@ -2,7 +2,9 @@ import config from 'config'
import jwt from 'jsonwebtoken'
import type { Request, Response, NextFunction } from 'express';
-function auth(req: Request, res: Response, next: NextFunction) {
+interface ResponseAndUser extends Request { user: string }
+
+function auth(req: ResponseAndUser, res: Response, next: NextFunction) {
// Get token from header
const token = req.header('x-auth-token');
@@ -11,13 +13,14 @@ function auth(req: Request, res: Response, next: NextFunction) {
return res.status(401).json({ msg: 'No token, authorization denied' });
}
+ console.log('auth middlewarwe')
// Verify token
try {
jwt.verify(token, config.get('jwtSecret'), (error, decoded) => {
if (error) {
return res.status(401).json({ msg: 'Token is not valid' });
} else {
- if ('user' in req && decoded && typeof decoded !== "string")
+ if (decoded && typeof decoded !== "string")
req.user = decoded?.user;
next();
}
@@ -29,3 +32,4 @@ function auth(req: Request, res: Response, next: NextFunction) {
};
export default auth
+
diff --git a/package-lock.json b/package-lock.json
index 498f47b..db97fb8 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -13,6 +13,7 @@
"bcryptjs": "^2.4.3",
"config": "^3.3.9",
"cors": "^2.8.5",
+ "dotenv": "^16.3.1",
"express": "^4.18.2",
"express-validator": "^7.0.1",
"gravatar": "^1.8.2",
@@ -613,6 +614,17 @@
"node": ">=0.3.1"
}
},
+ "node_modules/dotenv": {
+ "version": "16.3.1",
+ "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.3.1.tgz",
+ "integrity": "sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==",
+ "engines": {
+ "node": ">=12"
+ },
+ "funding": {
+ "url": "https://github.com/motdotla/dotenv?sponsor=1"
+ }
+ },
"node_modules/dynamic-dedupe": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/dynamic-dedupe/-/dynamic-dedupe-0.3.0.tgz",
@@ -2601,6 +2613,11 @@
"resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz",
"integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A=="
},
+ "dotenv": {
+ "version": "16.3.1",
+ "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.3.1.tgz",
+ "integrity": "sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ=="
+ },
"dynamic-dedupe": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/dynamic-dedupe/-/dynamic-dedupe-0.3.0.tgz",
diff --git a/package.json b/package.json
index 237840e..b9bf995 100644
--- a/package.json
+++ b/package.json
@@ -17,6 +17,7 @@
"bcryptjs": "^2.4.3",
"config": "^3.3.9",
"cors": "^2.8.5",
+ "dotenv": "^16.3.1",
"express": "^4.18.2",
"express-validator": "^7.0.1",
"gravatar": "^1.8.2",
diff --git a/routers/api/auth.ts b/routers/api/auth.ts
index 5739931..cdb8d55 100755
--- a/routers/api/auth.ts
+++ b/routers/api/auth.ts
@@ -14,9 +14,10 @@ const router = express.Router();
// @route GET api/auth
// @desc Get user by token
// @access Private
-router.get("/", auth, async (req: Request, res) => {
+router.get("/", auth, async (req: any, res) => {
try {
let user: unknown = null
+ console.log(req.user)
if (isUserId(req)) {
user = await User.findById(req.user.id).select("-password");
res.json(user);
diff --git a/server.ts b/server.ts
index f0fd1b3..2de8570 100755
--- a/server.ts
+++ b/server.ts
@@ -2,7 +2,6 @@ import express from 'express'
import connectDB from './config/db'
-
import path from 'path'
const app = express();