115 lines
3.3 KiB
JavaScript
115 lines
3.3 KiB
JavaScript
|
const bcrypt = require('bcrypt');
|
||
|
const jwt = require('jsonwebtoken');
|
||
|
const { Pool } = require('pg');
|
||
|
const pool = new Pool({
|
||
|
connectionString: process.env.DATABASE_URL,
|
||
|
});
|
||
|
|
||
|
|
||
|
async function registerUser(req, res) {
|
||
|
const { email, display_name, password } = req.body;
|
||
|
const hashedPassword = await bcrypt.hash(password, 10);
|
||
|
|
||
|
try {
|
||
|
const client = await pool.connect();
|
||
|
try {
|
||
|
const result = await client.query(
|
||
|
`INSERT INTO users (email, display_name, password_hash) VALUES ($1, $2, $3) RETURNING uid;`,
|
||
|
[email, display_name, hashedPassword]
|
||
|
);
|
||
|
const uid = result.rows[0].uid;
|
||
|
res.status(201).json({ uid });
|
||
|
} finally {
|
||
|
client.release();
|
||
|
}
|
||
|
} catch (error) {
|
||
|
console.error('Error registering user:', error);
|
||
|
res.status(500).json({ error: 'Server error' });
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async function authenticateUser(req, res) {
|
||
|
const { email, password } = req.body;
|
||
|
|
||
|
try {
|
||
|
const client = await pool.connect();
|
||
|
try {
|
||
|
const result = await client.query('SELECT uid, password_hash FROM users WHERE email = $1;', [email]);
|
||
|
const user = result.rows[0];
|
||
|
|
||
|
if (user && bcrypt.compareSync(password, user.password_hash)) {
|
||
|
const token = jwt.sign(
|
||
|
{ uid: user.uid, email: email },
|
||
|
process.env.JWT_SECRET,
|
||
|
{ expiresIn: '5d' }
|
||
|
);
|
||
|
res.status(200).json({ token });
|
||
|
} else {
|
||
|
res.status(401).json({ error: 'Invalid credentials' });
|
||
|
}
|
||
|
} finally {
|
||
|
client.release();
|
||
|
}
|
||
|
} catch (error) {
|
||
|
console.error('Error authenticating user:', error);
|
||
|
res.status(500).json({ error: 'Server error' });
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async function resetPassword(req, res) {
|
||
|
const { uid, email, new_password } = req.body;
|
||
|
const hashedPassword = await bcrypt.hash(new_password, 10);
|
||
|
|
||
|
try {
|
||
|
const client = await pool.connect();
|
||
|
try {
|
||
|
const result = await client.query(
|
||
|
'UPDATE users SET password_hash = $1 WHERE uid = $2 AND email = $3;',
|
||
|
[hashedPassword, uid, email]
|
||
|
);
|
||
|
if (result.rowCount === 0) {
|
||
|
return res.status(400).json({ error: 'User not found' });
|
||
|
}
|
||
|
res.status(200).json({ message: 'Password reset successfully' });
|
||
|
} finally {
|
||
|
client.release();
|
||
|
}
|
||
|
} catch (error) {
|
||
|
console.error('Error resetting password:', error);
|
||
|
res.status(500).json({ error: 'Server error' });
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async function getLeaderboard(req, res) {
|
||
|
try {
|
||
|
const client = await pool.connect();
|
||
|
try {
|
||
|
const result = await client.query(
|
||
|
'SELECT display_name, openskill_mu, current_elo, uid FROM users WHERE current_elo IS NOT NULL ORDER BY current_elo DESC;'
|
||
|
);
|
||
|
const players = result.rows;
|
||
|
|
||
|
const playerEloList = players.map(player => ({
|
||
|
player_name: player.display_name,
|
||
|
osk_mu: player.openskill_mu,
|
||
|
elo_rating: player.current_elo,
|
||
|
friend_code: player.uid
|
||
|
}));
|
||
|
|
||
|
res.status(200).json(playerEloList);
|
||
|
} finally {
|
||
|
client.release();
|
||
|
}
|
||
|
} catch (error) {
|
||
|
console.error('Error fetching leaderboard:', error);
|
||
|
res.status(500).json({ error: 'Server error' });
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = {
|
||
|
registerUser,
|
||
|
authenticateUser,
|
||
|
resetPassword,
|
||
|
getLeaderboard,
|
||
|
};
|