Skip to content

Commit

Permalink
att: implement new cors system
Browse files Browse the repository at this point in the history
  • Loading branch information
Kruceo committed Jun 20, 2024
1 parent e0f7ab7 commit 62ce3ac
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 28 deletions.
6 changes: 4 additions & 2 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,19 @@ ENV DB_DIALECT=postgres

ENV CORS_ORIGIN=localhost

ENV CORS_SAME_FROM_REQUEST=false

ENV SECRET=2crows

ENV TEST=false

ENV TOKEN_EXPIRE_TIME=24h

ENV TESTMODE=0
ENV TESTMODE=false

RUN npm i

RUN echo "node ./tools/genConfigFile.mjs \$DB_USERNAME \$DB_PASSWORD \$DB_SCHEMA \$DB_PORT \$DB_HOST \$DB_DIALECT \$CORS_ORIGIN \$SECRET \$TOKEN_EXPIRE_TIME \$TESTMODE ./config/config.json" > start.sh
RUN echo "node ./tools/genConfigFile.mjs \$DB_USERNAME \$DB_PASSWORD \$DB_SCHEMA \$DB_PORT \$DB_HOST \$DB_DIALECT \$CORS_ORIGIN \$CORS_SAME_FROM_REQUEST \$SECRET \$TOKEN_EXPIRE_TIME \$TESTMODE ./config/config.json" > start.sh
RUN echo "node index.mjs" >> start.sh
RUN chmod +x start.sh

Expand Down
10 changes: 7 additions & 3 deletions backend/config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
"server": {
"port": 8080,
"cors": {
"origin": ["http://192.168.0.62:5173","http://localhost:5173"],
"credentials": true
"origin": [
"http://192.168.0.62:5173",
"http://localhost:5173"
],
"credentials": true,
"sameFromRequest":false
}
},
"security": {
Expand All @@ -22,5 +26,5 @@
"prefixLength": 6,
"sufixLength": 10
},
"test":false
"test": false
}
21 changes: 18 additions & 3 deletions backend/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,27 @@ import cookieParser from 'cookie-parser'
import cors from 'cors'
import universalRouter from './src/routers/v1/router.mjs'

// import https from 'https'
// const server = https.createServer({key:cfg.server.ssl.})

const app = express()

app.use(express.json({ limit: "2mb" }))
app.use(cookieParser())

app.use(cors({
origin: cfg.server.cors.origin,
credentials: cfg.server.cors.credentials // Certifique-se de configurar as credenciais como verdadeiras se estiver enviando cookies
origin: (origin, callback) => {
if (cfg.server.cors.sameFromRequest) {
callback(null, true)
return
}
if (!origin || cfg.server.cors.origin.includes(origin)) {
callback(null, true)
return
}
callback("", false)
},
credentials: cfg.server.cors.credentials
}));

app.use('/v1',
Expand All @@ -22,8 +35,10 @@ app.use('/v1',
)
app.use(authRouter)

app.get("/status",(req,res)=>{
app.get("/status", (req, res) => {
res.send("OK")
})



app.listen(cfg.server.port, () => console.log(`Server running in ${cfg.server.port}`))
28 changes: 14 additions & 14 deletions backend/src/database/tables.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,14 @@ const Transacao_item = dbserver.define("transacao_item", {
singular: "transacao_item"
}
})
await Produto.sync({ force: false })
await Usuario.sync({ force: false })
await Fornecedor.sync({ force: false })


await Bote.sync({ force: false })
await Transacao.sync({ force: false })
await Transacao_item.sync({ force: false })
// await Produto.sync({ force: false })
// await Usuario.sync({ force: false })
// await Fornecedor.sync({ force: false })
//
//
// await Bote.sync({ force: false })
// await Transacao.sync({ force: false })
// await Transacao_item.sync({ force: false })


/** Setup relations */
Expand Down Expand Up @@ -204,12 +204,12 @@ Usuario.hasMany(Transacao, { foreignKey: 'usuario_id' })

// Sync relations

await Produto.sync({ alter: true })
await Usuario.sync({ alter: true })
await Bote.sync({ alter: true })
await Fornecedor.sync({ alter: true })
await Transacao.sync({ alter: true })
await Transacao_item.sync({ alter: true })
// await Produto.sync({ alter: true })
// await Usuario.sync({ alter: true })
// await Bote.sync({ alter: true })
// await Fornecedor.sync({ alter: true })
// await Transacao.sync({ alter: true })
// await Transacao_item.sync({ alter: true })

export default {
Bote, Fornecedor, Produto, Transacao, Transacao_item, Usuario
Expand Down
2 changes: 1 addition & 1 deletion backend/src/routers/auth/loginRouter.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ authRouter.post("/auth/login", async (req, res) => {
authRouter.get("/auth/validate", async (req, res) => {

const token = req.cookies.token

if (!token) return res.status(statusCodes.Unauthorized)
.json({ error: true, message: "Sem as credenciais necessárias." })

Expand Down
11 changes: 6 additions & 5 deletions backend/tools/genConfigFile.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fs from 'fs'
const [, , db_username, db_password, db_schema, db_port, db_host, db_dialect, cors_origin, secret, tokenExpireTime, testmode, output] = process.argv
const [, , db_username, db_password, db_schema, db_port, db_host, db_dialect, cors_origin, cors_sameFromRequest, secret, tokenExpireTime, testmode, output] = process.argv

if (!db_username, !db_password, !db_schema, !db_port, !db_host, !db_dialect, !cors_origin, !secret, !tokenExpireTime, !testmode, !output) {
console.error("use args 'db_username' 'db_password' 'db_schema' 'db_host' 'db_dialect' 'cors_origin' 'secret' 'testmode' 'output'")
if (!db_username, !db_password, !db_schema, !db_port, !db_host, !db_dialect, !cors_origin, !cors_sameFromRequest, !secret, !tokenExpireTime, !testmode, !output) {
console.error("use args 'db_username' 'db_password' 'db_schema' 'db_host' 'db_dialect' 'cors_origin' 'cors_sameFromRequest' 'secret' 'testmode' 'output'")
process.exit()
}

Expand All @@ -19,7 +19,8 @@ const f = {
"port": 8080,
"cors": {
"origin": cors_origin.split(","),
"credentials": true
"credentials": true,
"sameFromRequest": cors_sameFromRequest == "true"
}
},
"security": {
Expand All @@ -30,7 +31,7 @@ const f = {
"prefixLength": 6,
"sufixLength": 10
},
"test": parseInt(testmode)
"test": testmode == "true"
}

fs.writeFileSync(output, JSON.stringify(f))

0 comments on commit 62ce3ac

Please sign in to comment.