I am trying to set up LDAP authentication in my application, but I have an error.
When I click on the button, the form becomes empty, and nothing happens. I try again, and I have the following error : error
My [...nextauth].js :
const ldap = require("ldapjs")
import NextAuth from "next-auth"
import CredentialsProvider from "next-auth/providers/credentials"
export default NextAuth({
providers: [
CredentialsProvider({
name: "LDAP",
credentials: {
username: { label: "DN", type: "text", placeholder: "" },
password: { label: "Password", type: "password" },
},
async authorize(credentials, req) {
// You might want to pull this call out so we're not making a new LDAP client on every login attemp
const client = ldap.createClient({
url: "ldap://192.168.1.143:389",
tlsOptions: { rejectUnauthorized: false }
})
// Essentially promisify the LDAPJS client.bind function
return new Promise((resolve, reject) => {
client.bind(credentials.username, credentials.password, (error) => {
if (error) {
console.error("Failed", error)
reject()
} else {
console.log("Logged in")
resolve({
username: credentials.username,
password: credentials.password,
})
}
})
})
},
}),
],
callbacks: {
async jwt({ token, user }) {
const isSignIn = user ? true : false
if (isSignIn) {
token.username = user.username
token.password = user.password
}
return token
},
async session({ session, token }) {
return { ...session, user: { username: token.username } }
},
}
})
My _app.js
import { SessionProvider } from "next-auth/react"
import Layout from '@/components/layout/Layout'
import '@/styles/globals.css'
export default function App({ Component, pageProps: { session, ...pageProps } }) {
return (
<SessionProvider session={session}>
<Layout>
<Component {...pageProps} />
</Layout>
</SessionProvider>
)
}
My index.js
import { useSession } from "next-auth/react"
import { Inter } from 'next/font/google'
const inter = Inter({ subsets: ['latin'] })
export default function Home() {
const { status } = useSession({
required: true,
})
return (
<>
</>
)
}
Can someone help me ? Thanks.
I followed this documentation : https://next-auth.js.org/tutorials/ldap-auth-example