Fix local network authentication
This commit is contained in:
@@ -38,7 +38,13 @@ const corsOptions = {
|
||||
|
||||
console.log('CORS check for origin:', origin);
|
||||
|
||||
// Check if origin is allowed
|
||||
// Allow local network IPs (192.168.1.xxx)
|
||||
if (origin && origin.match(/^http:\/\/192\.168\.1\.\d{1,3}(:\d+)?$/)) {
|
||||
callback(null, true);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if origin is in allowed list
|
||||
if (!origin || allowedOrigins.indexOf(origin) !== -1) {
|
||||
callback(null, true);
|
||||
} else {
|
||||
@@ -96,19 +102,19 @@ app.post('/login', (req, res) => {
|
||||
expiresIn: '24h'
|
||||
});
|
||||
|
||||
// Determine if request is from localhost
|
||||
const isLocalhost = req.headers.origin?.includes('localhost');
|
||||
// Determine if request is from local network
|
||||
const isLocalNetwork = req.headers.origin?.includes('192.168.1.') || req.headers.origin?.includes('localhost');
|
||||
|
||||
const cookieOptions = {
|
||||
httpOnly: true,
|
||||
secure: !isLocalhost,
|
||||
sameSite: isLocalhost ? 'lax' : 'none',
|
||||
secure: !isLocalNetwork, // Only use secure for non-local requests
|
||||
sameSite: isLocalNetwork ? 'lax' : 'none',
|
||||
path: '/',
|
||||
maxAge: 24 * 60 * 60 * 1000 // 24 hours
|
||||
};
|
||||
|
||||
// Add domain only for production
|
||||
if (!isLocalhost) {
|
||||
// Only set domain for production
|
||||
if (!isLocalNetwork) {
|
||||
cookieOptions.domain = '.kent.pw';
|
||||
}
|
||||
|
||||
@@ -163,13 +169,13 @@ app.get('/check', (req, res) => {
|
||||
});
|
||||
|
||||
app.post('/logout', (req, res) => {
|
||||
const isLocalhost = req.headers.origin?.includes('localhost');
|
||||
const isLocalNetwork = req.headers.origin?.includes('192.168.1.') || req.headers.origin?.includes('localhost');
|
||||
const cookieOptions = {
|
||||
httpOnly: true,
|
||||
secure: !isLocalhost,
|
||||
sameSite: isLocalhost ? 'lax' : 'none',
|
||||
secure: !isLocalNetwork,
|
||||
sameSite: isLocalNetwork ? 'lax' : 'none',
|
||||
path: '/',
|
||||
domain: isLocalhost ? undefined : '.kent.pw'
|
||||
domain: isLocalNetwork ? undefined : '.kent.pw'
|
||||
};
|
||||
|
||||
console.log('Clearing cookie with options:', cookieOptions);
|
||||
|
||||
Reference in New Issue
Block a user