mirror of
https://github.com/anatolykopyl/vk-bingo.git
synced 2026-03-26 04:44:26 +00:00
🚀 Создал бэкенд
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,3 +1,5 @@
|
|||||||
|
backend/names.json
|
||||||
|
|
||||||
.DS_Store
|
.DS_Store
|
||||||
node_modules
|
node_modules
|
||||||
/dist
|
/dist
|
||||||
|
|||||||
71
backend/index.js
Normal file
71
backend/index.js
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
const express = require('express')
|
||||||
|
const session = require('express-session')
|
||||||
|
const app = express()
|
||||||
|
const {MongoClient} = require('mongodb')
|
||||||
|
const MongoStore = require('connect-mongo')
|
||||||
|
const cors = require('cors')
|
||||||
|
require('dotenv').config()
|
||||||
|
|
||||||
|
const {verifyCaptcha} = require('./verify-captcha')
|
||||||
|
|
||||||
|
app.use(cors())
|
||||||
|
app.use(express.json())
|
||||||
|
app.use(express.urlencoded({ extended: true }))
|
||||||
|
|
||||||
|
const names = require('./names.json')
|
||||||
|
const client = new MongoClient(process.env.URI, { useUnifiedTopology: true })
|
||||||
|
|
||||||
|
app.use(session({
|
||||||
|
secret: process.env.SECRET,
|
||||||
|
resave: false,
|
||||||
|
saveUninitialized: true,
|
||||||
|
store: MongoStore.create({
|
||||||
|
client,
|
||||||
|
dbName: process.env.DB_NAME
|
||||||
|
}),
|
||||||
|
cookie: { maxAge: 1000 * 60 * 60 * 24 }
|
||||||
|
}))
|
||||||
|
|
||||||
|
client.connect()
|
||||||
|
|
||||||
|
app.post('/auth', async (req, res) => {
|
||||||
|
verifyCaptcha(req, res, async () => {
|
||||||
|
try {
|
||||||
|
const pass = req.body.pass
|
||||||
|
if (pass.toLowerCase() === process.env.PASSWORD) {
|
||||||
|
req.session.loggedIn = true
|
||||||
|
res.status(200).send("Logged in")
|
||||||
|
} else {
|
||||||
|
res.status(401).send("Wrong password")
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.log("Error: " + e)
|
||||||
|
res.status(500).send()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
app.get('/card', async (req, res) => {
|
||||||
|
if (req.session.loggedIn) {
|
||||||
|
try {
|
||||||
|
let card = await client.db(process.env.DB_NAME).collection('cards').aggregate([{ $sample: { size: 1 } }]).toArray()
|
||||||
|
card = card[0]
|
||||||
|
res.status(200).send(card)
|
||||||
|
} catch (e) {
|
||||||
|
console.log("Error: " + e)
|
||||||
|
res.status(500).send()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
res.status(403).send()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
app.get('/options', async (req, res) => {
|
||||||
|
if (req.session.loggedIn) {
|
||||||
|
res.status(200).send(names)
|
||||||
|
} else {
|
||||||
|
res.status(403).send()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
app.listen(process.env.PORT)
|
||||||
3809
backend/package-lock.json
generated
Normal file
3809
backend/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
23
backend/package.json
Normal file
23
backend/package.json
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
{
|
||||||
|
"name": "vk-bingo-backend",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"dev": "nodemon index.js"
|
||||||
|
},
|
||||||
|
"author": "Anatoly Kopyl",
|
||||||
|
"license": "ISC",
|
||||||
|
"devDependencies": {
|
||||||
|
"nodemon": "^2.0.7"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"axios": "^0.21.1",
|
||||||
|
"connect-mongo": "^4.4.1",
|
||||||
|
"cors": "^2.8.5",
|
||||||
|
"dotenv": "^8.2.0",
|
||||||
|
"express": "^4.17.1",
|
||||||
|
"express-session": "^1.17.1",
|
||||||
|
"mongodb": "^3.6.5"
|
||||||
|
}
|
||||||
|
}
|
||||||
22
backend/verify-captcha.js
Normal file
22
backend/verify-captcha.js
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
const axios = require('axios')
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
verifyCaptcha: function (req, res, cb) {
|
||||||
|
if (!req.body['g-recaptcha-response']) {
|
||||||
|
return res.status(400).send("No captcha")
|
||||||
|
}
|
||||||
|
|
||||||
|
const URL = "https://www.google.com/recaptcha/api/siteverify?secret=" + process.env.SECRET_KEY + "&response=" + req.body['g-recaptcha-response'] + "&remoteip=" + req.socket.remoteAddress
|
||||||
|
|
||||||
|
axios.get(URL).then(function (response) {
|
||||||
|
if (response.data.success !== undefined && !response.data.success) {
|
||||||
|
return res.status(429).send("Invalid captcha")
|
||||||
|
}
|
||||||
|
|
||||||
|
cb()
|
||||||
|
})
|
||||||
|
.catch(function (error) {
|
||||||
|
console.log(error);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user