r2.1
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
const { Op } = require('sequelize');
|
||||
const bcrypt = require('bcryptjs');
|
||||
const jwt = require('jsonwebtoken');
|
||||
const crypto = require('crypto');
|
||||
@@ -60,22 +61,27 @@ async function createDeviceToken(models, userId, orgId, deviceName, deviceType)
|
||||
return { rawToken, record };
|
||||
}
|
||||
|
||||
async function login(models, req, { username, password, rememberDevice, deviceName, deviceType }) {
|
||||
checkLockout(username);
|
||||
async function login(models, req, { identifier, password, rememberDevice, deviceName, deviceType }) {
|
||||
checkLockout(identifier);
|
||||
|
||||
const user = await models.User.findOne({
|
||||
where: { username },
|
||||
where: {
|
||||
[Op.or]: [
|
||||
{ username: identifier },
|
||||
{ email: identifier },
|
||||
],
|
||||
},
|
||||
attributes: ['id', 'username', 'email', 'role', 'organizationId', 'isActive', 'password'],
|
||||
});
|
||||
|
||||
const valid = user && user.isActive && await bcrypt.compare(password, user.password);
|
||||
|
||||
if (!valid) {
|
||||
recordFailure(username);
|
||||
recordFailure(identifier);
|
||||
throw new AppError('LOGIN_FAILED', 'Invalid username or password.', 401);
|
||||
}
|
||||
|
||||
clearFailures(username);
|
||||
clearFailures(identifier);
|
||||
|
||||
await user.update({ lastLoginAt: new Date() });
|
||||
|
||||
@@ -89,7 +95,7 @@ async function login(models, req, { username, password, rememberDevice, deviceNa
|
||||
|
||||
const userPublic = { id: user.id, username: user.username, role: user.role, organizationId: user.organizationId };
|
||||
|
||||
await auditLog(req, 'auth.login.success', 'user', user.id, { username });
|
||||
await auditLog(req, 'auth.login.success', 'user', user.id, { identifier });
|
||||
|
||||
return { jwt: token, user: userPublic, ...(deviceToken ? { deviceToken } : {}) };
|
||||
}
|
||||
|
||||
@@ -49,6 +49,7 @@ router.patch('/', authenticateToken, authenticateRole(OWNERS), validate(updateOr
|
||||
|
||||
router.get('/branding', authenticateToken, async (req, res, next) => {
|
||||
try {
|
||||
if (!req.user.organizationId) return ok(res, {});
|
||||
const branding = await svc.getBranding(models, req.user.organizationId);
|
||||
ok(res, branding);
|
||||
} catch (err) { next(err); }
|
||||
|
||||
@@ -39,6 +39,13 @@ router.post('/', authenticateToken, authenticateRole(COACH_PLUS), validate(creat
|
||||
} catch (err) { next(err); }
|
||||
});
|
||||
|
||||
router.get('/session/active', authenticateToken, authenticateRole(COACH_PLUS), async (req, res, next) => {
|
||||
try {
|
||||
const session = svc.getActiveSession(req.user.organizationId);
|
||||
ok(res, session);
|
||||
} catch (err) { next(err); }
|
||||
});
|
||||
|
||||
router.get('/:id', authenticateToken, authenticateRole(COACH_PLUS), async (req, res, next) => {
|
||||
try {
|
||||
const timer = await svc.getTimerById(models, parseInt(req.params.id, 10), req.user.organizationId);
|
||||
|
||||
@@ -50,7 +50,7 @@ async function launchTimer(models, req, timerId, orgId) {
|
||||
const sessionId = `timer_${timerId}_${Date.now()}`;
|
||||
const engine = new TimerEngine(timer.toJSON(), io, sessionId);
|
||||
|
||||
activeSessions.set(sessionId, { engine, orgId });
|
||||
activeSessions.set(sessionId, { engine, orgId, timerName: timer.name });
|
||||
engine.start();
|
||||
|
||||
await auditLog(req, 'timer.launch', 'timer', timerId, { sessionId });
|
||||
@@ -70,7 +70,7 @@ async function stopTimer(req, sessionId, orgId) {
|
||||
function getActiveSession(orgId) {
|
||||
for (const [sessionId, session] of activeSessions) {
|
||||
if (session.orgId === orgId) {
|
||||
return { sessionId, state: session.engine.getState() };
|
||||
return { sessionId, timerName: session.timerName, state: session.engine.getState() };
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user