30 lines
649 B
JavaScript
30 lines
649 B
JavaScript
const { getDatabase } = require('../mongodb');
|
|
|
|
const store = new Map();
|
|
|
|
function setToken(userId, token) {
|
|
store.set(userId, token);
|
|
}
|
|
|
|
async function getToken(userId) {
|
|
try {
|
|
const currentToken = store.get(userId) || '';
|
|
if (currentToken) {
|
|
return currentToken;
|
|
}
|
|
const db = await getDatabase("corp");
|
|
const [latest] = await db.collection("login-log").find({ accountId: userId, type: 'login' }).sort({ createTime: -1 }).limit(1).toArray();
|
|
if (latest) {
|
|
setToken(userId, latest.token);
|
|
}
|
|
return latest?.token || '';
|
|
} catch (e) {
|
|
return '';
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
setToken,
|
|
getToken
|
|
}
|