[AJUDA] Strapi v4 Next Auth Credentials
Ola! estou com o seguinte problema, estou tentando configurar o Next Auth no meu projeto com NextJs no Frond End e o strapi no BackEnd. Mas mesmo digitando o email e senha corretamente, da o error "POST http://localhost:3000/api/auth/callback/credentials? 401 (Unauthorized)".
Usando o Insomnia passa normalmente, "status:200"
[...nextauth].js
jwt: {
signingKey: process.env.JWT_SECRET
},
secret: process.env.NEXT_AUTH_SECRET,
session: {
jwt: true,
maxAge: 30 * 24 * 60 * 60
},
pages: {
signIn: '/entrar'
},
providers: [
CredentialsProvider({
// The name to display on the sign in form (e.g. "Sign in with...")
name: 'credentials',
credentials: {},
async authorize(email, password) {
const response = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}api/auth/local`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
bady: new URLSearchParams({
identifier: email,
password: password
})
}
)
const data = await response.json()
if (data.user) {
return { ...data.user, jwt: data.jwt }
} else {
return null
}
}
})
],
callbacks: {
session: async (session, user) => {
session.jwt = user.jwt
session.id = user.id
return Promise.resolve(session)
},
jwt: async (token, user) => {
if (user) {
token.id = user.id
token.email = user.email
token.name = user.name
token.jwt = user.jwt
}
return Promise.resolve(token)
}
}
}
const Auth = (req, res) => NextAuth(req, res, options)
export default Auth
Pagina de Login Entrar
import * as S from './styled'
import { useEffect, useState } from 'react'
import { signIn } from 'next-auth/react'
import { useRouter } from 'next/router'
import TextField from '../TextFild'
export default function FormEntrar() {
const [values, setValues] = useState({ email: '', password: '' })
const { push } = useRouter()
const handleInput = (field, value) => {
setValues((e) => ({ ...e, [field]: value }))
}
const handleSubmit = async (event) => {
event.preventDefault()
const result = await signIn('credentials', {
...values,
redirect: false,
callbackUrl: '/'
})
if (result?.url) {
return push(result.url)
}
console.error('email ou senha inválidos')
console.log(values)
}
return (
<S.Wrapper>
<form onSubmit={handleSubmit}>
<TextField
name="email"
placeholder="Email"
type="email"
onInputChange={(v) => handleInput('email', v)}
required
/>
<TextField
name="password"
placeholder="Senha"
type="password"
onInputChange={(v) => handleInput('password', v)}
required
/>
<S.EsqueceuASenha href="a">Esqueceu Sua Senha?</S.EsqueceuASenha>
<S.Button type="submit">Entrar</S.Button>
<S.FormLink>
Nao tem uma conta?
<Link href="/inscrevase">Inscreva-se agora</Link>
</S.FormLink>
</form>
</S.Wrapper>
)
}
estou a alguma tempo tentando resolver isso, mas esta muito dificil!
se algum puder ajudar agradeço muito!