Skip to content

Commit cb02f23

Browse files
authored
Merge pull request #9 from maxchistt/api
Api (unfinished)
2 parents 8ddb824 + 309df67 commit cb02f23

File tree

13 files changed

+387
-128
lines changed

13 files changed

+387
-128
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ First of all, set up your project by creating `.env` file with next content:
1212

1313
```env
1414
mongoUri = "<YOUR MONGO URI>"
15+
jwtSecret = "<SECRET KEY>"
1516
```
1617

1718
Main available scripts in the project directory:
@@ -93,6 +94,7 @@ module.exports = {
9394
NODE_ENV: "production",
9495
mongoUri: "<YOUR MONGO URI>" /*replace this*/,
9596
httpsRedirect: false /*true if enable*/,
97+
jwtSecret: "<SECRET KEY>",
9698
},
9799
},
98100
],
@@ -108,4 +110,5 @@ module.exports = {
108110
- NODE_ENV: production
109111
- mongoUri: YOUR MONGO URI
110112
- httpsRedirect: true
113+
- jwtSecret: SECRET KEY
111114
- Click "Deploy branch" button

app.js

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,16 @@ const PORT = process.env.PORT || 5000
1010
const mongoUri = process.env.mongoUri
1111
const httpsRedirect = process.env.httpsRedirect || false
1212

13-
//temporary backend url
14-
const phpBaseUrl = 'https://php-server-notes.herokuapp.com/'
15-
1613
const app = express()
1714

1815
app.use(express.json({ extended: true }))
1916

20-
//app.use('/api/auth', require('./routes/auth.routes'))
17+
app.use('/api/auth', require('./routes/auth.routes'))
18+
app.use('/api/server', require('./routes/phpserver.routes'))
2119

22-
app.post('/server', function (req, res) {
23-
//console.log("backend redirect", req.url)
24-
res.redirect(307, phpBaseUrl)
25-
})
20+
if (httpsRedirect) app.use(httpToHttps)
2621

2722
if (!devMode) {
28-
if (httpsRedirect) app.use(httpToHttps)
2923
app.use('/', express.static(path.join(__dirname, 'client', 'build')))
3024
app.get('*', (req, res) => {
3125
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'))
@@ -38,16 +32,8 @@ if (!devMode) {
3832

3933
async function start() {
4034
try {
41-
if (mongoUri) {
42-
await mongoose.connect(mongoUri, {
43-
useNewUrlParser: true,
44-
useUnifiedTopology: true,
45-
useCreateIndex: true
46-
})
47-
} else {
48-
console.log("\n!!!NO MONGO URI!!!")
49-
}
50-
app.listen(PORT, () => logServerStart(PORT))
35+
connectMongo(mongoUri)
36+
app.listen(PORT, logServerStart)
5137
} catch (e) {
5238
console.log('Server Error', e.message)
5339
process.exit(1)
@@ -56,12 +42,25 @@ async function start() {
5642

5743
start()
5844

59-
function logServerStart(PORT) {
60-
dns.lookup(os.hostname(), (err, address, fam) => {
61-
const [logN, bef, af] = devMode ? ['Express server', ' ', ':'] : ['React Notes App', '-', '']
62-
console.log(`\n${logN} has been started`)
63-
console.log(`${bef} Local${af} http://localhost:${PORT}`)
64-
console.log(`${bef} On Your Network${af} http://${address}:${PORT}`)
45+
async function connectMongo(mongoUri) {
46+
if (mongoUri) {
47+
await mongoose.connect(mongoUri, {
48+
useNewUrlParser: true,
49+
useUnifiedTopology: true,
50+
useCreateIndex: true
51+
})
52+
} else {
53+
console.log("\n!!!NO MONGO URI!!!")
54+
}
55+
}
56+
57+
function logServerStart() {
58+
dns.lookup(os.hostname(), (err, address) => {
59+
const [logName, sBef, sAft] = devMode ? ['Express server', ' ', ':'] : ['React Notes App', '-', '']
60+
console.log(`\n${logName} has been started`)
61+
console.log(`${sBef} Local${sAft} http://localhost:${PORT}`)
62+
console.log(`${sBef} On Your Network${sAft} http://${address}:${PORT}`)
63+
if (err) console.log(err)
6564
})
6665
}
6766

client/package-lock.json

Lines changed: 43 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6-
"@testing-library/jest-dom": "^5.12.0",
7-
"@testing-library/react": "^11.2.6",
6+
"@testing-library/jest-dom": "^5.13.0",
7+
"@testing-library/react": "^11.2.7",
88
"@testing-library/user-event": "^12.8.3",
99
"bootstrap": "^4.6.0",
10-
"bootstrap-icons": "^1.4.1",
10+
"bootstrap-icons": "^1.5.0",
1111
"jquery": "^3.6.0",
1212
"react": "^17.0.2",
1313
"react-dom": "^17.0.2",
14-
"react-markdown": "^6.0.1",
14+
"react-markdown": "^6.0.2",
1515
"react-scripts": "4.0.3",
1616
"react-stack-grid": "^0.7.1",
17-
"react-textarea-autosize": "^8.3.2",
17+
"react-textarea-autosize": "^8.3.3",
1818
"remark-gfm": "^1.0.0",
1919
"web-vitals": "^0.2.4",
2020
"workbox-background-sync": "^5.1.4",

client/src/App.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import logo from './logo.svg';
2+
import logo from './Shared/Logo/logo.svg';
33
import './App.css';
44
import CardList from './Cards/CardList'
55
import AddCard from './Cards/AddCard'

client/src/Services/DataService.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import Card, { checkCardsArr } from '../Cards/cardType/Card'
44
export default function DataService() {
55
////////////////////////////////////////////////////////////
66
var user = null
7-
const nodeBackend = true
8-
const baseUrl = nodeBackend ? '/server/' : 'http://php-server-notes.std-1033.ist.mospolytech.ru/'
7+
const baseUrl = '/api/server/'
98

109
////////////////////////////////////////////////////////////
1110
function updDataServLogin(login) {
@@ -74,7 +73,7 @@ export default function DataService() {
7473
function checkData(data) {
7574
//console.log('start check data')
7675
try {
77-
if( data === null) console.log("null data");
76+
if (data === null) console.log("null data");
7877
return data === null || data === [] || checkCardsArr(data)
7978
} catch {
8079
return false
@@ -93,7 +92,7 @@ export default function DataService() {
9392
.then((d) => {
9493
let data = tryParce(d)//here we parce json
9594
//console.log("[DATA] from loadData(): ", data)
96-
if(!data)console.log("empty data from server");
95+
if (!data) console.log("empty data from server");
9796
if (!checkData(data)) {
9897
console.error("[loadData] Bad data format")
9998
console.log(data)
@@ -121,10 +120,10 @@ export default function DataService() {
121120
? Promise.reject(rej())
122121
: loadData())
123122
.then((d) => {
124-
if(!data)console.log("empty data to post");
125-
if(!d)console.log("empty loaded to check");
123+
if (!data) console.log("empty data to post");
124+
if (!d) console.log("empty loaded to check");
126125
let pDat = data === null ? (d || []) : data
127-
if(!pDat)console.log("empty will be posted");
126+
if (!pDat) console.log("empty will be posted");
128127
requestPostData(pDat).then(res, rej)
129128
})
130129
.catch(rej)
File renamed without changes.

middleware/auth.middleware.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const jwt = require('jsonwebtoken')
2+
require('dotenv').config()
3+
4+
module.exports = (req, res, next) => {
5+
if (req.method === 'OPTIONS') {
6+
return next()
7+
}
8+
9+
try {
10+
11+
const token = req.headers.authorization.split(' ')[1] // "Bearer TOKEN"
12+
13+
if (!token) {
14+
return res.status(401).json({ message: 'Нет авторизации' })
15+
}
16+
17+
const decoded = jwt.verify(token, process.env.jwtSecret)
18+
req.user = decoded
19+
next()
20+
21+
} catch (e) {
22+
res.status(401).json({ message: 'Нет авторизации' })
23+
}
24+
}

models/User.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const {Schema, model, Types} = require('mongoose')
2+
3+
const schema = new Schema({
4+
email: {type: String, required: true, unique: true},
5+
password: {type: String, required: true}
6+
})
7+
8+
module.exports = model('User', schema)

0 commit comments

Comments
 (0)