mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-07 19:50:20 +00:00
135 lines
3.5 KiB
JavaScript
135 lines
3.5 KiB
JavaScript
import NextAuth from "next-auth"; // <-- Changed from "next-auth/next"
|
|
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);
|
|
|
|
export const authOptions = {
|
|
providers: [
|
|
// Correct syntax: no function call, just reference the imported object
|
|
{
|
|
id: "credentials",
|
|
name: "Credentials",
|
|
type: "credentials",
|
|
credentials: {
|
|
username: { label: "Username", type: "text" },
|
|
password: { label: "Password", type: "password" },
|
|
},
|
|
async authorize(credentials) {
|
|
try {
|
|
if (!credentials?.username || !credentials?.password) {
|
|
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) {
|
|
console.error("Authentication error:", error);
|
|
return null;
|
|
}
|
|
},
|
|
},
|
|
],
|
|
|
|
session: {
|
|
strategy: "jwt",
|
|
maxAge: 30 * 24 * 60 * 60, // 30 days
|
|
},
|
|
|
|
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) {
|
|
token.id = user.id;
|
|
token.role = user.role;
|
|
token.username = user.username;
|
|
token.teamId = user.team_id || user.teamId;
|
|
}
|
|
return token;
|
|
},
|
|
|
|
async session({ session, token }) {
|
|
if (token) {
|
|
session.user = session.user || {};
|
|
session.user.id = token.id;
|
|
session.user.role = token.role;
|
|
session.user.username = token.username;
|
|
session.user.teamId = token.teamId;
|
|
}
|
|
return session;
|
|
},
|
|
},
|
|
|
|
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);
|
|
},
|
|
},
|
|
};
|
|
|
|
// Token helpers
|
|
export function createToken(payload) {
|
|
return jwt.sign(payload, JWT_SECRET, { expiresIn: "30d" });
|
|
}
|
|
|
|
export function verifyToken(token) {
|
|
try {
|
|
return jwt.verify(token, JWT_SECRET);
|
|
} catch (error) {
|
|
console.error("Token verification error:", error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// Set session runtime to nodejs (not edge for better compatibility)
|
|
export const runtime = "nodejs";
|
|
|
|
// Create the handler with correct NextAuth instantiation
|
|
const handler = NextAuth(authOptions);
|
|
export { handler as GET, handler as POST };
|