diff --git a/controllers/user.js b/controllers/user.js index 5a35b85b..0c2a33d7 100644 --- a/controllers/user.js +++ b/controllers/user.js @@ -20,10 +20,10 @@ async function register(req, res) { // Admin is role 1 const user = await Users.findOne({ email }) - if (user) return res.status(400).json({ msg: "The email already exists" }) + if (user) return res.status(409).json({ msg: "Conflict: The email already exists" }) if (password.length < 6) - return res.status(400).json({ msg: "Password is at least 6 characters long" }) + return res.status(401).json({ msg: "Password is at least 6 characters long" }) //Password Encryption const passwordHash = await bcrypt.hash(password, 10) @@ -70,7 +70,7 @@ function refreshToken(req, res) { async function login(req, res) { try { - const { email, password } = req.body + const { email, password, rememberMe } = req.body const user = await Users.findOne({ email }) if (!user) return res.status(400).json({ msg: "User does not exist." }) @@ -81,11 +81,14 @@ async function login(req, res) { const accesstoken = createAccessToken({ id: user._id }) const refreshtoken = createRefreshToken({ id: user._id }) - res.cookie('refreshtoken', refreshtoken, { - httpOnly: true, - path: '/api/user/refresh_token', - maxAge: 7 * 25 * 60 * 60 * 1000 - }) + if(rememberMe) { + // Only set cookies if user checks remember me + res.cookie('refreshtoken', refreshtoken, { + httpOnly: true, + path: '/api/user/refresh_token', + maxAge: 7 * 25 * 60 * 60 * 1000 + }) + } res.json({accesstoken }) } catch (err) { diff --git a/src/App.js b/src/App.js index 92166565..940e89e2 100755 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,5 @@ // import React, { Component, lazy, Suspense, useContext} from 'react'; -import React, { lazy, Suspense} from 'react'; +import React, { lazy, Suspense, useContext} from 'react'; import * as Sentry from "@sentry/react"; import Home from './Pages/Home/Home' import Projects from './Pages/Projects/Projects' @@ -7,7 +7,7 @@ import Articles from './Pages/Articles/Articles' import Error from './Pages/Error/Error' // import Shop from './Pages/Shop/Shop'; import { BrowserRouter, Switch, Route } from 'react-router-dom'; -// import CreateArticle from './Pages/Articles/CreateArticle' +import CreateArticle from './Pages/Articles/CreateArticle' import { DataProvider } from './GlobalState'; import ArticleItem from './Pages/Articles/Article/Article'; import ProjectItem from './Pages/Projects/Project/Project'; @@ -34,13 +34,12 @@ const Contact = lazy(() => import("./Pages/Contact/Contact")); // render() { -const App = () => { +const App = (props) => { const history = createBrowserHistory(); - // const state = useContext(GlobalState); - // const isLoggedIn = false - // const [isLoggedIn] = state.userAPI.isLoggedIn + const isLoggedIn = false + console.log(props, 'state') // const isAdmin = false // const [isAdmin] = state.userAPI.isAdmin @@ -49,7 +48,7 @@ const App = () => { {/* */} - ()}/> + ()}/> }> @@ -75,7 +74,7 @@ const App = () => { {/* Requires Login */} - {/* */} + {/* */} {/* Requires Login */} {/* Blog */} diff --git a/src/GlobalState.js b/src/GlobalState.js index 251a3e4c..b6db849c 100755 --- a/src/GlobalState.js +++ b/src/GlobalState.js @@ -44,6 +44,7 @@ export const DataProvider = ({ children }) => { dispatch: dispatch } + console.log(state) ProductsAPI(); return ( diff --git a/src/Pages/Articles/Articles.jsx b/src/Pages/Articles/Articles.jsx index e75811d2..2862d1d1 100755 --- a/src/Pages/Articles/Articles.jsx +++ b/src/Pages/Articles/Articles.jsx @@ -284,7 +284,7 @@ const Articles = () => {
{popularPosts.map(article => { return ( - +
{article.title}

) })} diff --git a/src/Pages/Articles/CreateArticle.jsx b/src/Pages/Articles/CreateArticle.jsx index d34400cd..5362c056 100755 --- a/src/Pages/Articles/CreateArticle.jsx +++ b/src/Pages/Articles/CreateArticle.jsx @@ -8,6 +8,7 @@ import marked from 'marked'; import { v4 as uuidv4 } from 'uuid'; import NavBar from '../../Components/NavBar/NavBar'; import Footer from '../../Components/Footer/Footer'; +import Error401 from '../Error/Error401' const initialState = { article_id: '', @@ -30,6 +31,24 @@ function CreatArticle() { const [articles] = state.articlesAPI.articles const [onEdit, setOnEdit] = useState(false) const [callback, setCallback] = state.articlesAPI.callback + const [isLoggedIn] = state.userAPI.isLoggedIn + + console.log(isLoggedIn) + function sleep(num) { + let now = new Date(); + const stop = now.getTime() + num; + while(true) { + now = new Date(); + if(now.getTime() > stop) return; + } + } + +useEffect(() => { + if (!isLoggedIn) { + sleep(5000) + history.push('/') + } + }, []) useEffect(() => { if (param.id) { @@ -112,146 +131,153 @@ function CreatArticle() { return ( <> - -
-
-
-
-
-

Add Article

-
-
-
-
-
- -
- -
-
+ {isLoggedIn ? + <> + +
+
+
+
+
+

Add Article

-
-
- -
- +
+ +
+
+ +
+ +
+
-
-
-
-
- -
- +
+
+ +
+ +
+
+
+
+
+ +
+ +
+
-
-
-
-
- -
- - - - - + + + + - + +
+
-
-
-
-
- -
- +
+
+ +
+ +
+
-
-
-
-
- -
+
+
+ +
- + - { - loading ? -
- : -
- - X -
+ { + loading ? +
+ : +
+ + X +
- } + } +
+
-
-
- {/* */} -
-
- -
-
Enter your markdown
- -
-
-
See the result
-
+ {/* */} +
+
+ +
+
Enter your markdown
+ +
+
+
See the result
+
+
+
-
-
-
-
-

- - -
+
+
+
+ + +
+
+
- +
-
-
-
-