mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-08 03:50:21 +00:00
FIX OF ALL ISSUES WITH LOG IN PAGE. EVERYTHING TO THE POINT OF TESTING THE MAP WORKS SO FAR.
This commit is contained in:
1 parent
84f010cb87
commit
c79a9636a7
10 files changed
+228
-233
No files matched your search
@@ -3,6 +3,7 @@ import CredentialsProvider from "next-auth/providers/credentials";
|
||||
import { validateCredentials } from "@/actions/auth";
|
||||
import jwt from "jsonwebtoken";
|
||||
import { cookies } from "next/headers";
|
||||
import { queryOne } from "@/lib/db";
|
||||
|
||||
const JWT_SECRET =
|
||||
process.env.NEXTAUTH_SECRET ||
|
||||
@@ -53,6 +54,14 @@ 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;
|
||||
}
|
||||
return token;
|
||||
},
|
||||
async session({ session, token }) {
|
||||
@@ -92,30 +101,9 @@ export function createToken(payload) {
|
||||
}
|
||||
|
||||
export function verifyToken(token) {
|
||||
if (!token) return null;
|
||||
try {
|
||||
if (!token) return null;
|
||||
const parts = token.split(".");
|
||||
if (parts.length === 2) {
|
||||
// Custom token verification
|
||||
const [encodedData, signature] = parts;
|
||||
const expectedSignature = Buffer.from(
|
||||
`${encodedData}.${JWT_SECRET}`
|
||||
).toString("base64");
|
||||
if (signature !== expectedSignature) {
|
||||
console.error("Signature mismatch");
|
||||
return null;
|
||||
}
|
||||
const data = JSON.parse(
|
||||
Buffer.from(encodedData, "base64").toString("utf-8")
|
||||
);
|
||||
if (data.exp && data.exp < Math.floor(Date.now() / 1000)) return null;
|
||||
return data;
|
||||
} else if (parts.length === 3) {
|
||||
// Standard JWT verification
|
||||
return jwt.verify(token, JWT_SECRET);
|
||||
} else {
|
||||
throw new Error("Invalid token format");
|
||||
}
|
||||
return jwt.verify(token, JWT_SECRET);
|
||||
} catch (error) {
|
||||
console.error("Token verification error:", error);
|
||||
return null;
|
||||
|
||||
Reference in new issue
Block a user