diff --git a/.env.example b/.env.example index 24ec3af..915afad 100644 --- a/.env.example +++ b/.env.example @@ -12,8 +12,7 @@ SERVER_NAME=yourdomainename.com # your domain name, for the nginx configuration # mongodb setup DB_NAME=XXX -DB_USER=XXX -DB_PASSWORD=XXX +DB_PORT=XXX # cors CLIENT_ORIGIN=http://localhost:$REACT_PORT # URL of the frontend, to make a cors exception diff --git a/.github/workflows/prettier.yml b/.github/workflows/prettier.yml index ab3607f..6012a39 100644 --- a/.github/workflows/prettier.yml +++ b/.github/workflows/prettier.yml @@ -14,9 +14,6 @@ jobs: steps: - name: Checkout uses: actions/checkout@v3 - with: - # Make sure the actual branch is checked out when running on pull requests - ref: ${{ github.head_ref }} - name: Prettify code uses: creyD/prettier_action@v4.3 diff --git a/README.md b/README.md index dde87a9..1872fbc 100644 --- a/README.md +++ b/README.md @@ -70,8 +70,6 @@ cp .env.example .env.development After copying the example config of `.env`, you must fill in the missing information in this file. Check the example for more information. -> If you don't know how to deploy your database, consider using [Atlas](https://www.mongodb.com/atlas/database). - #### Backend From the root directory of the repository, do the following: diff --git a/backend/app.js b/backend/app.js index 6750ded..a188618 100644 --- a/backend/app.js +++ b/backend/app.js @@ -8,7 +8,7 @@ const articleRoutes = require('./routes/articles') const httpServer = createServer(app) require('dotenv').config({ path: `../.env.${process.env.NODE_ENV}` }) -const { DB_USER, DB_PASSWORD, DB_NAME, NODE_PORT, CLIENT_ORIGIN } = process.env +const { DB_PORT, DB_NAME, NODE_PORT, CLIENT_ORIGIN } = process.env app.use(cors({ origin: CLIENT_ORIGIN || 'http://localhost:3000' })) app.use(express.json({ limit: '1MB' })) @@ -19,10 +19,9 @@ app.get('/', (req, res) => { res.send('iscsc.fr is running') }) +uri = `mongodb://mongodb:${DB_PORT}/${DB_NAME}?retryWrites=true&w=majority` mongoose - .connect( - `mongodb+srv://${DB_USER}:${DB_PASSWORD}@iscsc.a11re32.mongodb.net/${DB_NAME}?retryWrites=true&w=majority` - ) + .connect(uri) .then(() => { httpServer.listen(NODE_PORT || 3001, () => { console.log(`Server listening: http://localhost:${NODE_PORT}`) diff --git a/docker-compose.yml b/docker-compose.yml index e21b480..d7a5c00 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,7 +1,23 @@ version: "3.8" services: + mongodb: + image: docker.io/bitnami/mongodb:4.4 + networks: + - database + restart: always + env_file: ./.env.production + ports: + - $DB_PORT:$DB_PORT + volumes: + - "./mongodb:/data/db" + node-app: + depends_on: + - mongodb + networks: + - proxy + - database build: ./backend restart: unless-stopped env_file: ./.env.production @@ -11,6 +27,8 @@ services: react-app: depends_on: - node-app + networks: + - proxy env_file: ./.env.production build: context: ./frontend @@ -19,9 +37,12 @@ services: nginx: restart: always + networks: + - proxy depends_on: - react-app - node-app + - mongodb build: context: ./nginx args: @@ -40,3 +61,7 @@ services: volumes: - ./certbot/www/:/var/www/certbot/:rw - ./certbot/conf/:/etc/letsencrypt/:rw + +networks: + proxy: + database: diff --git a/frontend/src/App.js b/frontend/src/App.js index ff1bc3c..e0fb724 100644 --- a/frontend/src/App.js +++ b/frontend/src/App.js @@ -16,20 +16,16 @@ function App() { <div className="pages"> <Routes> <Route path="/" element={<Navigate to="/blog" />} /> - <Route path="/blog" element={<Blog />} /> <Route path="/blog/create-article" - element={user ? <CreateArticle /> : <Navigate to="/login" />} + element={ + user ? <CreateArticle /> : <Login next="/blog/create-article" /> + } /> <Route path="/blog/:id" element={<Article />} /> - <Route - path="login" - element={!user ? <Login /> : <Navigate to="/" />} - /> - <Route - path="signup" - element={!user ? <Signup /> : <Navigate to="/" />} - /> + <Route path="/blog" element={<Blog />} /> + <Route path="/login" element={<Login next="/" />} /> + <Route path="/signup" element={<Signup next="/" />} /> </Routes> </div> </BrowserRouter> diff --git a/frontend/src/pages/Login.js b/frontend/src/pages/Login.js index 58f6753..cd7deb8 100644 --- a/frontend/src/pages/Login.js +++ b/frontend/src/pages/Login.js @@ -1,19 +1,22 @@ import { useNavigate } from 'react-router-dom' +import { useAuthContext } from '../hooks/useAuthContext' import { useLogin } from '../hooks/useLogin' - const { useState } = require('react') -const Login = () => { +const Login = ({ next }) => { + const navigate = useNavigate() + const { user } = useAuthContext() + if (user) navigate(next) + const [email, setEmail] = useState('') const [password, setPassword] = useState('') const { login, error, isLoading, ok } = useLogin() - const navigate = useNavigate() const handleSubmit = async e => { e.preventDefault() await login(email, password) if (ok) { - navigate('/') + navigate(next) } } diff --git a/frontend/src/pages/Signup.js b/frontend/src/pages/Signup.js index 34cdbcc..835154a 100644 --- a/frontend/src/pages/Signup.js +++ b/frontend/src/pages/Signup.js @@ -1,13 +1,17 @@ import { useNavigate } from 'react-router-dom' +import { useAuthContext } from '../hooks/useAuthContext' import { useSignup } from '../hooks/useSignup' const { useState } = require('react') -const Signup = () => { +const Signup = ({ next }) => { + const navigate = useNavigate() + const { user } = useAuthContext() + if (user) navigate(next) + const [email, setEmail] = useState('') const [username, setUsername] = useState('') const [password, setPassword] = useState('') const { signup, isLoading, error, ok } = useSignup() - const navigate = useNavigate() const handleSubmit = async e => { e.preventDefault()