Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Allowed Ipaddress (backend) #16

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions middleware/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ require('dotenv').config({ path: path.resolve(__dirname, '../.env') });
const config = {
port: process.env.PORT || 3000,
allowed: process.env.ALLOWED || 'http://localhost:8080',
allowed_ipaddress: process.env.ALLOWED_IPADDRESS,
// Database connection
db: {
client: process.env.DB_CLIENT || 'mysql',
Expand Down
43 changes: 43 additions & 0 deletions routes/ip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const cors = require('cors');
const express = require('express');
const app = express();
const config = require('../middleware/config');
const fs = require('fs');

// Set app headers to allow localhost:8080 to acces the api
let allowedOrigins = config.allowed.split(', ');
app.use(cors({
origin: allowedOrigins,
credentials: true
}));

// Get the allowed ip address
var allowedIpaddress = config.allowed_ipaddress;

// basic route at /api
app.get('/ip', (req, res) => {
res.status(200).send({
allowedIpaddress: 'The allowed ip of school',
});
});

app.post('/ip', (req, res) => {
// Get the ip address from the request
let ipaddress = req.body.ipaddress;

// Check if the ip address is allowed
if (allowedIpaddress == ipaddress) {
res.status(200).send({
allowedIp: true,
});
} else {
res.status(200).send({
allowedIp: false,
});
}
return;
});


// export the app
module.exports = app;