Temp user updates and several bug fixes
This commit is contained in:
@@ -50,7 +50,25 @@ class AuthController {
|
||||
error: 'Invalid email or password'
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// Check if temp user has expired
|
||||
const userType = user.UserType || user.userType || 'user';
|
||||
if (userType === 'temp') {
|
||||
const expiration = user.ExpiresAt || user.expiresAt || user.Expiration || user.expiration;
|
||||
if (expiration) {
|
||||
const expirationDate = new Date(expiration);
|
||||
const now = new Date();
|
||||
|
||||
if (now > expirationDate) {
|
||||
logger.warn(`Expired temp user attempted login: ${email}, expired: ${expiration}`);
|
||||
return res.status(401).json({
|
||||
success: false,
|
||||
error: 'Account has expired. Please contact an administrator.'
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update last login time
|
||||
try {
|
||||
const userId = extractId(user);
|
||||
@@ -73,6 +91,7 @@ class AuthController {
|
||||
req.session.userEmail = user.email || user.Email; // Make sure this is set
|
||||
req.session.userName = user.name || user.Name;
|
||||
req.session.isAdmin = user.admin || user.Admin || false;
|
||||
req.session.userType = user.UserType || user.userType || (req.session.isAdmin ? 'admin' : 'user');
|
||||
|
||||
logger.info('User logged in:', {
|
||||
email: req.session.userEmail,
|
||||
@@ -97,7 +116,8 @@ class AuthController {
|
||||
user: {
|
||||
email: email,
|
||||
name: req.session.userName,
|
||||
isAdmin: req.session.isAdmin
|
||||
isAdmin: req.session.isAdmin,
|
||||
userType: req.session.userType
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -128,12 +148,48 @@ class AuthController {
|
||||
}
|
||||
|
||||
async check(req, res) {
|
||||
// If user is authenticated, check for temp user expiration
|
||||
if (req.session?.authenticated && req.session?.userType === 'temp' && req.session?.userEmail) {
|
||||
try {
|
||||
const user = await nocodbService.getUserByEmail(req.session.userEmail);
|
||||
if (user) {
|
||||
const expiration = user.ExpiresAt || user.ExpiresAt || user.Expiration || user.expiration;
|
||||
if (expiration) {
|
||||
const expirationDate = new Date(expiration);
|
||||
const now = new Date();
|
||||
|
||||
if (now > expirationDate) {
|
||||
logger.warn(`Expired temp user session detected in check: ${req.session.userEmail}, expired: ${expiration}`);
|
||||
|
||||
// Destroy the session
|
||||
req.session.destroy((err) => {
|
||||
if (err) {
|
||||
logger.error('Session destroy error:', err);
|
||||
}
|
||||
});
|
||||
|
||||
return res.json({
|
||||
authenticated: false,
|
||||
user: null,
|
||||
expired: true,
|
||||
message: 'Account has expired. Please contact an administrator.'
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error('Error checking temp user expiration in check:', error.message);
|
||||
// Don't fail the check on database errors, just log it
|
||||
}
|
||||
}
|
||||
|
||||
res.json({
|
||||
authenticated: req.session?.authenticated || false,
|
||||
user: req.session?.authenticated ? {
|
||||
email: req.session.userEmail,
|
||||
name: req.session.userName,
|
||||
isAdmin: req.session.isAdmin || false
|
||||
isAdmin: req.session.isAdmin || false,
|
||||
userType: req.session.userType || 'user'
|
||||
} : null
|
||||
});
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ class UsersController {
|
||||
|
||||
async create(req, res) {
|
||||
try {
|
||||
const { email, password, name, admin } = req.body;
|
||||
const { email, password, name, isAdmin, userType, expireDays } = req.body;
|
||||
|
||||
if (!email || !password) {
|
||||
return res.status(400).json({
|
||||
@@ -81,6 +81,14 @@ class UsersController {
|
||||
});
|
||||
}
|
||||
|
||||
// Calculate expiration date for temp users
|
||||
let expiresAt = null;
|
||||
if (userType === 'temp' && expireDays) {
|
||||
const expirationDate = new Date();
|
||||
expirationDate.setDate(expirationDate.getDate() + expireDays);
|
||||
expiresAt = expirationDate.toISOString();
|
||||
}
|
||||
|
||||
// Create new user - use the actual column names from your table
|
||||
const userData = {
|
||||
Email: email,
|
||||
@@ -89,9 +97,13 @@ class UsersController {
|
||||
password: password,
|
||||
Name: name || '',
|
||||
name: name || '',
|
||||
Admin: admin === true,
|
||||
admin: admin === true
|
||||
// Removed created_at fields as they might not exist
|
||||
Admin: isAdmin === true,
|
||||
admin: isAdmin === true,
|
||||
UserType: userType || 'user',
|
||||
userType: userType || 'user',
|
||||
CreatedAt: new Date().toISOString(),
|
||||
ExpiresAt: expiresAt,
|
||||
ExpireDays: userType === 'temp' ? expireDays : null
|
||||
};
|
||||
|
||||
const response = await nocodbService.create(
|
||||
@@ -106,7 +118,9 @@ class UsersController {
|
||||
id: extractId(response),
|
||||
email: email,
|
||||
name: name,
|
||||
admin: admin
|
||||
admin: isAdmin,
|
||||
userType: userType,
|
||||
expiresAt: expiresAt
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user