front and back work
This commit is contained in:
@@ -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 = () => {
|
||||
</Router>
|
||||
</Provider>
|
||||
);
|
||||
/*
|
||||
return (
|
||||
<Provider store={store}>
|
||||
<Router>
|
||||
<Routes>
|
||||
<Route path="login" element={<Login />} />
|
||||
</Routes>
|
||||
</Router>
|
||||
</Provider>
|
||||
);
|
||||
*/
|
||||
};
|
||||
|
||||
export default App;
|
||||
|
||||
@@ -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");
|
||||
});
|
||||
};
|
||||
|
||||
+2
-4
@@ -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<typeof store.getState>
|
||||
// Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState}
|
||||
export type AppDispatch = typeof store.dispatch
|
||||
|
||||
export default store;
|
||||
|
||||
@@ -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);
|
||||
|
||||
+6
-2
@@ -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
|
||||
|
||||
|
||||
Generated
+17
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
+2
-1
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user