Hello everyone on stackoverflow,
I could really use some assistance, please.
Currently, I am dealing with a GraphQL data source and utilizing apollo client to fetch that data. Specifically, I am focusing on my login function where I am incorporating the next-auth credential provider: [Modified code below showing the fetch call to graphql]
import NextAuth from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
export default NextAuth({
session: {
strategy: 'jwt',
},
callbacks: {
async jwt({ token, user}: any) {
if(user?._id) token._id = user._id;
return token;
},
async session({ session, token}: any) {
if(token?._id) session.user._id = token._id;
return session;
},
},
providers: [
CredentialsProvider({
async authorize(credentials: any) {
const query = `query User($email: String!) { user(email: $email) { id, username, email, password, }}`;
const response: any = await fetch('http://localhost:4000/graphql', {
method: "POST",
headers: {"Content-Type": "application/json","Accept": "application/json", },
body: JSON.stringify({query, variables: { email: credentials.email }})
});
const {data}: any = await response.json();
if(data) {
return {
_id: data.user.id,
name: data.user.username,
email: data.user.email,
};
}
throw new Error("Invalid email or password");
},
}),
],
});
Here is my getUser hook [Omitted this section as it is not relevant anymore]
// import { useQuery, gql } from '@apollo/client';
// const Get_User = gql`
// query User($email: String!) {
// user(email: $email) {
// id
// username
// email
// password
// }
// }
// `;
// export default function getUser(email: any) {
// const { error, data } = useQuery(Get_User, {variables: {email}});
// return {
// error,
// data,
// }
// }
I have confirmed that my next-auth endpoint is operational by disabling the GraphQL getUser and adjusting the if statement comparison to itself (credential.password === credential.password), returning static data instead.
The values within the credentials object are accessible correctly.
[YES I WAS :( ] I believe I may be breaching some rules regarding react hooks here, but I am struggling to pinpoint the issue. Any insights would be greatly appreciated. Thank you in advance! ^-^
It appears there might be an issue with the timing of my fetch calls as the user details are not being returned as expected, even though the fetch request works when tested on another page.