mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-07 19:50:20 +00:00
fixed issue with login that keeps returning.
This commit is contained in:
1 parent
ea38775a82
commit
11fc5c8b3d
5 files changed
+392
-142
No files matched your search
@@ -1,16 +1,15 @@
|
||||
import NextAuth from "next-auth"; // <-- Changed from "next-auth/next"
|
||||
import NextAuth from "next-auth";
|
||||
import CredentialsProvider from "next-auth/providers/credentials";
|
||||
import { validateCredentials } from "@/actions/auth";
|
||||
import jwt from "jsonwebtoken";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
// Use a consistent secret for JWT signing and verification
|
||||
const JWT_SECRET = process.env.NEXTAUTH_SECRET || "banana";
|
||||
console.log("Using NextAuth Secret:", JWT_SECRET);
|
||||
const JWT_SECRET =
|
||||
process.env.NEXTAUTH_SECRET ||
|
||||
"your-fallback-secret-should-be-at-least-32-chars";
|
||||
|
||||
export const authOptions = {
|
||||
providers: [
|
||||
// Correct syntax: no function call, just reference the imported object
|
||||
{
|
||||
id: "credentials",
|
||||
name: "Credentials",
|
||||
@@ -25,18 +24,14 @@ export const authOptions = {
|
||||
console.log("Missing credentials");
|
||||
return null;
|
||||
}
|
||||
|
||||
// Validate credentials against our database
|
||||
const user = await validateCredentials(
|
||||
credentials.username,
|
||||
credentials.password
|
||||
);
|
||||
|
||||
if (user) {
|
||||
console.log("User authenticated:", user.username);
|
||||
return user;
|
||||
}
|
||||
|
||||
console.log("Invalid credentials for:", credentials.username);
|
||||
return null;
|
||||
} catch (error) {
|
||||
@@ -46,27 +41,10 @@ export const authOptions = {
|
||||
},
|
||||
},
|
||||
],
|
||||
|
||||
session: {
|
||||
strategy: "jwt",
|
||||
maxAge: 30 * 24 * 60 * 60, // 30 days
|
||||
maxAge: 30 * 24 * 60 * 60,
|
||||
},
|
||||
|
||||
jwt: {
|
||||
secret: JWT_SECRET,
|
||||
encode: async ({ secret, token }) => {
|
||||
return jwt.sign(token, secret);
|
||||
},
|
||||
decode: async ({ secret, token }) => {
|
||||
try {
|
||||
return jwt.verify(token, secret);
|
||||
} catch (error) {
|
||||
console.error("JWT decode error:", error);
|
||||
return null;
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
callbacks: {
|
||||
async jwt({ token, user }) {
|
||||
if (user) {
|
||||
@@ -77,7 +55,6 @@ export const authOptions = {
|
||||
}
|
||||
return token;
|
||||
},
|
||||
|
||||
async session({ session, token }) {
|
||||
if (token) {
|
||||
session.user = session.user || {};
|
||||
@@ -88,31 +65,28 @@ export const authOptions = {
|
||||
}
|
||||
return session;
|
||||
},
|
||||
async redirect({ baseUrl, session }) {
|
||||
// Redirect admins to dashboard, users with a team to their team page,
|
||||
// otherwise to team selection.
|
||||
if (session?.user) {
|
||||
if (session.user.role === "admin") {
|
||||
return `${baseUrl}/admin/dashboard`;
|
||||
}
|
||||
if (session.user.teamId) {
|
||||
return `${baseUrl}/team/${session.user.teamId}`;
|
||||
}
|
||||
}
|
||||
return `${baseUrl}/team-selection`;
|
||||
},
|
||||
},
|
||||
|
||||
pages: {
|
||||
signIn: "/login",
|
||||
error: "/login?error=true",
|
||||
},
|
||||
|
||||
// Add debugging to help identify issues with API responses
|
||||
debug: process.env.NODE_ENV === "development",
|
||||
|
||||
// Add logger to catch issues
|
||||
logger: {
|
||||
error(code, metadata) {
|
||||
console.error(`NextAuth error: ${code}`, metadata);
|
||||
},
|
||||
warn(code) {
|
||||
console.warn(`NextAuth warning: ${code}`);
|
||||
},
|
||||
debug(code, metadata) {
|
||||
console.log(`NextAuth debug: ${code}`, metadata);
|
||||
},
|
||||
},
|
||||
secret: JWT_SECRET,
|
||||
};
|
||||
|
||||
// Token helpers
|
||||
export function createToken(payload) {
|
||||
return jwt.sign(payload, JWT_SECRET, { expiresIn: "30d" });
|
||||
}
|
||||
@@ -126,9 +100,9 @@ export function verifyToken(token) {
|
||||
}
|
||||
}
|
||||
|
||||
// Set session runtime to nodejs (not edge for better compatibility)
|
||||
export const runtime = "nodejs";
|
||||
// Ensure that the NextAuth export is callable over interop
|
||||
const NextAuthFn = NextAuth.default ? NextAuth.default : NextAuth;
|
||||
const handler = NextAuthFn(authOptions);
|
||||
|
||||
// Create the handler with correct NextAuth instantiation
|
||||
const handler = NextAuth(authOptions);
|
||||
export { handler as GET, handler as POST };
|
||||
export const runtime = "nodejs";
|
||||
Reference in new issue
Block a user