|
|
|
@ -1,136 +1,124 @@ |
|
|
|
|
|
|
|
//import {Request, Response} from "express";
|
|
|
|
const express = require('express'); |
|
|
|
const fs = require('fs'); |
|
|
|
const app = express(); |
|
|
|
const bodyParser = require('body-parser'); |
|
|
|
const cookieParser = require('cookie-parser'); |
|
|
|
const jwt = require('jsonwebtoken'); |
|
|
|
const expressJwt = require('express-jwt'); |
|
|
|
const cors = require('cors'); |
|
|
|
|
|
|
|
const fs = require('fs'); |
|
|
|
const app = express(); |
|
|
|
const upload = require('./backend/routes/upload-replay'); |
|
|
|
|
|
|
|
const AdvancableBuffer = require('./backend/utilities/AdvancableBuffer.js'); |
|
|
|
const gbxHeader = require('./backend/trackmania-replays/gbx-header.js'); |
|
|
|
|
|
|
|
// handling CORS
|
|
|
|
app.use((req, res, next) => { |
|
|
|
res.header("Access-Control-Allow-Origin", |
|
|
|
"http://localhost:4200"); |
|
|
|
res.header("Access-Control-Allow-Headers", |
|
|
|
"Origin, X-Requested-With, Content-Type, Accept"); |
|
|
|
next(); |
|
|
|
}); |
|
|
|
|
|
|
|
app.use(cors()); |
|
|
|
app.use(bodyParser.json()); |
|
|
|
app.route('/api/login').post(loginRoute); |
|
|
|
|
|
|
|
const RSA_PRIVATE_KEY = fs.readFileSync('./private.key'); |
|
|
|
//const RSA_PRIVATE_KEY = fs.readFileSync('./private.key');
|
|
|
|
const RSA_PRIVATE_KEY = "Secret_KeY"; |
|
|
|
const RSA_PUBLIC_KEY = fs.readFileSync('./public.key'); |
|
|
|
|
|
|
|
//const checkIfAuthenticated = expressJwt({
|
|
|
|
// secret: RSA_PUBLIC_KEY
|
|
|
|
//});
|
|
|
|
const users = [ |
|
|
|
{ id: 1, username: 'user1', password: 'password1' }, |
|
|
|
{ id: 2, username: 'user2', password: 'password2' } |
|
|
|
]; |
|
|
|
|
|
|
|
/*export */function loginRoute(req, res) { |
|
|
|
|
|
|
|
const email = req.body.email |
|
|
|
const password = req.body.password; |
|
|
|
|
|
|
|
if (validateEmailAndPassword(email, password)) |
|
|
|
{ |
|
|
|
const userId = findUserIdForEmail(email); |
|
|
|
|
|
|
|
const jwtBearerToken = jwt.sign({}, RSA_PRIVATE_KEY, { |
|
|
|
algorithm: 'RS256', |
|
|
|
expiresIn: 120, |
|
|
|
subject: userId |
|
|
|
}); |
|
|
|
|
|
|
|
res.status(200).json({ |
|
|
|
idToken: jwtBearerToken, |
|
|
|
expiresIn: 120 |
|
|
|
}); |
|
|
|
} |
|
|
|
else { |
|
|
|
// send status 401 Unauthorized
|
|
|
|
res.sendStatus(401); |
|
|
|
} |
|
|
|
} |
|
|
|
const seasons = [ |
|
|
|
{ id: 1, seasonName: "Season 1", seasonTag: "Post Winter Blues", seasonCardImage: "", seasonHeaderImage: "", seasonStartDate: "", seasonEndDate: "", seasonDesc: "",}, |
|
|
|
{ id: 2, seasonName: "Season 2", seasonTag: "Post Winter Blues", seasonCardImage: "", seasonHeaderImage: "", seasonStartDate: "", seasonEndDate: "", seasonDesc: "",} |
|
|
|
] |
|
|
|
|
|
|
|
//app.route('/api/session/create').get(checkIfAuthenticated, )
|
|
|
|
|
|
|
|
const upload = require('./backend/routes/upload-replay'); |
|
|
|
|
|
|
|
app.get('/api/seasons', (req, res) => { |
|
|
|
seasons = [{ |
|
|
|
const season_details = { |
|
|
|
details: { |
|
|
|
id: 1, |
|
|
|
seasonName: "Season 1", |
|
|
|
seasonTag: "Post Winter Blues", |
|
|
|
seasonCardImage: "", |
|
|
|
seasonHeaderImage: "", |
|
|
|
seasonStartDate: "", |
|
|
|
seasonEndDate: "", |
|
|
|
seasonSendDate: "", |
|
|
|
seasonId: "", |
|
|
|
seasonDesc: "", |
|
|
|
}, |
|
|
|
standings: [{ |
|
|
|
position: 1, |
|
|
|
points: 4, |
|
|
|
user: { |
|
|
|
realName: "Dan H", |
|
|
|
gamerHandle: "Quildra", |
|
|
|
} |
|
|
|
}, |
|
|
|
{ |
|
|
|
id: 2, |
|
|
|
seasonName: "Season 2", |
|
|
|
seasonTag: "Post Winter Blues", |
|
|
|
seasonCardImage: "", |
|
|
|
seasonHeaderImage: "", |
|
|
|
seasonStartDate: "", |
|
|
|
seasonEndDate: "", |
|
|
|
seasonDesc: "", |
|
|
|
position: 2, |
|
|
|
points: 2, |
|
|
|
user: { |
|
|
|
realName: "Dan Mc", |
|
|
|
gamerHandle: "Mini-Quildra", |
|
|
|
} |
|
|
|
}, |
|
|
|
], |
|
|
|
weeks:[{ |
|
|
|
id: "1", |
|
|
|
map: "bob", |
|
|
|
mapImg: "bob.jpg", |
|
|
|
entries: [ |
|
|
|
{ |
|
|
|
position: 1, |
|
|
|
runTime: 4.0, |
|
|
|
user: { |
|
|
|
realName: "Dan H", |
|
|
|
gamerHandle: "Quildra", |
|
|
|
} |
|
|
|
} |
|
|
|
] |
|
|
|
}] |
|
|
|
} |
|
|
|
|
|
|
|
function verifyToken(req, res, next) { |
|
|
|
const token = req.headers['authorization']; |
|
|
|
|
|
|
|
if (!token) { |
|
|
|
return res.status(403).json({ message: 'Token not provided' }); |
|
|
|
} |
|
|
|
|
|
|
|
jwt.verify(token, RSA_PRIVATE_KEY, (err, decoded) => { |
|
|
|
if (err) { |
|
|
|
return res.status(401).json({ message: 'Failed to authenticate token' }); |
|
|
|
} |
|
|
|
|
|
|
|
req.userId = decoded.userId; |
|
|
|
next(); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
app.post('/api/login', (req, res) => { |
|
|
|
const { username, password } = req.body; |
|
|
|
|
|
|
|
// Check if the user exists and the password is correct
|
|
|
|
const user = users.find(u => u.username === username && u.password === password); |
|
|
|
|
|
|
|
if (!user) { |
|
|
|
return res.status(401).json({ message: 'Invalid username or password' }); |
|
|
|
} |
|
|
|
|
|
|
|
// Generate and return a JWT token
|
|
|
|
const token = jwt.sign({ userId: user.id, username: user.username }, RSA_PRIVATE_KEY, { expiresIn: '1h' }); |
|
|
|
res.json({ token }); |
|
|
|
}); |
|
|
|
|
|
|
|
// Protected route example
|
|
|
|
app.get('/api/profile', verifyToken, (req, res) => { |
|
|
|
const user = users.find(u => u.id === req.userId); |
|
|
|
res.json({ username: user.username, userId: user.id }); |
|
|
|
}); |
|
|
|
|
|
|
|
app.get('/api/seasons', (req, res) => { |
|
|
|
res.json({seasons: seasons}); |
|
|
|
}); |
|
|
|
|
|
|
|
app.get('/api/seasons/:id', (req, res) => { |
|
|
|
data = { |
|
|
|
details: { |
|
|
|
id: 1, |
|
|
|
seasonName: "Season 1", |
|
|
|
seasonTag: "Post Winter Blues", |
|
|
|
seasonCardImage: "", |
|
|
|
seasonHeaderImage: "", |
|
|
|
seasonStartDate: "", |
|
|
|
seasonSendDate: "", |
|
|
|
seasonId: "", |
|
|
|
seasonDesc: "", |
|
|
|
}, |
|
|
|
standings: [{ |
|
|
|
position: 1, |
|
|
|
points: 4, |
|
|
|
user: { |
|
|
|
realName: "Dan H", |
|
|
|
gamerHandle: "Quildra", |
|
|
|
} |
|
|
|
}, |
|
|
|
{ |
|
|
|
position: 2, |
|
|
|
points: 2, |
|
|
|
user: { |
|
|
|
realName: "Dan Mc", |
|
|
|
gamerHandle: "Mini-Quildra", |
|
|
|
} |
|
|
|
}, |
|
|
|
], |
|
|
|
weeks:[{ |
|
|
|
id: "1", |
|
|
|
map: "bob", |
|
|
|
mapImg: "bob.jpg", |
|
|
|
entries: [ |
|
|
|
{ |
|
|
|
position: 1, |
|
|
|
runTime: 4.0, |
|
|
|
user: { |
|
|
|
realName: "Dan H", |
|
|
|
gamerHandle: "Quildra", |
|
|
|
} |
|
|
|
} |
|
|
|
] |
|
|
|
}] |
|
|
|
} |
|
|
|
res.json({data: data}); |
|
|
|
res.json({data: season_details}); |
|
|
|
}); |
|
|
|
|
|
|
|
// route for handling requests from the Angular client
|
|
|
|
|