Next-Auth "signIn()" function not working as expected
Answered
Exzotic posted this in #help-forum
ExzoticOP
Hello! I am trying to login from a custom signin page, am using this code on the login page:
(the return value is undefined)
(the return value is undefined)
const handleSubmit = async () => {
await signIn("credentials", { redirect: true, callbackUrl: "/" }, { username: emailRef.current!.value, password: passwordRef.current!.value });
};20 Replies
ExzoticOP
export const authOptions: AuthOptions = {
pages: {
signIn: "/login",
newUser: "/"
},
providers: [
CredentialsProvider({
name: "Credentials",
credentials: {
email: { type: 'email', placeholder: 'email@email.com' },
password: { type: 'password', placeholder: '*****' },
},
// @ts-ignore
async authorize(credentials, req) {
if (!credentials?.email || !credentials?.password) {
return null
}
const user = await prisma.user.findUnique({
where: {
email: credentials.email
}
})
if (!user) return;
if(!await compare(credentials.password, user.password!)) return;
return user;
},
})
],
session: {
strategy: "jwt"
}
}Heres my config ^
signin should take you to a different page
idk if I've ever seen credentials passed like that
@Marchy signin should take you to a different page
ExzoticOP
The signin page is on
/loginand the way I passed credentials is just how the ts types say
That's used for this
https://next-auth.js.org/configuration/pages#email-sign-in
https://next-auth.js.org/configuration/pages#email-sign-in
ExzoticOP
ah right
await signIn("credentials", { username: emailRef.current!.value, password: passwordRef.current!.value, redirect: true, callbackUrl: "/" })changing it to this doesnt help much either
<button onClick={() => signIn("credentials")}>Sign in</button>ExzoticOP
do I not need to pass data
nope, it redirects to the login page that next-auth generates
if it's configured
ExzoticOP
But I want to use this custom page on
/login instead of the default oneExzoticOP
Ive looked up stuff on youtube and in the docs but im not doing anything different
@Exzotic ts
export const authOptions: AuthOptions = {
pages: {
signIn: "/login",
newUser: "/"
},
providers: [
CredentialsProvider({
name: "Credentials",
credentials: {
email: { type: 'email', placeholder: 'email@email.com' },
password: { type: 'password', placeholder: '*****' },
},
// @ts-ignore
async authorize(credentials, req) {
if (!credentials?.email || !credentials?.password) {
return null
}
const user = await prisma.user.findUnique({
where: {
email: credentials.email
}
})
if (!user) return;
if(!await compare(credentials.password, user.password!)) return;
return user;
},
})
],
session: {
strategy: "jwt"
}
}
Heres my config ^
ExzoticOP
You can see the config here then in my client
/login page i run:await signIn("credentials", { username: "a@a.com", password: "password" })which is a valid user but it returns undefined
ExzoticOP
Oh I was using "username" instead of email, my bad 🤣
Answer