feat(auth): enhance session and user deserialization logging
- Added detailed logging for session checks in the auth controller, including session ID and full session object. - Implemented user existence verification in the deserialize function, with improved logging for user checks and database query results. - Enhanced the serialize function to log the serialized user object and completion of the serialization process, improving traceability in authentication workflows.
This commit is contained in:
@@ -32,8 +32,27 @@ export default class AuthController extends WorklenzControllerBase {
|
||||
console.log("req.user:", req.user);
|
||||
console.log("req.isAuthenticated():", req.isAuthenticated());
|
||||
console.log("req.session.passport:", (req.session as any).passport);
|
||||
console.log("req.session.id:", req.sessionID);
|
||||
console.log("Full session object:", JSON.stringify(req.session, null, 2));
|
||||
console.log("req.query.strategy:", req.query.strategy);
|
||||
|
||||
// Check if session exists in database
|
||||
if (req.sessionID) {
|
||||
db.query("SELECT sid, sess FROM pg_sessions WHERE sid = $1", [req.sessionID])
|
||||
.then(result => {
|
||||
if (result.rows.length > 0) {
|
||||
console.log("Session found in database:");
|
||||
console.log("Session ID:", result.rows[0].sid);
|
||||
console.log("Session data:", JSON.stringify(result.rows[0].sess, null, 2));
|
||||
} else {
|
||||
console.log("Session NOT FOUND in database for ID:", req.sessionID);
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
console.log("Error checking session in database:", err);
|
||||
});
|
||||
}
|
||||
|
||||
// Flash messages sent from passport-local-signup.ts and passport-local-login.ts
|
||||
const errors = req.flash()["error"] || [];
|
||||
const messages = req.flash()["success"] || [];
|
||||
|
||||
@@ -33,11 +33,23 @@ export async function deserialize(user: { id: string | null }, done: IDeserializ
|
||||
const {id} = user;
|
||||
console.log("Deserializing user ID:", id);
|
||||
|
||||
// First check if user exists in users table
|
||||
const userCheck = await db.query("SELECT id, active_team FROM users WHERE id = $1", [id]);
|
||||
console.log("User exists check:", userCheck.rowCount, userCheck.rows[0]);
|
||||
|
||||
if (!userCheck.rowCount) {
|
||||
console.log("User not found in users table");
|
||||
return done(null, null);
|
||||
}
|
||||
|
||||
const excludedSubscriptionTypes = ["TRIAL", "PADDLE"];
|
||||
const q = `SELECT deserialize_user($1) AS user;`;
|
||||
console.log("Calling deserialize_user with ID:", id);
|
||||
|
||||
const result = await db.query(q, [id]);
|
||||
|
||||
console.log("Database query result rows length:", result.rows.length);
|
||||
console.log("Raw database result:", result.rows);
|
||||
|
||||
if (result.rows.length) {
|
||||
const [data] = result.rows;
|
||||
@@ -58,7 +70,7 @@ export async function deserialize(user: { id: string | null }, done: IDeserializ
|
||||
console.log("Returning successful user:", data.user);
|
||||
return done(null, data.user as IPassportSession);
|
||||
}
|
||||
console.log("No user data in result");
|
||||
console.log("No user data in result - deserialize_user returned null");
|
||||
}
|
||||
console.log("No rows returned from database");
|
||||
|
||||
|
||||
@@ -7,5 +7,10 @@ export function serialize($user: IPassportSession, done: ISerializeCallback) {
|
||||
console.log("Serializing user:", $user);
|
||||
console.log("User ID:", $user?.id);
|
||||
|
||||
done(null, { id: $user?.id ?? null });
|
||||
const serializedUser = { id: $user?.id ?? null };
|
||||
console.log("Serialized user object:", serializedUser);
|
||||
|
||||
done(null, serializedUser);
|
||||
|
||||
console.log("Serialize done callback completed");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user