Skip to content

Commit

Permalink
fix(articles): added login validation and updated articles info
Browse files Browse the repository at this point in the history
  • Loading branch information
HoseaCodes committed Mar 20, 2023
1 parent be9f621 commit 7149f4c
Show file tree
Hide file tree
Showing 11 changed files with 435 additions and 300 deletions.
19 changes: 11 additions & 8 deletions controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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." })
Expand All @@ -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) {
Expand Down
15 changes: 7 additions & 8 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// 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'
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';
Expand All @@ -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

Expand All @@ -49,7 +48,7 @@ const App = () => {
<Switch>
<DataProvider>
{/* <NavBar/> */}
<Route exact path="/" render={() => (<Home/>)}/>
<Route exact path="/" render={() => (<Home />)}/>
<Suspense fallback={<ProLoader/>}>
<Route path="/about" exact component={About}/>
<Route path="/contact" exact component={Contact} />
Expand All @@ -75,7 +74,7 @@ const App = () => {
<Route path="/blog" exact component={Articles} />
<Route path="/blog/:id" exact component={ArticleItem} />
{/* Requires Login */}
{/* <Route path="/blog/new" exact component={!isLoggedIn ? CreateArticle : Error} /> */}
<Route path="/blog/new" exact component={CreateArticle} />
{/* <Route path="/blog/edit" exact component={!isLoggedIn ? CreateArticle : Error} /> */}
{/* Requires Login */}
{/* Blog */}
Expand Down
1 change: 1 addition & 0 deletions src/GlobalState.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const DataProvider = ({ children }) => {
dispatch: dispatch

}
console.log(state)
ProductsAPI();
return (
<GlobalState.Provider value={state}>
Expand Down
2 changes: 1 addition & 1 deletion src/Pages/Articles/Articles.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ const Articles = () => {
<section className='popular-articles'>
{popularPosts.map(article => {
return (
<a key={article.id} href={`/blog/${article._id}`} target="_blank"rel="noopener noreferrer" >
<a key={article.id} href={`/blog/${article._id}`} rel="noopener noreferrer" >
<div className="popular-link">{article.title}</div><br /></a>
)
})}
Expand Down
Loading

0 comments on commit 7149f4c

Please sign in to comment.