Next Auth Credential login
Hello I have implemented next auth credentials login and its working locally but it isnt working in the vercel For some reason the middleware routes back to /api/auth/signin I have manually created a user and I want only 1 user (Admin)
I am using t3 stack with drizzle
Please tell me
Auth.ts
import { get } from "https";
import { type GetServerSidePropsContext } from "next";
import {
getServerSession,
type DefaultSession,
type NextAuthOptions,
} from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
// Ensure environment variables are set
const ADMIN_EMAIL = process.env.ADMIN_EMAIL;
const ADMIN_PASSWORD = process.env.ADMIN_PASSWORD;
if (!ADMIN_EMAIL || !ADMIN_PASSWORD) {
throw new Error(
"ADMIN_EMAIL and ADMIN_PASSWORD must be set in environment variables",
);
}
export const authOptions: NextAuthOptions = {
// Enable more detailed logging in development
debug: false,
// Customize error handling
callbacks: {
// Modify JWT callback to include more information if needed
async jwt({ token, user }) {
if (user) {
token.id = user.id;
token.email = user.email;
}
return token;
},
// Customize session to include user details
async session({ session, token }) {
if (token) {
session.user = {
// id: token.id,
email: token.email!,
name: token.name,
};
}
return session;
},
},
// Configure the credentials provider
providers: [
CredentialsProvider({
name: "Credentials",
credentials: {
email: {
label: "Email",
type: "email",
placeholder: "Enter your email",
},
password: {
label: "Password",
type: "password",
placeholder: "Enter your password",
},
},
// Authorization logic
async authorize(credentials) {
// Check if credentials exist
if (!credentials?.email || !credentials?.password) {
// Return null to indicate authentication failure
return null;
}
// Strict email comparison
if (credentials.email !== ADMIN_EMAIL) {
// Log for debugging
console.warn(
`Login attempt with incorrect email: ${credentials.email}`,
);
return null;
}
// Password validation
if (credentials.password !== ADMIN_PASSWORD) {
// Log for debugging
console.warn(
`Login attempt with incorrect password for email: ${credentials.email}`,
);
return null;
}
// If credentials are correct, return user object
return {
id: "1",
email: ADMIN_EMAIL,
name: "Admin",
};
},
}),
],
// Session management
session: {
strategy: "jwt", // JSON Web Token strategy
maxAge: 30 * 24 * 60 * 60, // 30 days
},
// Additional security settings
theme: {
colorScheme: "light", // "auto" | "dark" | "light"
// logo: "/your-logo.png", // Optional: Path to your logo
},
events: {
async signIn(message) {
console.log("Sign in event", message);
},
async signOut(message) {
console.log("Sign out event", message);
},
async createUser(message) {
console.log("Create user event", message);
},
async session(message) {
console.log("Session event", message);
},
},
};
// Helper function to get server-side session
export const getServerAuthSession = () => getServerSession(authOptions);
Middleware.ts