mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-08 03:50:21 +00:00
Fixed join team issue
This commit is contained in:
1 parent
b278c96776
commit
0d57df2559
8 files changed
+381
-71
No files matched your search
@@ -54,13 +54,26 @@ export const authOptions = {
|
||||
token.username = user.username;
|
||||
token.teamId = user.team_id || user.teamId;
|
||||
}
|
||||
|
||||
// If teamId is not set on the token, query DB for the latest value.
|
||||
if (!token.teamId) {
|
||||
const freshUser = await queryOne(
|
||||
"SELECT team_id FROM users WHERE id = ?",
|
||||
[token.id]
|
||||
);
|
||||
token.teamId = freshUser?.team_id;
|
||||
try {
|
||||
const freshUser = await queryOne(
|
||||
"SELECT team_id FROM users WHERE id = ?",
|
||||
[token.id]
|
||||
);
|
||||
token.teamId = freshUser?.team_id;
|
||||
} catch (error) {
|
||||
console.error("Error refreshing teamId from database:", error);
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure both sub and id exist for maximum compatibility
|
||||
if (token.id && !token.sub) {
|
||||
token.sub = token.id.toString();
|
||||
}
|
||||
if (token.sub && !token.id) {
|
||||
token.id = parseInt(token.sub, 10);
|
||||
}
|
||||
return token;
|
||||
},
|
||||
@@ -101,11 +114,42 @@ export function createToken(payload) {
|
||||
}
|
||||
|
||||
export function verifyToken(token) {
|
||||
if (!token) return null;
|
||||
try {
|
||||
return jwt.verify(token, JWT_SECRET);
|
||||
if (!token) {
|
||||
console.error("No token provided");
|
||||
return null;
|
||||
}
|
||||
|
||||
console.log("Verifying token:", token.substring(0, 10) + "...");
|
||||
|
||||
const secret =
|
||||
process.env.NEXTAUTH_SECRET ||
|
||||
"your-fallback-secret-should-be-at-least-32-chars";
|
||||
|
||||
const decoded = jwt.verify(token, secret);
|
||||
|
||||
// Validate that we have the minimum required fields
|
||||
if (!decoded.sub && !decoded.id) {
|
||||
console.error("Token missing required user identifier");
|
||||
return null;
|
||||
}
|
||||
|
||||
// Log successful token verification with user ID
|
||||
console.log(
|
||||
"Token verified successfully for user:",
|
||||
decoded.sub || decoded.id
|
||||
);
|
||||
|
||||
return decoded;
|
||||
} catch (error) {
|
||||
console.error("Token verification error:", error);
|
||||
// Provide more detailed error logs
|
||||
if (error.name === "TokenExpiredError") {
|
||||
console.error("Token expired at:", error.expiredAt);
|
||||
} else if (error.name === "JsonWebTokenError") {
|
||||
console.error("Invalid token:", error.message);
|
||||
} else {
|
||||
console.error("Token verification error:", error.message);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user