PSFPortal/api/user.js

66 lines
1.8 KiB
JavaScript
Raw Normal View History

2019-12-30 14:27:49 +00:00
import express from 'express'
import * as db from './db.js'
import { NEED_SESSION, get_pagination, fetch_user_middleware } from './util.js'
2019-12-30 14:27:49 +00:00
const api = express.Router();
api.param("user", fetch_user_middleware);
api.get('/user', NEED_SESSION, async (req, res, next) => {
2019-12-30 14:27:49 +00:00
try {
const account = await db.get_account_by_id(req.session.account_id);
res.status(200).json({ id: account.id, name: account.username, admin: account.gm });
2019-12-30 14:27:49 +00:00
} catch (e) {
console.log(e);
res.status(500).json({ message: 'error' });
}
});
api.get('/user/:user/profile', NEED_SESSION, async (req, res, next) => {
2019-12-30 18:20:50 +00:00
const target_account = req.user;
if (target_account.id !== req.session.account_id && !req.session_account.gm) {
res.status(403).json({ message: 'not allowed to see for other users' });
return;
}
2019-12-30 14:27:49 +00:00
try {
2019-12-30 18:20:50 +00:00
const account = await db.get_account_by_id(target_account.id);
const characters = await db.get_characters_by_account(target_account.id);
2019-12-30 14:27:49 +00:00
res.status(200).json({
id: account.id,
2019-12-30 14:27:49 +00:00
name: account.username,
//email : account.email, // TODO
email: "N/A",
account_created: account.created,
admin: account.gm,
2019-12-30 18:50:32 +00:00
inactive: account.inactive,
2019-12-30 14:27:49 +00:00
characters: characters,
});
} catch (e) {
console.log(e);
res.status(500).json({ message: 'error' });
}
});
api.get('/user/:user/logins', NEED_SESSION, async (req, res, next) => {
2019-12-30 14:27:49 +00:00
const account = req.user;
const pagination = get_pagination(req);
2019-12-30 18:20:50 +00:00
if (account.id !== req.session.account_id && !req.session_account.gm) {
2019-12-30 14:27:49 +00:00
res.status(403).json({ message: 'not allowed to see for other users' });
return;
}
try {
const logins = await db.get_account_logins(account.id, pagination)
res.status(200).json({ logins: logins, page: pagination });
2019-12-30 14:27:49 +00:00
} catch (e) {
console.log(e)
res.status(500).json({ message: 'error' });
}
});
export default api;